cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From and...@apache.org
Subject svn commit: r734464 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/map/ main/java/org/apache/cayenne/util/ test/java/org/apache/cayenne/util/
Date Wed, 14 Jan 2009 18:13:43 GMT
Author: andrey
Date: Wed Jan 14 10:13:42 2009
New Revision: 734464

URL: http://svn.apache.org/viewvc?rev=734464&view=rev
Log:
CAY-1160 Default delete rule is not used when migrating schema (to model)

Moving default delete rules to cayenne-unpublished, since the have to be used in synchronizing

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/DeleteRuleUpdater.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/EntityMergeSupportTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DeleteRule.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DeleteRule.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DeleteRule.java?rev=734464&r1=734463&r2=734464&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DeleteRule.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DeleteRule.java
Wed Jan 14 10:13:42 2009
@@ -45,6 +45,18 @@
      */
     public static final int DENY = 3;
     private static final String DENY_NAME = "Deny";
+    
+    /**
+     * Default delete rule for one-to-many relationships. It is used when new rels are
+     * created via modeler, or when synchrozining Obj- and DbEntities
+     */
+    public static final int DEFAULT_DELETE_RULE_TO_MANY = DeleteRule.DENY;
+    
+    /**
+     * Default delete rule for many-to-one relationships. It is used when new rels are
+     * created via modeler, or when synchrozining Obj- and DbEntities
+     */
+    public static final int DEFAULT_DELETE_RULE_TO_ONE = DeleteRule.NULLIFY;
 
     /** 
      * Returns String label for a delete rule state. Used for save/load (xml),

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/DeleteRuleUpdater.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/DeleteRuleUpdater.java?rev=734464&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/DeleteRuleUpdater.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/DeleteRuleUpdater.java
Wed Jan 14 10:13:42 2009
@@ -0,0 +1,83 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.util;
+
+import java.util.Collection;
+
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.DeleteRule;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.ObjRelationship;
+
+/**
+ * DeleteRuleUpdater is responsible for auto-setting delete rules for object relationships
+ */
+public class DeleteRuleUpdater implements EntityMergeListener {
+    /**
+     * Singleton object to for defining EntityMergeListener instance
+     */
+    private static DeleteRuleUpdater instance;
+    
+    /**
+     * Updates delete rules for all obj entities in a datamap
+     */
+    public static void updateDataMap(DataMap map) {
+        Collection<ObjEntity> entities = map.getObjEntities();
+        for (ObjEntity ent : entities) {
+            updateObjEntity(ent);
+        }
+    }
+    
+    /**
+     * Updates delete rules for all relationships in a objentity
+     */
+    public static void updateObjEntity(ObjEntity e) {
+        Collection<ObjRelationship> rels = e.getRelationships();
+        for (ObjRelationship rel : rels) {
+            updateObjRelationship(rel);
+        }
+    }
+    
+    /**
+     * Updates delete rules for specified relationship
+     */
+    public static void updateObjRelationship(ObjRelationship rel) {
+        rel.setDeleteRule(rel.isToMany() ? DeleteRule.DEFAULT_DELETE_RULE_TO_MANY :
+            DeleteRule.DEFAULT_DELETE_RULE_TO_ONE);
+    }
+
+    public void objAttributeAdded(ObjAttribute attr) {
+    }
+
+    public void objRelationshipAdded(ObjRelationship rel) {
+        updateObjRelationship(rel);
+    }
+    
+    /**
+     * Returns EntityMergeListener instance, which can set delete rule at relationship change
+     */
+    public static EntityMergeListener getEntityMergeListener() {
+        if (instance == null) {
+            instance = new DeleteRuleUpdater();
+        }
+        
+        return instance;
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java?rev=734464&r1=734463&r2=734464&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
Wed Jan 14 10:13:42 2009
@@ -67,6 +67,11 @@
         this.listeners = new ArrayList<EntityMergeListener>(); 
         
         this.namingStrategy = namingStrategy;
+        
+        /**
+         * Adding a listener, so that all created ObjRelationships would have default delete
rule
+         */
+        addEntityMergeListener(DeleteRuleUpdater.getEntityMergeListener());
     }
 
     /**

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/EntityMergeSupportTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/EntityMergeSupportTest.java?rev=734464&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/EntityMergeSupportTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/EntityMergeSupportTest.java
Wed Jan 14 10:13:42 2009
@@ -0,0 +1,94 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.util;
+
+import java.sql.Types;
+import java.util.Arrays;
+
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbJoin;
+import org.apache.cayenne.map.DbRelationship;
+import org.apache.cayenne.map.DeleteRule;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.ObjRelationship;
+import org.apache.cayenne.merge.MergeCase;
+
+public class EntityMergeSupportTest extends MergeCase {
+     public void testMerging() {
+         DbEntity dbEntity1 = new DbEntity("NEW_TABLE");
+
+         DbAttribute e1col1 = new DbAttribute("ID", Types.INTEGER, dbEntity1);
+         e1col1.setMandatory(true);
+         e1col1.setPrimaryKey(true);
+         dbEntity1.addAttribute(e1col1);
+
+         DbAttribute e1col2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity1);
+         e1col2.setMaxLength(10);
+         e1col2.setMandatory(false);
+         dbEntity1.addAttribute(e1col2);
+
+         map.addDbEntity(dbEntity1);
+
+         DbEntity dbEntity2 = new DbEntity("NEW_TABLE2");
+         DbAttribute e2col1 = new DbAttribute("ID", Types.INTEGER, dbEntity2);
+         e2col1.setMandatory(true);
+         e2col1.setPrimaryKey(true);
+         dbEntity2.addAttribute(e2col1);
+         DbAttribute e2col2 = new DbAttribute("FK", Types.INTEGER, dbEntity2);
+         dbEntity2.addAttribute(e2col2);
+
+         map.addDbEntity(dbEntity2);
+
+         // create db relationships
+         DbRelationship rel1To2 = new DbRelationship("rel1To2");
+         rel1To2.setSourceEntity(dbEntity1);
+         rel1To2.setTargetEntity(dbEntity2);
+         rel1To2.setToMany(true);
+         rel1To2.addJoin(new DbJoin(rel1To2, e1col1.getName(), e2col2.getName()));
+         dbEntity1.addRelationship(rel1To2);
+         DbRelationship rel2To1 = new DbRelationship("rel2To1");
+         rel2To1.setSourceEntity(dbEntity2);
+         rel2To1.setTargetEntity(dbEntity1);
+         rel2To1.setToMany(false);
+         rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
+         dbEntity2.addRelationship(rel2To1);
+         assertSame(rel1To2, rel2To1.getReverseRelationship());
+         assertSame(rel2To1, rel1To2.getReverseRelationship());
+         
+         ObjEntity objEntity1 = new ObjEntity("NewTable");
+         objEntity1.setDbEntity(dbEntity1);
+         map.addObjEntity(objEntity1);
+         
+         ObjEntity objEntity2 = new ObjEntity("NewTable2");
+         objEntity2.setDbEntity(dbEntity2);
+         map.addObjEntity(objEntity2);
+         
+         assertTrue(new EntityMergeSupport(map).synchronizeWithDbEntities(
+                 Arrays.asList(objEntity1, objEntity2)));
+         assertNotNull(objEntity1.getAttribute("name"));
+         assertNotNull(objEntity1.getRelationship("rel1To2"));
+         assertNotNull(objEntity2.getRelationship("rel2To1"));
+         
+         assertEquals(((ObjRelationship) objEntity1.getRelationship("rel1To2")).getDeleteRule(),
+                 DeleteRule.DEFAULT_DELETE_RULE_TO_MANY);
+         assertEquals(((ObjRelationship) objEntity2.getRelationship("rel2To1")).getDeleteRule(),
+                 DeleteRule.DEFAULT_DELETE_RULE_TO_ONE);
+     }
+}



Mime
View raw message