You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by jo...@apache.org on 2008/10/22 03:30:49 UTC

svn commit: r706842 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity: GenericEntity.java condition/EntityExpr.java condition/EntityFunction.java

Author: jonesde
Date: Tue Oct 21 18:30:49 2008
New Revision: 706842

URL: http://svn.apache.org/viewvc?rev=706842&view=rev
Log:
Changed GenericEntity.set data type warning to be far more obnoxious as discussed in the mailing list, not in a different file or anything yet; also added a similar check to the EntityExpr class, called for now just when creating the SQL because before then we don't even know which entity it is, though it would be nice to do in the constructor this at least gets us close when there are issues

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityExpr.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityFunction.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java?rev=706842&r1=706841&r2=706842&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java Tue Oct 21 18:30:49 2008
@@ -410,7 +410,7 @@
                 if (!ObjectType.instanceOf(value, type.getJavaType())) {
                     String errMsg = "In entity field [" + this.getEntityName() + "." + name + "] set the value passed in [" + value.getClass().getName() + "] is not compatible with the Java type of the field [" + type.getJavaType() + "]";
                     // eventually we should do this, but for now we'll do a "soft" failure: throw new IllegalArgumentException(errMsg);
-                    Debug.logWarning(errMsg, module);
+                    Debug.logWarning(new Exception("Location of database type error"), "=-=-=-=-=-=-=-=-= DATABASE TYPE ERROR IN GenericEntity.set =-=-=-=-=-=-=-=-= " + errMsg, module);
                 }
             }
             Object old = fields.put(name, value);

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityExpr.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityExpr.java?rev=706842&r1=706841&r2=706842&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityExpr.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityExpr.java Tue Oct 21 18:30:49 2008
@@ -21,18 +21,23 @@
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javolution.context.ObjectFactory;
 
 import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.ObjectType;
 import org.ofbiz.entity.EntityCryptoException;
 import org.ofbiz.entity.GenericDelegator;
 import org.ofbiz.entity.GenericEntity;
+import org.ofbiz.entity.GenericEntityException;
 import org.ofbiz.entity.GenericModelException;
 import org.ofbiz.entity.GenericValue;
 import org.ofbiz.entity.config.DatasourceInfo;
 import org.ofbiz.entity.model.ModelEntity;
 import org.ofbiz.entity.model.ModelField;
+import org.ofbiz.entity.model.ModelFieldType;
+import org.ofbiz.entity.model.ModelFieldTypeReader;
 
 /**
  * Encapsulates simple expressions used for specifying queries
@@ -100,7 +105,7 @@
         }
         this.operator = operator;
         this.rhs = rhs;
-
+        
         //Debug.logInfo("new EntityExpr internal field=" + lhs + ", value=" + rhs + ", value type=" + (rhs == null ? "null object" : rhs.getClass().getName()), module);
     }
     
@@ -158,6 +163,9 @@
 
     public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, DatasourceInfo datasourceInfo) {
         // if (Debug.verboseOn()) Debug.logVerbose("makeWhereString for entity " + modelEntity.getEntityName(), module);
+        
+        this.checkRhsType(modelEntity, null);
+
         StringBuilder sql = new StringBuilder();
         operator.addSqlValue(sql, modelEntity, entityConditionParams, true, lhs, rhs, datasourceInfo);
         return sql.toString();
@@ -210,6 +218,55 @@
     public void accept(EntityConditionVisitor visitor) {
         visitor.acceptEntityExpr(this);
     }
+    
+    public void checkRhsType(ModelEntity modelEntity, GenericDelegator delegator) {
+        if (this.rhs == null || modelEntity == null) return;
+
+        Object value = this.rhs;
+        if (this.rhs instanceof EntityFunction) {
+            value = ((EntityFunction) this.rhs).getOriginalValue();
+        }
+        
+        if (value instanceof Collection) {
+            Collection valueCol = (Collection) value;
+            if (valueCol.size() > 0) {
+                value = valueCol.iterator().next();
+            }
+        }
+        
+        if (delegator == null) {
+            // this will be the common case for now as the delegator isn't available where we want to do this
+            // we'll cheat a little here and assume the default delegator
+            delegator = GenericDelegator.getGenericDelegator("default");
+        }
+        
+        String fieldName = null;
+        if (this.lhs instanceof EntityFieldValue) {
+            EntityFieldValue efv = (EntityFieldValue) this.lhs;
+            fieldName = efv.getFieldName();
+        } else {
+            // nothing to check
+            return;
+        }
+        
+        ModelField curField = modelEntity.getField(fieldName);
+        ModelFieldType type = null;
+        try {
+            type = delegator.getEntityFieldType(modelEntity, curField.getType());
+        } catch (GenericEntityException e) {
+            Debug.logWarning(e, module);
+        }
+        if (type == null) {
+            throw new IllegalArgumentException("Type " + curField.getType() + " not found for entity [" + modelEntity.getEntityName() + "]; probably because there is no datasource (helper) setup for the entity group that this entity is in: [" + delegator.getEntityGroupName(modelEntity.getEntityName()) + "]");
+        }
+        
+        // make sure the type matches the field Java type
+        if (!ObjectType.instanceOf(value, type.getJavaType())) {
+            String errMsg = "In entity field [" + modelEntity.getEntityName() + "." + curField.getName() + "] set the value passed in [" + value.getClass().getName() + "] is not compatible with the Java type of the field [" + type.getJavaType() + "]";
+            // eventually we should do this, but for now we'll do a "soft" failure: throw new IllegalArgumentException(errMsg);
+            Debug.logWarning(new Exception("Location of database type error"), "=-=-=-=-=-=-=-=-= DATABASE TYPE ERROR in EntityExpr =-=-=-=-=-=-=-=-= " + errMsg, module);
+        }
+    }
 
     public boolean equals(Object obj) {
         if (!(obj instanceof EntityExpr)) return false;

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityFunction.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityFunction.java?rev=706842&r1=706841&r2=706842&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityFunction.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityFunction.java Tue Oct 21 18:30:49 2008
@@ -178,10 +178,15 @@
     }
 
     public String getCode() {
-        if (codeString == null)
+        if (codeString == null) {
             return "null";
-        else
+        } else {
             return codeString;
+        }
+    }
+    
+    public Object getOriginalValue() {
+        return this.value;
     }
 
     public int getId() {