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/14 22:34:25 UTC

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

Author: mgentry
Date: Fri Jun 14 20:34:24 2013
New Revision: 1493237

URL: http://svn.apache.org/r1493237
Log:
More fixes for CAY-1813: Missing ObjEntity Attribute Validation with Duplicate DbEntity Columns.  Refactored some of the logic to be more user-friendly.  More to come.

Modified:
    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/ObjEntityValidator.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/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=1493237&r1=1493236&r2=1493237&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 Fri Jun 14 20:34:24 2013
@@ -99,7 +99,7 @@ public class ObjAttributeValidator exten
 
                 Iterator<EmbeddableAttribute> it = embAttributes.iterator();
                 while (it.hasNext()) {
-                    EmbeddableAttribute embAttr = (EmbeddableAttribute) it.next();
+                    EmbeddableAttribute embAttr = it.next();
                     String dbAttributeName;
                     if (attrOverrides.size() > 0
                             && attrOverrides.containsKey(embAttr.getName())) {
@@ -123,7 +123,6 @@ public class ObjAttributeValidator exten
                     }
                 }
             }
-
         }
         else if (attribute.getDbAttribute() == null) {
             validator.registerWarning("ObjAttribute has no DbAttribute mapping.", path);
@@ -136,5 +135,26 @@ public class ObjAttributeValidator exten
                     + attribute.getDbAttributeName(), path);
         }
 
+        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) {
+            ObjEntity entity = (ObjEntity) attribute.getEntity();
+
+            for (ObjAttribute comparisonAttribute : entity.getAttributes()) {
+                if (attribute != comparisonAttribute) {
+                    String dbAttributeName = attribute.getDbAttributeName();
+
+                    if (dbAttributeName != null) {
+                        if (dbAttributeName.equals(comparisonAttribute.getDbAttributeName()))
+                            validator.registerWarning("ObjEntity " + entity.getName() +
+                                                      " contains duplicate DbAttribute mapping (" +
+                                                      attribute.getName() + " -> " + dbAttributeName + ")", path);
+                        return;
+                    }
+                }
+            }
+        }
     }
 }

Modified: cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjEntityValidator.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/ObjEntityValidator.java?rev=1493237&r1=1493236&r2=1493237&view=diff
==============================================================================
--- cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjEntityValidator.java (original)
+++ cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/project/validator/ObjEntityValidator.java Fri Jun 14 20:34:24 2013
@@ -19,16 +19,9 @@
 
 package org.apache.cayenne.project.validator;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import org.apache.cayenne.access.DataDomain;
 import org.apache.cayenne.map.DataMap;
-import org.apache.cayenne.map.DbAttribute;
-import org.apache.cayenne.map.EmbeddedAttribute;
-import org.apache.cayenne.map.ObjAttribute;
 import org.apache.cayenne.map.ObjEntity;
-import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.project.ProjectPath;
 import org.apache.cayenne.util.Util;
 
@@ -49,8 +42,6 @@ public class ObjEntityValidator extends 
         validateName(ent, path, validator);
         validateClassName(ent, path, validator);
         validateSuperClassName(ent, path, validator);
-        validateAttributes(ent, path, validator);
-        validateRelationships(ent, path, validator);
 
         // validate DbEntity presence
         if (ent.getDbEntity() == null && !ent.isAbstract()) {
@@ -116,104 +107,6 @@ public class ObjEntityValidator extends 
         }
     }
 
-    private void validateAttributes(ObjEntity entity, ProjectPath path, Validator validator) {
-        // Map of dbAttributeName:objAttributeName.
-        Map<String,String> dbAttributes = new HashMap<String,String>();
-
-        for (ObjAttribute attribute : entity.getAttributes()) {
-            DbAttribute dbAttribute = attribute.getDbAttribute();
-
-            /*
-             * When a dbAttributeName has not been encountered, the
-             * dbAttributeName is added to the map along with the
-             * objAttributeName. This is the original occurrence.
-             *
-             * When a duplicate dbAttributeName is encountered, check to see if
-             * the objAttributeName is NOT null (indicating the first time the
-             * duplicate is encountered) and warn about the original
-             * dbAttributeName (which is a duplication of the current one) and
-             * set the original objAttributeName to null (indicating it has been
-             * processed), then warn about the duplicate, too.
-             */
-
-            // Embleddables do not have DB Attributes.
-            if (attribute instanceof EmbeddedAttribute) {
-                if (dbAttribute != null)
-                    validator.registerWarning("Embeddable (" + attribute.getName() + ") cannot have a DbAttribute mapping", path);
-            }
-            else if (dbAttribute == null) {
-                validator.registerWarning("Attribute (" + attribute.getName() + ") must have a DbAttribute mapping", path);
-            }
-            else {
-                String dbAttributeName = dbAttribute.getName();
-
-                if (Util.isEmptyString(dbAttributeName) == false) {
-                    // Be sure to add the original duplicate if not already processed:
-                    if (dbAttributes.containsKey(dbAttributeName)) {
-                        if (dbAttributes.get(dbAttributeName) != null) {
-                            addAttributeWarning(validator, entity.getName(), dbAttributes.get(dbAttributeName), dbAttributeName, path);
-                            dbAttributes.put(dbAttributeName, null);
-                        }
-
-                        // Add the current duplicate:
-                        addAttributeWarning(validator, entity.getName(), attribute.getName(), dbAttributeName, path);
-                    }
-                    else {
-                        // Add the original (not duplicated):
-                        dbAttributes.put(dbAttributeName, attribute.getName());
-                    }
-                }
-            }
-        }
-    }
-
-    private void validateRelationships(ObjEntity entity, ProjectPath path, Validator validator) {
-        // Map of relationshipPath:relationshipName.
-        Map<String,String> dbRelationshipPaths = new HashMap<String,String>();
-
-        for (ObjRelationship relationship : entity.getRelationships()) {
-            String dbRelationshipPath = relationship.getTargetEntityName() + "." + relationship.getDbRelationshipPath();
-
-            /*
-             * When a relationshipPath has not been encountered, the
-             * relationshipPath is added to the map along with the
-             * relationshipName. This is the original occurrence.
-             *
-             * When a duplicate relationshipPath is encountered, check to see if
-             * the relationshipName is NOT null (indicating the first time the
-             * duplicate is encountered) and warn about the original
-             * relationshipPath (which is a duplication of the current one) and
-             * set the original relationshipName to null (indicating it has been
-             * processed), then warn about the duplicate, too.
-             */
-
-            if (Util.isEmptyString(dbRelationshipPath) == false) {
-                if (dbRelationshipPaths.containsKey(dbRelationshipPath)) {
-                    // Be sure to add the original duplicate if not already processed:
-                    if (dbRelationshipPaths.get(dbRelationshipPath) != null) {
-                        addRelationshipWarning(validator, entity.getName(), dbRelationshipPaths.get(dbRelationshipPath), dbRelationshipPath, path);
-                        dbRelationshipPaths.put(dbRelationshipPath, null);
-                    }
-
-                    // Add the current duplicate:
-                    addRelationshipWarning(validator, entity.getName(), relationship.getName(), dbRelationshipPath, path);
-                }
-                else {
-                    // Add the original (not duplicated):
-                    dbRelationshipPaths.put(dbRelationshipPath, relationship.getName());
-                }
-            }
-        }
-    }
-
-    private void addAttributeWarning(Validator validator, String entityName, String objAttributeName, String dbAttributeName, ProjectPath path) {
-        validator.registerWarning("ObjEntity " + entityName + " contains duplicate DbAttribute mappings (" + objAttributeName + " -> " + dbAttributeName + ")", path);
-    }
-
-    private void addRelationshipWarning(Validator validator, String entityName, String relationshipName, String relationshipPath, ProjectPath path) {
-        validator.registerWarning("ObjEntity " + entityName + " contains duplicate ObjRelationship mappings (" + relationshipName + " -> " + relationshipPath + ")", path);
-    }
-
     protected void validateName(ObjEntity entity, ProjectPath path, Validator validator) {
         String name = entity.getName();
 

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=1493237&r1=1493236&r2=1493237&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 Fri Jun 14 20:34:24 2013
@@ -127,7 +127,7 @@ public class ObjRelationshipValidator ex
                         break;
                     }
                 }
-                
+
                 if (check) {
                     validator
                             .registerWarning(
@@ -138,6 +138,28 @@ 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();
+
+            if (dbRelationshipPath != null) {
+                for (ObjRelationship comparisonRelationship : entity.getRelationships()) {
+                    if (relationship != comparisonRelationship) {
+                        String comparisonDbRelationshipPath = comparisonRelationship.getTargetEntityName() + "." + comparisonRelationship.getDbRelationshipPath();
+
+                        if (dbRelationshipPath.equals(comparisonDbRelationshipPath)) {
+                            validator.registerWarning("ObjEntity " + entity.getName() + " contains duplicate ObjRelationship mappings (" + relationship.getName() + " -> " + dbRelationshipPath + ")", path);
+                            return;
+                        }
+                    }
+                }
+            }
+        }
     }
 
     public String objRelationshipIdentifier(ObjRelationship rel) {