Author: andrey
Date: Sat May 16 21:33:48 2009
New Revision: 775538

URL: http://svn.apache.org/viewvc?rev=775538&view=rev
Log:
CAY-1205 Renaming DbRelationships breaks different ObjRelationships

Modified:
    
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DbEntity.java
    
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjRelationship.java
    
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjRelationshipTest.java

Modified: 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DbEntity.java
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DbEntity.java?rev=775538&r1=775537&r2=775538&view=diff
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DbEntity.java
 (original)
+++ 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DbEntity.java
 Sat May 16 21:33:48 2009
@@ -165,6 +165,7 @@
     /**
      * @deprecated since 3.0 use {...@link #getPrimaryKeys()} that returns a 
collection.
      */
+    @Deprecated
     public List<DbAttribute> getPrimaryKey() {
         return new ArrayList<DbAttribute>(getPrimaryKeys());
     }
@@ -525,22 +526,6 @@
         // handle relationship name changes
         if (e.getId() == RelationshipEvent.CHANGE && e.isNameChange()) {
             String oldName = e.getOldName();
-            String newName = e.getNewName();
-
-            DataMap map = getDataMap();
-            if (map != null) {
-                // finds all object entities with a db relationship path to 
the renamed
-                // relationship
-                for (ObjEntity oe : map.getObjEntities()) {
-                    for (Relationship relationship : oe.getRelationships()) {
-                        ObjRelationship or = (ObjRelationship) relationship;
-                        // rename the db relationship path with the new name
-                        if (Util.nullSafeEquals(or.getDbRelationshipPath(), 
oldName)) {
-                            or.setDbRelationshipPath(newName);
-                        }
-                    }
-                }
-            }
 
             // clear the relationship out of the collection
             relationships.remove(oldName);

Modified: 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjRelationship.java
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjRelationship.java?rev=775538&r1=775537&r2=775538&view=diff
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjRelationship.java
 (original)
+++ 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjRelationship.java
 Sat May 16 21:33:48 2009
@@ -49,11 +49,9 @@
     public static final String DEFAULT_COLLECTION_TYPE = "java.util.List";
 
     boolean readOnly;
-    boolean dbRelationshipsRefreshNeeded = true;
 
     protected int deleteRule = DeleteRule.NO_ACTION;
     protected boolean usedForLocking;
-    protected String dbRelationshipPath;
 
     protected List<DbRelationship> dbRelationships = new 
ArrayList<DbRelationship>(2);
 
@@ -228,7 +226,6 @@
      * Returns an immutable list of underlying DbRelationships.
      */
     public List<DbRelationship> getDbRelationships() {
-        refreshFromPath(true);
         return Collections.unmodifiableList(dbRelationships);
     }
 
@@ -236,8 +233,6 @@
      * Appends a DbRelationship to the existing list of DbRelationships.
      */
     public void addDbRelationship(DbRelationship dbRel) {
-        refreshFromPath(true);
-
         if (dbRel.getName() == null) {
             throw new IllegalArgumentException("DbRelationship has no name");
         }
@@ -261,13 +256,6 @@
 
         dbRelationships.add(dbRel);
 
-        if (dbRelationshipPath == null) {
-            dbRelationshipPath = dbRel.getName();
-        }
-        else {
-            dbRelationshipPath += '.' + dbRel.getName();
-        }
-
         this.recalculateReadOnlyValue();
         this.recalculateToManyValue();
     }
@@ -276,36 +264,13 @@
      * Removes the relationship <code>dbRel</code> from the list of 
relationships.
      */
     public void removeDbRelationship(DbRelationship dbRel) {
-        refreshFromPath(true);
-
         if (dbRelationships.remove(dbRel)) {
-
-            if (dbRelationships.isEmpty()) {
-                dbRelationshipPath = null;
-            }
-            else {
-                StringBuilder path = new StringBuilder();
-
-                for (int i = 0; i < dbRelationships.size(); i++) {
-                    DbRelationship r = dbRelationships.get(i);
-                    if (i > 0) {
-                        path.append('.');
-                    }
-
-                    path.append(r.getName());
-                }
-
-                dbRelationshipPath = path.toString();
-            }
-
             this.recalculateReadOnlyValue();
             this.recalculateToManyValue();
         }
     }
 
     public void clearDbRelationships() {
-        this.dbRelationshipPath = null;
-        this.dbRelationshipsRefreshNeeded = false;
         this.dbRelationships.clear();
         this.readOnly = false;
         this.toMany = false;
@@ -361,13 +326,13 @@
      * @return flag indicating if the relationship is read only or not
      */
     public boolean isReadOnly() {
-        refreshFromPath(true);
+        recalculateReadOnlyValue();
         return readOnly;
     }
 
     @Override
     public boolean isToMany() {
-        refreshFromPath(true);
+        recalculateToManyValue();
         return super.isToMany();
     }
 
@@ -408,6 +373,7 @@
     /**
      * @deprecated since 3.0 as ObjRelationship no longer reacts to 
DbRelationship events.
      */
+    @Deprecated
     public void dbRelationshipDidChange(RelationshipEvent event) {
         recalculateToManyValue();
         recalculateReadOnlyValue();
@@ -437,27 +403,22 @@
      * @since 1.1
      */
     public String getDbRelationshipPath() {
-        if (dbRelationshipsRefreshNeeded) {
-            return dbRelationshipPath;
+        // build path on the fly
+        if (getDbRelationships().isEmpty()) {
+            return null;
         }
-        else {
-            // build path on the fly
-            if (getDbRelationships().isEmpty()) {
-                return null;
-            }
 
-            StringBuilder path = new StringBuilder();
-            Iterator<DbRelationship> it = getDbRelationships().iterator();
-            while (it.hasNext()) {
-                DbRelationship next = it.next();
-                path.append(next.getName());
-                if (it.hasNext()) {
-                    path.append(Entity.PATH_SEPARATOR);
-                }
+        StringBuilder path = new StringBuilder();
+        Iterator<DbRelationship> it = getDbRelationships().iterator();
+        while (it.hasNext()) {
+            DbRelationship next = it.next();
+            path.append(next.getName());
+            if (it.hasNext()) {
+                path.append(Entity.PATH_SEPARATOR);
             }
-
-            return path.toString();
         }
+
+        return path.toString();
     }
 
     /**
@@ -502,9 +463,8 @@
      * Sets mapped DbRelationships as a dot-separated path.
      */
     public void setDbRelationshipPath(String relationshipPath) {
-        if (!Util.nullSafeEquals(this.dbRelationshipPath, relationshipPath)) {
-            this.dbRelationshipPath = relationshipPath;
-            this.dbRelationshipsRefreshNeeded = true;
+        if (!Util.nullSafeEquals(getDbRelationshipPath(), relationshipPath)) {
+            refreshFromPath(relationshipPath);
         }
     }
 
@@ -551,21 +511,13 @@
     /**
      * Rebuild a list of relationships if String relationshipPath has changed.
      */
-    final void refreshFromPath(boolean stripInvalid) {
-        if (!dbRelationshipsRefreshNeeded) {
-            return;
-        }
-
+    final void refreshFromPath(String dbRelationshipPath) {
         synchronized (this) {
-            // check flag again in the synced block...
-            if (!dbRelationshipsRefreshNeeded) {
-                return;
-            }
 
             // remove existing relationships
             dbRelationships.clear();
 
-            if (this.dbRelationshipPath != null) {
+            if (dbRelationshipPath != null) {
 
                 ObjEntity entity = (ObjEntity) getSourceEntity();
                 if (entity == null) {
@@ -576,7 +528,7 @@
                 try {
                     // add new relationships from path
                     Iterator<CayenneMapEntry> it = entity
-                            .resolvePathComponents(new 
ASTDbPath(this.dbRelationshipPath));
+                            .resolvePathComponents(new 
ASTDbPath(dbRelationshipPath));
 
                     while (it.hasNext()) {
                         DbRelationship relationship = (DbRelationship) 
it.next();
@@ -585,16 +537,12 @@
                     }
                 }
                 catch (ExpressionException ex) {
-                    if (!stripInvalid) {
-                        throw ex;
-                    }
+                    throw ex;
                 }
             }
 
             recalculateToManyValue();
             recalculateReadOnlyValue();
-
-            dbRelationshipsRefreshNeeded = false;
         }
     }
 
@@ -668,7 +616,7 @@
     public String toString() {
         return new ToStringBuilder(this).append("name", getName()).append(
                 "dbRelationshipPath",
-                dbRelationshipPath).toString();
+                getDbRelationshipPath()).toString();
     }
 
     /**

Modified: 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjRelationshipTest.java
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjRelationshipTest.java?rev=775538&r1=775537&r2=775538&view=diff
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjRelationshipTest.java
 (original)
+++ 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjRelationshipTest.java
 Sat May 16 21:33:48 2009
@@ -71,8 +71,11 @@
     }
 
     public void testSerializability() throws Exception {
-        ObjRelationship r1 = new ObjRelationship("r1");
-        r1.setDbRelationshipPath("aaaa");
+        ObjEntity artistObjEnt = getObjEntity("Artist");
+
+        // start with "to many"
+        ObjRelationship r1 = (ObjRelationship) artistObjEnt
+                .getRelationship("paintingArray");
 
         ObjRelationship r2 = (ObjRelationship) Util.cloneViaSerialization(r1);
         assertEquals(r1.getName(), r2.getName());
@@ -115,25 +118,22 @@
     }
 
     public void testSetDbRelationshipPath() {
-        ObjRelationship relationship = new ObjRelationship();
-        relationship.dbRelationshipsRefreshNeeded = false;
-
-        relationship.setDbRelationshipPath("dummy.path");
-        assertTrue(relationship.dbRelationshipsRefreshNeeded);
+        ObjEntity artistObjEnt = getObjEntity("Artist");
 
-        assertEquals("dummy.path", relationship.getDbRelationshipPath());
-        assertTrue(relationship.dbRelationshipsRefreshNeeded);
+        ObjRelationship r = new ObjRelationship("r");
+        r.setSourceEntity(artistObjEnt);
+        r.setDbRelationshipPath("paintingArray");
+        assertEquals(r.getDbRelationshipPath(), "paintingArray");
     }
 
     public void testRefreshFromPath() {
         ObjRelationship relationship = new ObjRelationship();
-        relationship.setDbRelationshipPath("dummy.path");
 
         // attempt to resolve must fail - relationship is outside of context,
         // plus the path is random
         try {
-            relationship.refreshFromPath(false);
-            fail("refresh without source entity should have failed.");
+            relationship.setDbRelationshipPath("dummy.path");
+            fail("set random path should have failed.");
         }
         catch (CayenneRuntimeException ex) {
             // expected
@@ -147,7 +147,7 @@
         // attempt to resolve must fail - relationship is outside of context,
         // plus the path is random
         try {
-            relationship.refreshFromPath(false);
+            relationship.refreshFromPath("dummy.path");
             fail("refresh over a dummy path should have failed.");
         }
         catch (ExpressionException ex) {
@@ -171,8 +171,7 @@
         dbEntity1.addRelationship(dummyR);
         dbEntity2.addRelationship(pathR);
 
-        relationship.refreshFromPath(false);
-        assertFalse(relationship.dbRelationshipsRefreshNeeded);
+        relationship.refreshFromPath("dummy.path");
 
         List<DbRelationship> resolvedPath = relationship.getDbRelationships();
         assertEquals(2, resolvedPath.size());
@@ -207,7 +206,6 @@
 
         // test how toMany changes dependending on the underlying 
DbRelationships
         // add DbRelationships directly to avoid events to test 
"calculateToMany"
-        relationship.dbRelationshipsRefreshNeeded = false;
         relationship.dbRelationships.add(dummyR);
         assertFalse(relationship.isToMany());
 


Reply via email to