You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by mg...@apache.org on 2013/06/17 18:40:55 UTC

svn commit: r1493839 - in /cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator: DbRelationshipValidator.java ObjAttributeValidator.java ObjRelationshipValidator.java

Author: mgentry
Date: Mon Jun 17 16:40:55 2013
New Revision: 1493839

URL: http://svn.apache.org/r1493839
Log:
More fixes for CAY-1813: Missing ObjEntity Attribute Validation with Duplicate DbEntity Columns.  Principally, adding a DbRelationship validator and cleanup.

Modified:
    cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/DbRelationshipValidator.java
    cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjAttributeValidator.java
    cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjRelationshipValidator.java

Modified: cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/DbRelationshipValidator.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/DbRelationshipValidator.java?rev=1493839&r1=1493838&r2=1493839&view=diff
==============================================================================
--- cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/DbRelationshipValidator.java (original)
+++ cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/DbRelationshipValidator.java Mon Jun 17 16:40:55 2013
@@ -19,8 +19,11 @@
 
 package org.apache.cayenne.project.validator;
 
-import java.util.Iterator;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 
+import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.DbJoin;
 import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.project.ProjectPath;
@@ -89,8 +92,73 @@ public class DbRelationshipValidator ext
                                 path);
             }
         }
+
+        checkForDuplicates(path, validator, rel);
+    }
+
+    private String getJoins(DbRelationship relationship) {
+        List<String> joins = new ArrayList<String>();
+
+        for (DbJoin join : relationship.getJoins()) {
+            StringBuilder builder = new StringBuilder();
+
+            builder.append("[");
+            builder.append("source=").append(join.getSourceName());
+            builder.append(",");
+            builder.append("target=").append(join.getTargetName());
+            builder.append("]");
+
+            joins.add(builder.toString());
+        }
+
+        Collections.sort(joins);
+
+        return Util.join(joins, ",");
     }
-    
+
+    /**
+     * Per CAY-1813, make sure two (or more) DbRelationships do not map to the
+     * same database path.
+     */
+    private void checkForDuplicates(ProjectPath    path,
+                                    Validator      validator,
+                                    DbRelationship relationship) {
+        if (relationship                       != null &&
+            relationship.getName()             != null &&
+            relationship.getTargetEntityName() != null) {
+
+            String dbRelationshipPath =
+                       relationship.getTargetEntityName() +
+                       "." +
+                       getJoins(relationship);
+
+            if (dbRelationshipPath != null) {
+                DbEntity entity = (DbEntity) relationship.getSourceEntity();
+
+                for (DbRelationship comparisonRelationship : entity.getRelationships()) {
+                    if (relationship != comparisonRelationship) {
+                        String comparisonDbRelationshipPath =
+                                   comparisonRelationship.getTargetEntityName() +
+                                   "." +
+                                   getJoins(comparisonRelationship);
+
+                        if (dbRelationshipPath.equals(comparisonDbRelationshipPath)) {
+                            validator.registerWarning
+                                ("DbEntity " +
+                                 entity.getName() +
+                                 " contains a duplicate DbRelationship mapping (" +
+                                 relationship.getName() +
+                                 " -> " +
+                                 dbRelationshipPath +
+                                 ")", path);
+                            return; // Duplicate found, stop.
+                        }
+                    }
+                }
+            }
+        }
+    }
+
     public String dbRelationshipIdentifier(DbRelationship rel)
     {
         if (null == rel.getSourceEntity())

Modified: cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjAttributeValidator.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjAttributeValidator.java?rev=1493839&r1=1493838&r2=1493839&view=diff
==============================================================================
--- cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjAttributeValidator.java (original)
+++ cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjAttributeValidator.java Mon Jun 17 16:40:55 2013
@@ -138,20 +138,34 @@ public class ObjAttributeValidator exten
         checkForDuplicates(path, validator, attribute);
     }
 
-    private void checkForDuplicates(ProjectPath path, Validator validator, ObjAttribute attribute) {
-        if (attribute != null && attribute.getName() != null && attribute.isInherited() == false && attribute.isFlattened() == false) {
+    /**
+     * Per CAY-1813, make sure two (or more) ObjAttributes do not map to the
+     * same database path.
+     */
+    private void checkForDuplicates(ProjectPath  path,
+                                    Validator    validator,
+                                    ObjAttribute attribute) {
+        if (attribute               != null &&
+            attribute.getName()     != null &&
+            attribute.isInherited() == false) {
+
             ObjEntity entity = (ObjEntity) attribute.getEntity();
 
             for (ObjAttribute comparisonAttribute : entity.getAttributes()) {
                 if (attribute != comparisonAttribute) {
-                    String dbAttributeName = attribute.getDbAttributeName();
+                    String dbAttributePath = attribute.getDbAttributePath();
 
-                    if (dbAttributeName != null) {
-                        if (dbAttributeName.equals(comparisonAttribute.getDbAttributeName()))
-                            validator.registerWarning("ObjEntity " + entity.getName() +
-                                                      " contains duplicate DbAttribute mapping (" +
-                                                      attribute.getName() + " -> " + dbAttributeName + ")", path);
-                        return;
+                    if (dbAttributePath != null) {
+                        if (dbAttributePath.equals(comparisonAttribute.getDbAttributePath()))
+                            validator.registerWarning
+                                ("ObjEntity " +
+                                 entity.getName() +
+                                 " contains a duplicate DbAttribute mapping (" +
+                                 attribute.getName() +
+                                 " -> " +
+                                 dbAttributePath +
+                                 ")", path);
+                        return; // Duplicate found, stop.
                     }
                 }
             }

Modified: cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjRelationshipValidator.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjRelationshipValidator.java?rev=1493839&r1=1493838&r2=1493839&view=diff
==============================================================================
--- cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjRelationshipValidator.java (original)
+++ cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjRelationshipValidator.java Mon Jun 17 16:40:55 2013
@@ -142,19 +142,42 @@ public class ObjRelationshipValidator ex
         checkForDuplicates(path, validator, rel);
     }
 
-    private void checkForDuplicates(ProjectPath path, Validator validator, ObjRelationship relationship) {
-        if (relationship != null && relationship.getName() != null && relationship.getTargetEntityName() != null && relationship.isFlattened() == false) {
-            ObjEntity entity             = (ObjEntity) relationship.getSourceEntity();
-            String    dbRelationshipPath = relationship.getTargetEntityName() + "." + relationship.getDbRelationshipPath();
+    /**
+     * Per CAY-1813, make sure two (or more) ObjRelationships do not map to the
+     * same database path.
+     */
+    private void checkForDuplicates(ProjectPath     path,
+                                    Validator       validator,
+                                    ObjRelationship relationship) {
+        if (relationship                       != null &&
+            relationship.getName()             != null &&
+            relationship.getTargetEntityName() != null) {
+
+            String dbRelationshipPath =
+                       relationship.getTargetEntityName() +
+                       "." +
+                       relationship.getDbRelationshipPath();
 
             if (dbRelationshipPath != null) {
+                ObjEntity entity = (ObjEntity) relationship.getSourceEntity();
+
                 for (ObjRelationship comparisonRelationship : entity.getRelationships()) {
                     if (relationship != comparisonRelationship) {
-                        String comparisonDbRelationshipPath = comparisonRelationship.getTargetEntityName() + "." + comparisonRelationship.getDbRelationshipPath();
+                        String comparisonDbRelationshipPath =
+                                   comparisonRelationship.getTargetEntityName() +
+                                   "." +
+                                   comparisonRelationship.getDbRelationshipPath();
 
                         if (dbRelationshipPath.equals(comparisonDbRelationshipPath)) {
-                            validator.registerWarning("ObjEntity " + entity.getName() + " contains duplicate ObjRelationship mappings (" + relationship.getName() + " -> " + dbRelationshipPath + ")", path);
-                            return;
+                            validator.registerWarning
+                                ("ObjEntity " +
+                                 entity.getName() +
+                                 " contains a duplicate ObjRelationship mapping (" +
+                                 relationship.getName() +
+                                 " -> " +
+                                 dbRelationshipPath +
+                                 ")", path);
+                            return; // Duplicate found, stop.
                         }
                     }
                 }