svn commit: r1476745 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity: GenericEntity.java GenericValue.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1476745 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity: GenericEntity.java GenericValue.java

adrianc
Author: adrianc
Date: Sun Apr 28 09:47:32 2013
New Revision: 1476745

URL: http://svn.apache.org/r1476745
Log:
Pull up some data and methods from GenericValue to GenericEntity. GenericValue is now a GenericEntity with some convenience methods.

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.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=1476745&r1=1476744&r2=1476745&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericEntity.java Sun Apr 28 09:47:32 2013
@@ -51,10 +51,13 @@ import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.base.util.UtilXml;
 import org.ofbiz.base.util.collections.LocalizedMap;
 import org.ofbiz.entity.condition.EntityCondition;
+import org.ofbiz.entity.condition.EntityFieldMap;
 import org.ofbiz.entity.jdbc.SqlJdbcUtil;
 import org.ofbiz.entity.model.ModelEntity;
 import org.ofbiz.entity.model.ModelField;
 import org.ofbiz.entity.model.ModelFieldType;
+import org.ofbiz.entity.model.ModelKeyMap;
+import org.ofbiz.entity.model.ModelRelation;
 import org.ofbiz.entity.model.ModelViewEntity;
 import org.ofbiz.entity.model.ModelViewEntity.ModelAlias;
 import org.w3c.dom.Document;
@@ -85,6 +88,10 @@ public class GenericEntity implements Ma
     /** Reference to an instance of GenericDelegator used to do some basic operations on this entity value. If null various methods in this class will fail. This is automatically set by the GenericDelegator for all GenericValue objects instantiated through it. You may set this manually for objects you instantiate manually, but it is optional. */
     private transient Delegator internalDelegator = null;
 
+    /** A Map containing the original field values from the database.
+     */
+    private Map<String, Object> originalDbValues = null;
+
     /** Contains the fields for this entity. Note that this should always be a
      *  HashMap to allow for two things: non-synchronized reads (synchronized
      *  writes are done through synchronized setters) and being able to store
@@ -237,6 +244,7 @@ public class GenericEntity implements Ma
         // from GenericEntity
         this.delegatorName = null;
         this.internalDelegator = null;
+        this.originalDbValues = null;
         this.fields = new HashMap<String, Object>();
         this.entityName = null;
         this.modelEntity = null;
@@ -279,6 +287,7 @@ public class GenericEntity implements Ma
      */
     public void synchronizedWithDatasource() {
         assertIsMutable();
+        this.originalDbValues = Collections.unmodifiableMap(getAllFields());
         this.clearChanged();
     }
 
@@ -1522,6 +1531,78 @@ public class GenericEntity implements Ma
         getObservable().setChanged();
     }
 
+    public boolean originalDbValuesAvailable() {
+        return this.originalDbValues != null ? true : false;
+    }
+
+    public Object getOriginalDbValue(String name) {
+        if (getModelEntity().getField(name) == null) {
+            throw new IllegalArgumentException("[GenericEntity.get] \"" + name + "\" is not a field of " + getEntityName());
+        }
+        if (originalDbValues == null) return null;
+        return originalDbValues.get(name);
+    }
+
+    /**
+     * Checks to see if all foreign key records exist in the database. Will create a dummy value for
+     * those missing when specified.
+     *
+     * @param insertDummy Create a dummy record using the provided fields
+     * @return true if all FKs exist (or when all missing are created)
+     * @throws GenericEntityException
+     */
+    public boolean checkFks(boolean insertDummy) throws GenericEntityException {
+        ModelEntity model = this.getModelEntity();
+        Iterator<ModelRelation> relItr = model.getRelationsIterator();
+        while (relItr.hasNext()) {
+            ModelRelation relation = relItr.next();
+            if ("one".equalsIgnoreCase(relation.getType())) {
+                // see if the related value exists
+                Map<String, Object> fields = new HashMap<String, Object>();
+                for (int i = 0; i < relation.getKeyMapsSize(); i++) {
+                    ModelKeyMap keyMap = relation.getKeyMap(i);
+                    fields.put(keyMap.getRelFieldName(), this.get(keyMap.getFieldName()));
+                }
+                EntityFieldMap ecl = EntityCondition.makeCondition(fields);
+                long count = this.getDelegator().findCountByCondition(relation.getRelEntityName(), ecl, null, null);
+                if (count == 0) {
+                    if (insertDummy) {
+                        // create the new related value (dummy)
+                        GenericValue newValue = this.getDelegator().makeValue(relation.getRelEntityName());
+                        Iterator<ModelKeyMap> keyMapIter = relation.getKeyMapsIterator();
+                        boolean allFieldsSet = true;
+                        while (keyMapIter.hasNext()) {
+                            ModelKeyMap mkm = keyMapIter.next();
+                            if (this.get(mkm.getFieldName()) != null) {
+                                newValue.set(mkm.getRelFieldName(), this.get(mkm.getFieldName()));
+                                if (Debug.infoOn()) Debug.logInfo("Set [" + mkm.getRelFieldName() + "] to - " + this.get(mkm.getFieldName()), module);
+                            } else {
+                                allFieldsSet = false;
+                            }
+                        }
+                        if (allFieldsSet) {
+                            if (Debug.infoOn()) Debug.logInfo("Creating place holder value : " + newValue, module);
+
+                            // inherit create and update times from this value in order to make this not seem like new/fresh data
+                            newValue.put(ModelEntity.CREATE_STAMP_FIELD, this.get(ModelEntity.CREATE_STAMP_FIELD));
+                            newValue.put(ModelEntity.CREATE_STAMP_TX_FIELD, this.get(ModelEntity.CREATE_STAMP_TX_FIELD));
+                            newValue.put(ModelEntity.STAMP_FIELD, this.get(ModelEntity.STAMP_FIELD));
+                            newValue.put(ModelEntity.STAMP_TX_FIELD, this.get(ModelEntity.STAMP_TX_FIELD));
+                            // set isFromEntitySync so that create/update stamp fields set above will be preserved
+                            newValue.setIsFromEntitySync(true);
+                            // check the FKs for the newly created entity
+                            newValue.checkFks(true);
+                            newValue.create();
+                        }
+                    } else {
+                        return false;
+                    }
+                }
+            }
+        }
+        return true;
+    }
+
     public static interface NULL {
     }
 

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java?rev=1476745&r1=1476744&r2=1476745&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java Sun Apr 28 09:47:32 2013
@@ -19,20 +19,10 @@
 
 package org.ofbiz.entity;
 
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.entity.condition.EntityCondition;
-import org.ofbiz.entity.condition.EntityFieldMap;
 import org.ofbiz.entity.model.ModelEntity;
-import org.ofbiz.entity.model.ModelKeyMap;
-import org.ofbiz.entity.model.ModelRelation;
-
 
 /**
  * Generic Entity Value Object - Handles persistence for any defined entity.
@@ -43,10 +33,6 @@ public class GenericValue extends Generi
 
     public static final GenericValue NULL_VALUE = new NullGenericValue();
 
-    /** A Map containing the original field values from the database.
-     */
-    private Map<String, Object> originalDbValues = null;
-
     /** Creates new GenericValue */
     public static GenericValue create(ModelEntity modelEntity) {
         GenericValue newValue = new GenericValue();
@@ -82,18 +68,6 @@ public class GenericValue extends Generi
         return newValue;
     }
 
-    @Override
-    public void reset() {
-        super.reset();
-        this.originalDbValues = null;
-    }
-
-    @Override
-    public void synchronizedWithDatasource() {
-        super.synchronizedWithDatasource();
-        this.originalDbValues = Collections.unmodifiableMap(getAllFields());
-    }
-
     public GenericValue create() throws GenericEntityException {
         return this.getDelegator().create(this);
     }
@@ -114,18 +88,6 @@ public class GenericValue extends Generi
         this.getDelegator().refreshFromCache(this);
     }
 
-    public boolean originalDbValuesAvailable() {
-        return this.originalDbValues != null ? true : false;
-    }
-
-    public Object getOriginalDbValue(String name) {
-        if (getModelEntity().getField(name) == null) {
-            throw new IllegalArgumentException("[GenericEntity.get] \"" + name + "\" is not a field of " + getEntityName());
-        }
-        if (originalDbValues == null) return null;
-        return originalDbValues.get(name);
-    }
-
     /** Get the named Related Entity for the GenericValue from the persistent store
      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
      *@return List of GenericValue instances as specified in the relation definition
@@ -145,7 +107,7 @@ public class GenericValue extends Generi
      */
     @Deprecated
     public List<GenericValue> getRelated(String relationName, List<String> orderBy) throws GenericEntityException {
-        return this.getDelegator().getRelated(relationName, new HashMap<String, Object>(), orderBy, this, false);
+        return this.getDelegator().getRelated(relationName, null, orderBy, this, false);
     }
 
     /** Get the named Related Entity for the GenericValue from the persistent store
@@ -343,66 +305,6 @@ public class GenericValue extends Generi
         return this.getDelegator().getRelatedDummyPK(relationName, byAndFields, this);
     }
 
-    /**
-     * Checks to see if all foreign key records exist in the database. Will create a dummy value for
-     * those missing when specified.
-     *
-     * @param insertDummy Create a dummy record using the provided fields
-     * @return true if all FKs exist (or when all missing are created)
-     * @throws GenericEntityException
-     */
-    public boolean checkFks(boolean insertDummy) throws GenericEntityException {
-        ModelEntity model = this.getModelEntity();
-        Iterator<ModelRelation> relItr = model.getRelationsIterator();
-        while (relItr.hasNext()) {
-            ModelRelation relation = relItr.next();
-            if ("one".equalsIgnoreCase(relation.getType())) {
-                // see if the related value exists
-                Map<String, Object> fields = new HashMap<String, Object>();
-                for (int i = 0; i < relation.getKeyMapsSize(); i++) {
-                    ModelKeyMap keyMap = relation.getKeyMap(i);
-                    fields.put(keyMap.getRelFieldName(), this.get(keyMap.getFieldName()));
-                }
-                EntityFieldMap ecl = EntityCondition.makeCondition(fields);
-                long count = this.getDelegator().findCountByCondition(relation.getRelEntityName(), ecl, null, null);
-                if (count == 0) {
-                    if (insertDummy) {
-                        // create the new related value (dummy)
-                        GenericValue newValue = this.getDelegator().makeValue(relation.getRelEntityName());
-                        Iterator<ModelKeyMap> keyMapIter = relation.getKeyMapsIterator();
-                        boolean allFieldsSet = true;
-                        while (keyMapIter.hasNext()) {
-                            ModelKeyMap mkm = keyMapIter.next();
-                            if (this.get(mkm.getFieldName()) != null) {
-                                newValue.set(mkm.getRelFieldName(), this.get(mkm.getFieldName()));
-                                if (Debug.infoOn()) Debug.logInfo("Set [" + mkm.getRelFieldName() + "] to - " + this.get(mkm.getFieldName()), module);
-                            } else {
-                                allFieldsSet = false;
-                            }
-                        }
-                        if (allFieldsSet) {
-                            if (Debug.infoOn()) Debug.logInfo("Creating place holder value : " + newValue, module);
-
-                            // inherit create and update times from this value in order to make this not seem like new/fresh data
-                            newValue.put(ModelEntity.CREATE_STAMP_FIELD, this.get(ModelEntity.CREATE_STAMP_FIELD));
-                            newValue.put(ModelEntity.CREATE_STAMP_TX_FIELD, this.get(ModelEntity.CREATE_STAMP_TX_FIELD));
-                            newValue.put(ModelEntity.STAMP_FIELD, this.get(ModelEntity.STAMP_FIELD));
-                            newValue.put(ModelEntity.STAMP_TX_FIELD, this.get(ModelEntity.STAMP_TX_FIELD));
-                            // set isFromEntitySync so that create/update stamp fields set above will be preserved
-                            newValue.setIsFromEntitySync(true);
-                            // check the FKs for the newly created entity
-                            newValue.checkFks(true);
-                            newValue.create();
-                        }
-                    } else {
-                        return false;
-                    }
-                }
-            }
-        }
-        return true;
-    }
-
     /** Clones this GenericValue, this is a shallow clone & uses the default shallow HashMap clone
      *@return Object that is a clone of this GenericValue
      */