svn commit: r1481518 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model: ModelKeyMap.java ModelRelation.java

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

svn commit: r1481518 - in /ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model: ModelKeyMap.java ModelRelation.java

adrianc
Author: adrianc
Date: Sun May 12 10:52:55 2013
New Revision: 1481518

URL: http://svn.apache.org/r1481518
Log:
Improvements to the ModelRelation and ModelKeyMap classes. Now the entity engine can accurately detect duplicate relations.

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelKeyMap.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelRelation.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelKeyMap.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelKeyMap.java?rev=1481518&r1=1481517&r2=1481518&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelKeyMap.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelKeyMap.java Sun May 12 10:52:55 2013
@@ -18,14 +18,14 @@
  *******************************************************************************/
 package org.ofbiz.entity.model;
 
+import java.io.Serializable;
 import java.util.List;
 
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-
 import org.ofbiz.base.lang.ThreadSafe;
 import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.base.util.UtilXml;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 
 /**
@@ -34,7 +34,7 @@ import org.ofbiz.base.util.UtilXml;
  */
 @ThreadSafe
 @SuppressWarnings("serial")
-public final class ModelKeyMap implements java.io.Serializable {
+public final class ModelKeyMap implements Comparable<ModelKeyMap>, Serializable {
 
     /*
      * Developers - this is an immutable class. Once constructed, the object should not change state.
@@ -48,10 +48,14 @@ public final class ModelKeyMap implement
     /** name of the field in related entity */
     private final String relFieldName;
 
+    /** Full name of the key map (fieldName:relFieldName) */
+    private final String fullName;
+
     /** Data Constructor, if relFieldName is null defaults to fieldName */
     public ModelKeyMap(String fieldName, String relFieldName) {
         this.fieldName = fieldName;
         this.relFieldName = UtilXml.checkEmpty(relFieldName, this.fieldName);
+        this.fullName = this.fieldName.concat(":").concat(this.relFieldName);
     }
 
     /** XML Constructor */
@@ -59,6 +63,7 @@ public final class ModelKeyMap implement
         this.fieldName = UtilXml.checkEmpty(keyMapElement.getAttribute("field-name")).intern();
         // if no relFieldName is specified, use the fieldName; this is convenient for when they are named the same, which is often the case
         this.relFieldName = UtilXml.checkEmpty(keyMapElement.getAttribute("rel-field-name"), this.fieldName).intern();
+        this.fullName = this.fieldName.concat(":").concat(this.relFieldName);
     }
 
     /** Returns the field name. */
@@ -87,18 +92,19 @@ public final class ModelKeyMap implement
 
     @Override
     public int hashCode() {
-        return this.fieldName.hashCode() + this.relFieldName.hashCode();
+        return this.fullName.hashCode();
     }
 
     @Override
     public boolean equals(Object other) {
-        if (!(other instanceof ModelKeyMap)) return false;
-        ModelKeyMap otherKeyMap = (ModelKeyMap) other;
-
-        if (!otherKeyMap.fieldName.equals(this.fieldName)) return false;
-        if (!otherKeyMap.relFieldName.equals(this.relFieldName)) return false;
-
-        return true;
+        if (this == other) {
+            return true;
+        }
+        if (other instanceof ModelKeyMap) {
+            ModelKeyMap otherKeyMap = (ModelKeyMap) other;
+            return this.fullName.equals(otherKeyMap.fullName);
+        }
+        return false;
     }
 
     // TODO: Externalize this.
@@ -110,4 +116,14 @@ public final class ModelKeyMap implement
         }
         return root;
     }
+
+    @Override
+    public int compareTo(ModelKeyMap other) {
+        return this.fullName.compareTo(other.fullName);
+    }
+
+    @Override
+    public String toString() {
+        return this.fullName;
+    }
 }

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelRelation.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelRelation.java?rev=1481518&r1=1481517&r2=1481518&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelRelation.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelRelation.java Sun May 12 10:52:55 2013
@@ -22,6 +22,8 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
 
 import org.ofbiz.base.lang.ThreadSafe;
 import org.ofbiz.base.util.UtilValidate;
@@ -137,7 +139,17 @@ public final class ModelRelation extends
         this.keyMaps = keyMaps;
         this.isAutoRelation = isAutoRelation;
         StringBuilder sb = new StringBuilder();
-        sb.append(modelEntity).append("->").append(title).append(relEntityName);
+        sb.append(modelEntity == null ? "Unknown" : modelEntity.getEntityName()).append("->").append(title).append(relEntityName).append("[");
+        Set<ModelKeyMap> keyMapSet = new TreeSet<ModelKeyMap>(keyMaps);
+        Iterator<ModelKeyMap> setIter = keyMapSet.iterator();
+        while (setIter.hasNext()) {
+            ModelKeyMap keyMap = setIter.next();
+            sb.append(keyMap);
+            if (setIter.hasNext()) {
+                sb.append(",");
+            }
+        }
+        sb.append("]");
         this.fullName = sb.toString();
         this.combinedName = title.concat(relEntityName);
     }
@@ -207,6 +219,11 @@ public final class ModelRelation extends
     }
 
     @Override
+    public int hashCode() {
+        return this.fullName.hashCode();
+    }
+
+    @Override
     public String toString() {
         return this.fullName;
     }