You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2009/11/15 21:47:42 UTC

svn commit: r836415 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity: jdbc/SqlJdbcUtil.java model/ModelFieldType.java

Author: adrianc
Date: Sun Nov 15 20:47:42 2009
New Revision: 836415

URL: http://svn.apache.org/viewvc?rev=836415&view=rev
Log:
The first step in getting the entity engine to use the new Java object type conversion code. Right now it only works while getting values from the database.

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SqlJdbcUtil.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelFieldType.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SqlJdbcUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SqlJdbcUtil.java?rev=836415&r1=836414&r2=836415&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SqlJdbcUtil.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/SqlJdbcUtil.java Sun Nov 15 20:47:42 2009
@@ -26,10 +26,10 @@
 import java.math.BigDecimal;
 import java.nio.ByteBuffer;
 import java.sql.Blob;
+import java.sql.Clob;
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
-import java.sql.Clob;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
@@ -42,6 +42,8 @@
 
 import javolution.util.FastMap;
 
+import org.ofbiz.base.conversion.Converter;
+import org.ofbiz.base.conversion.Converters;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.ObjectType;
 import org.ofbiz.base.util.UtilGenerics;
@@ -55,7 +57,6 @@
 import org.ofbiz.entity.condition.EntityConditionParam;
 import org.ofbiz.entity.condition.OrderByList;
 import org.ofbiz.entity.config.DatasourceInfo;
-import org.ofbiz.entity.datasource.GenericDAO;
 import org.ofbiz.entity.model.ModelEntity;
 import org.ofbiz.entity.model.ModelField;
 import org.ofbiz.entity.model.ModelFieldType;
@@ -68,7 +69,7 @@
  *
  */
 public class SqlJdbcUtil {
-    public static final String module = GenericDAO.class.getName();
+    public static final String module = SqlJdbcUtil.class.getName();
 
     public static final int CHAR_BUFFER_SIZE = 4096;
 
@@ -487,6 +488,7 @@
         }
     }
 
+    @SuppressWarnings("unchecked")
     public static void getValue(ResultSet rs, int ind, ModelField curField, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException {
         ModelFieldType mft = modelFieldTypeReader.getModelFieldType(curField.getType());
 
@@ -494,6 +496,37 @@
             throw new GenericModelException("definition fieldType " + curField.getType() + " not found, cannot getValue for field " +
                     entity.getEntityName() + "." + curField.getName() + ".");
         }
+
+        // ----- Try out the new converter code -----
+
+        Object sourceObject = null;
+        try {
+            sourceObject = rs.getObject(ind);
+            if (sourceObject == null) {
+                entity.dangerousSetNoCheckButFast(curField, null);
+                return;
+            }
+        } catch (SQLException e) {
+            throw new GenericEntityException(e);
+        }
+        Class<?> targetClass = mft.getJavaClass();
+        if (targetClass != null) {
+            if (targetClass.equals(sourceObject.getClass())) {
+                entity.dangerousSetNoCheckButFast(curField, sourceObject);
+                return;
+            }
+            try {
+                Converter<Object, Object> converter = (Converter<Object, Object>) Converters.getConverter(sourceObject.getClass(), targetClass);
+                entity.dangerousSetNoCheckButFast(curField, converter.convert(sourceObject));
+                return;
+            } catch (Exception e) {
+                Debug.logError(e, module);
+            }
+            Debug.logInfo("Unable to convert, falling back on switch statement", module);
+        }
+
+        // ------------------------------------------
+        
         String fieldType = mft.getJavaType();
 
         try {

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelFieldType.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelFieldType.java?rev=836415&r1=836414&r2=836415&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelFieldType.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelFieldType.java Sun Nov 15 20:47:42 2009
@@ -19,26 +19,41 @@
 package org.ofbiz.entity.model;
 
 import java.io.Serializable;
-import java.util.*;
-import org.w3c.dom.*;
+import java.util.ArrayList;
+import java.util.List;
 
-import org.ofbiz.base.util.*;
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.UtilXml;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
 
 /**
  * Generic Entity - FieldType model class
  *
  */
+
+@SuppressWarnings("serial")
 public class ModelFieldType implements Serializable {
 
+    public static final String module = ModelFieldType.class.getName();
+
     /** The type of the Field */
     protected String type = null;
 
     /** The java-type of the Field */
     protected String javaType = null;
 
+    /** The Java class of the Field */
+    protected Class<?> javaClass = null;
+
     /** The sql-type of the Field */
     protected String sqlType = null;
 
+    /** The sql class of the Field */
+    protected Class<?> sqlClass = null;
+
     /** The sql-type-alias of the Field, this is optional */
     protected String sqlTypeAlias = null;
 
@@ -54,7 +69,6 @@
         this.javaType = UtilXml.checkEmpty(fieldTypeElement.getAttribute("java-type")).intern();
         this.sqlType = UtilXml.checkEmpty(fieldTypeElement.getAttribute("sql-type")).intern();
         this.sqlTypeAlias = UtilXml.checkEmpty(fieldTypeElement.getAttribute("sql-type-alias")).intern();
-
         NodeList validateList = fieldTypeElement.getElementsByTagName("validate");
         for (int i = 0; i < validateList.getLength(); i++) {
             Element element = (Element) validateList.item(i);
@@ -64,6 +78,15 @@
                 this.validators.add(new ModelFieldValidator(className.intern(), methodName.intern()));
             }
         }
+        ((ArrayList<ModelFieldValidator>)this.validators).trimToSize();
+        if (this.javaType != null) {
+            ClassLoader loader = Thread.currentThread().getContextClassLoader();
+            try {
+                this.javaClass = loader.loadClass(this.javaType);
+            } catch (ClassNotFoundException e) {
+                Debug.logError(e, module);
+            }
+        }
     }
 
     /** The type of the Field */
@@ -76,11 +99,23 @@
         return this.javaType;
     }
 
+    /** The Java class of the Field */
+    public Class<?> getJavaClass() {
+        return this.javaClass;
+    }
+
     /** The sql-type of the Field */
     public String getSqlType() {
         return this.sqlType;
     }
 
+    /** Returns the SQL <code>Class</code> of the Field. The returned value might
+     * be <code>null</code>. The SQL class is unknown until a connection is made
+     * to the database. */
+    public Class<?> getSqlClass() {
+        return this.sqlClass;
+    }
+
     /** The sql-type-alias of the Field */
     public String getSqlTypeAlias() {
         return this.sqlTypeAlias;
@@ -91,6 +126,14 @@
         return this.validators;
     }
 
+    /** Sets the SQL <code>Class</code> for this field.
+     * 
+     * @param sqlClass
+     */
+    public synchronized void setSqlClass(Class<?> sqlClass) {
+        this.sqlClass = sqlClass;
+    }
+
     /** A simple function to derive the max length of a String created from the field value, based on the sql-type
      * @return max length of a String representing the Field value
      */