cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmen...@apache.org
Subject svn commit: r673241 - in /cayenne/main/trunk: docs/doc/src/main/resources/ framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/ framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ framework/cayenne-modeler/src/mai...
Date Wed, 02 Jul 2008 00:12:36 GMT
Author: kmenard
Date: Tue Jul  1 17:12:36 2008
New Revision: 673241

URL: http://svn.apache.org/viewvc?rev=673241&view=rev
Log:
CAY-436: In modeler, change default object relationship delete rule

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeListener.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DeleteRuleUpdater.java
Modified:
    cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ModelerPreferences.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/CreateObjEntityAction.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/CreateRelationshipAction.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/DbLoaderHelper.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/EntitySyncController.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoModel.java

Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=673241&r1=673240&r2=673241&view=diff
==============================================================================
--- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original)
+++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Tue Jul  1 17:12:36 2008
@@ -15,6 +15,7 @@
 
 Changes/New Features Since M4:
 
+CAY-436 In modeler, change default object relationship delete rule
 CAY-888 CM Usability: Object Select Query Improvements
 CAY-911 CM Usability: JComboBox Autocompletion
 CAY-1056 Detection of the project file changes on disk.

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeListener.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeListener.java?rev=673241&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeListener.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeListener.java
Tue Jul  1 17:12:36 2008
@@ -0,0 +1,40 @@
+/*****************************************************************
+ *   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 org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjRelationship;
+
+/**
+ * EntityMergeListener interface defines API useful for listening to
+ * EntityMergeSupport processing.
+ *  
+ * @author Andrey Razumovsky
+ */
+public interface EntityMergeListener {
+    /**
+     * Invoked when a missing attribute in ObjEntity is completed from DbEntity
+     */
+    public void objAttributeAdded(ObjAttribute attr);
+    
+    /**
+     * Invoked when a missing relationship in ObjEntity is completed from DbEntity
+     */
+    public void objRelationshipAdded(ObjRelationship rel);
+}

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=673241&r1=673240&r2=673241&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
Tue Jul  1 17:12:36 2008
@@ -19,23 +19,15 @@
 
 package org.apache.cayenne.util;
 
+import org.apache.cayenne.dba.TypesMapping;
+import org.apache.cayenne.map.*;
+import org.apache.cayenne.project.NamedObjectFactory;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 
-import org.apache.cayenne.dba.TypesMapping;
-import org.apache.cayenne.map.DataMap;
-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.Entity;
-import org.apache.cayenne.map.ObjAttribute;
-import org.apache.cayenne.map.ObjEntity;
-import org.apache.cayenne.map.ObjRelationship;
-import org.apache.cayenne.project.NamedObjectFactory;
-
 /**
  * Implements methods for entity merging.
  * 
@@ -45,10 +37,16 @@
 
     protected DataMap map;
     protected boolean removeMeaningfulFKs;
-
+    
+    /**
+     * Listeners of merge process. 
+     */
+    protected List<EntityMergeListener> listeners;
+    
     public EntityMergeSupport(DataMap map) {
         this.map = map;
         this.removeMeaningfulFKs = true;
+        this.listeners = new ArrayList<EntityMergeListener>(); 
     }
 
     /**
@@ -118,6 +116,8 @@
                 ObjAttribute oa = new ObjAttribute(attrName, type, entity);
                 oa.setDbAttributePath(da.getName());
                 entity.addAttribute(oa);
+                
+                fireAttributeAdded(oa);
                 changed = true;
             }
 
@@ -140,6 +140,8 @@
                     or.setSourceEntity(entity);
                     or.setTargetEntity(mappedTarget);
                     entity.addRelationship(or);
+                    
+                    fireRelationshipAdded(or);
                     changed = true;
                 }
             }
@@ -278,4 +280,43 @@
     public void setRemoveMeaningfulFKs(boolean removeMeaningfulFKs) {
         this.removeMeaningfulFKs = removeMeaningfulFKs;
     }
+    
+    /**
+     * Registers new EntityMergeListener
+     */
+    public void addEntityMergeListener(EntityMergeListener listener) {
+        listeners.add(listener);
+    }
+    
+    /**
+     * Unregisters an EntityMergeListener
+     */
+    public void removeEntityMergeListener(EntityMergeListener listener) {
+        listeners.remove(listener);
+    }
+    
+    /**
+     * Returns registered listeners
+     */
+    public EntityMergeListener[] getEntityMergeListeners() {
+        return listeners.toArray(new EntityMergeListener[0]);
+    }
+    
+    /**
+     * Notifies all listeners that an ObjAttribute was added
+     */
+    protected void fireAttributeAdded(ObjAttribute attr) {
+        for (int i = 0; i < listeners.size(); i++) {
+            listeners.get(i).objAttributeAdded(attr);
+        }
+    }
+    
+    /**
+     * Notifies all listeners that an ObjRelationship was added
+     */
+    protected void fireRelationshipAdded(ObjRelationship rel) {
+        for (int i = 0; i < listeners.size(); i++) {
+            listeners.get(i).objRelationshipAdded(rel);
+        }
+    }
 }

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ModelerPreferences.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ModelerPreferences.java?rev=673241&r1=673240&r2=673241&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ModelerPreferences.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ModelerPreferences.java
Tue Jul  1 17:12:36 2008
@@ -19,16 +19,17 @@
 
 package org.apache.cayenne.modeler;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
+import org.apache.cayenne.map.DeleteRule;
 import org.apache.cayenne.project.CayenneUserDir;
 import org.apache.commons.collections.ExtendedProperties;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
 /**
  * ModelerPreferences class supports persistent user preferences. Preferences are saved in
  * the user home directory in "<code>$HOME/.cayenne/modeler.preferences</code>"
file.
@@ -64,6 +65,18 @@
      * Number of items in combobox visible without scrolling 
      */
     public static final int COMBOBOX_MAX_VISIBLE_SIZE = 12;
+    
+    /**
+     * Default delete rule for one-to-many relationships. It is used when new rels are
+     * created via 'New' button or reverse engineering
+     */
+    public static final int DEF_DELETE_RULE_TO_MANY = DeleteRule.DENY;
+    
+    /**
+     * Default delete rule for many-to-one relationships. It is used when new rels are
+     * created via 'New' button or reverse engineering
+     */
+    public static final int DEF_DELETE_RULE_TO_ONE = DeleteRule.NULLIFY;
 
     protected static ModelerPreferences sharedInstance;
 

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/CreateObjEntityAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/CreateObjEntityAction.java?rev=673241&r1=673240&r2=673241&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/CreateObjEntityAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/CreateObjEntityAction.java
Tue Jul  1 17:12:36 2008
@@ -19,8 +19,6 @@
 
 package org.apache.cayenne.modeler.action;
 
-import java.awt.event.ActionEvent;
-
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.ObjEntity;
@@ -30,11 +28,14 @@
 import org.apache.cayenne.modeler.ProjectController;
 import org.apache.cayenne.modeler.event.EntityDisplayEvent;
 import org.apache.cayenne.modeler.util.CayenneAction;
+import org.apache.cayenne.modeler.util.DeleteRuleUpdater;
 import org.apache.cayenne.project.NamedObjectFactory;
 import org.apache.cayenne.project.ProjectPath;
 import org.apache.cayenne.util.EntityMergeSupport;
 import org.apache.cayenne.util.NameConverter;
 
+import java.awt.event.ActionEvent;
+
 /**
  * @author Andrus Adamchik
  */
@@ -51,6 +52,7 @@
         super(getActionName(), application);
     }
 
+    @Override
     public String getIconName() {
         return "icon-new_objentity.gif";
     }
@@ -58,6 +60,7 @@
     /**
      * @see org.apache.cayenne.modeler.util.CayenneAction#performAction(ActionEvent)
      */
+    @Override
     public void performAction(ActionEvent e) {
         createObjEntity();
     }
@@ -109,6 +112,7 @@
 
         // perform the merge
         EntityMergeSupport merger = new EntityMergeSupport(dataMap);
+        merger.addEntityMergeListener(DeleteRuleUpdater.getEntityMergeListener());
         merger.synchronizeWithDbEntity(entity);
 
         mediator.fireObjEntityEvent(new EntityEvent(this, entity, MapEvent.ADD));
@@ -125,6 +129,7 @@
     /**
      * Returns <code>true</code> if path contains a DataMap object.
      */
+    @Override
     public boolean enableForPath(ProjectPath path) {
         if (path == null) {
             return false;

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/CreateRelationshipAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/CreateRelationshipAction.java?rev=673241&r1=673240&r2=673241&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/CreateRelationshipAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/CreateRelationshipAction.java
Tue Jul  1 17:12:36 2008
@@ -19,22 +19,19 @@
 
 package org.apache.cayenne.modeler.action;
 
-import java.awt.event.ActionEvent;
-
-import org.apache.cayenne.map.DbEntity;
-import org.apache.cayenne.map.DbRelationship;
-import org.apache.cayenne.map.Entity;
-import org.apache.cayenne.map.ObjEntity;
-import org.apache.cayenne.map.ObjRelationship;
+import org.apache.cayenne.map.*;
 import org.apache.cayenne.map.event.MapEvent;
 import org.apache.cayenne.map.event.RelationshipEvent;
 import org.apache.cayenne.modeler.Application;
 import org.apache.cayenne.modeler.ProjectController;
 import org.apache.cayenne.modeler.event.RelationshipDisplayEvent;
 import org.apache.cayenne.modeler.util.CayenneAction;
+import org.apache.cayenne.modeler.util.DeleteRuleUpdater;
 import org.apache.cayenne.project.NamedObjectFactory;
 import org.apache.cayenne.project.ProjectPath;
 
+import java.awt.event.ActionEvent;
+
 /**
  * @author Andrus Adamchik
  */
@@ -51,6 +48,7 @@
         super(getActionName(), application);
     }
 
+    @Override
     public String getIconName() {
         return "icon-relationship.gif";
     }
@@ -58,6 +56,7 @@
     /**
      * @see org.apache.cayenne.modeler.util.CayenneAction#performAction(ActionEvent)
      */
+    @Override
     public void performAction(ActionEvent e) {
         ObjEntity objEnt = getProjectController().getCurrentObjEntity();
         if (objEnt != null) {
@@ -78,6 +77,8 @@
                 ObjRelationship.class,
                 objEnt);
         rel.setSourceEntity(objEnt);
+        DeleteRuleUpdater.updateObjRelationship(rel);
+        
         objEnt.addRelationship(rel);
 
         mediator.fireObjRelationshipEvent(
@@ -118,6 +119,7 @@
     /**
     * Returns <code>true</code> if path contains an Entity object.
     */
+    @Override
     public boolean enableForPath(ProjectPath path) {
         if (path == null) {
             return false;

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/DbLoaderHelper.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/DbLoaderHelper.java?rev=673241&r1=673240&r2=673241&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/DbLoaderHelper.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/DbLoaderHelper.java
Tue Jul  1 17:12:36 2008
@@ -19,16 +19,6 @@
 
 package org.apache.cayenne.modeler.dialog.db;
 
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-
-import javax.swing.JFrame;
-import javax.swing.JOptionPane;
-import javax.swing.SwingUtilities;
-
 import org.apache.cayenne.CayenneException;
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.access.DbLoader;
@@ -43,12 +33,21 @@
 import org.apache.cayenne.modeler.Application;
 import org.apache.cayenne.modeler.ProjectController;
 import org.apache.cayenne.modeler.event.DataMapDisplayEvent;
+import org.apache.cayenne.modeler.util.DeleteRuleUpdater;
 import org.apache.cayenne.modeler.util.LongRunningTask;
 import org.apache.cayenne.project.NamedObjectFactory;
 import org.apache.cayenne.util.Util;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import javax.swing.*;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
 /**
  * Stateful helper class that encapsulates access to DbLoader.
  * 
@@ -82,6 +81,11 @@
     protected List schemas;
 
     protected String loadStatusNote;
+    
+    /**
+     * Obj Entities which were added to project during reverse engineering 
+     */
+    protected List<ObjEntity> addedObjEntities;
 
     static synchronized DbLoaderMergeDialog getMergeDialogInstance() {
         if (mergeDialog == null) {
@@ -168,6 +172,7 @@
         this.tableNamePattern = dialog.getTableNamePattern();
         this.loadProcedures = dialog.isLoadingProcedures();
         this.procedureNamePattern = dialog.getProcedureNamePattern();
+        this.addedObjEntities = new ArrayList<ObjEntity>();
 
         // load DataMap...
         LongRunningTask loadDataMapTask = new LoadDataMapTask(Application
@@ -239,6 +244,7 @@
             checkCanceled();
 
             loadStatusNote = "Creating ObjEntity '" + entity.getName() + "'...";
+            addedObjEntities.add(entity);
 
             if (existingMap) {
                 mediator.fireObjEntityEvent(new EntityEvent(this, entity, MapEvent.ADD));
@@ -280,22 +286,27 @@
             setMaxValue(10);
         }
 
+        @Override
         protected String getCurrentNote() {
             return loadStatusNote;
         }
 
+        @Override
         protected int getCurrentValue() {
             return getMinValue();
         }
 
+        @Override
         protected boolean isIndeterminate() {
             return true;
         }
 
+        @Override
         public boolean isCanceled() {
             return isStoppingReverseEngineering();
         }
 
+        @Override
         public void setCanceled(boolean b) {
             if (b) {
                 loadStatusNote = "Canceling..";
@@ -311,6 +322,7 @@
             super(frame, title);
         }
 
+        @Override
         protected void execute() {
             loadStatusNote = "Loading available schemas...";
 
@@ -329,6 +341,7 @@
             super(frame, title);
         }
 
+        @Override
         protected void execute() {
 
             loadStatusNote = "Preparing...";
@@ -351,6 +364,13 @@
 
             try {
                 loader.loadDataMapFromDB(schemaName, tableNamePattern, dataMap);
+                
+                /**
+                 * Update default rules for relationships 
+                 */
+                for (int i = 0; i < addedObjEntities.size(); i++) {
+                    DeleteRuleUpdater.updateObjEntity(addedObjEntities.get(i));
+                }
             }
             catch (Throwable th) {
                 if (!isCanceled()) {

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/EntitySyncController.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/EntitySyncController.java?rev=673241&r1=673240&r2=673241&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/EntitySyncController.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/EntitySyncController.java
Tue Jul  1 17:12:36 2008
@@ -19,18 +19,19 @@
 
 package org.apache.cayenne.modeler.dialog.objentity;
 
-import java.awt.Component;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.modeler.util.CayenneController;
+import org.apache.cayenne.modeler.util.DeleteRuleUpdater;
+import org.apache.cayenne.util.EntityMergeSupport;
+
+import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 
-import org.apache.cayenne.map.DbEntity;
-import org.apache.cayenne.map.ObjEntity;
-import org.apache.cayenne.modeler.util.CayenneController;
-import org.apache.cayenne.util.EntityMergeSupport;
-
 public class EntitySyncController extends CayenneController {
 
     protected DbEntity dbEntity;
@@ -60,6 +61,7 @@
         }
 
         final EntityMergeSupport merger = new EntityMergeSupport(dbEntity.getDataMap());
+        merger.addEntityMergeListener(DeleteRuleUpdater.getEntityMergeListener());
 
         // see if we need to remove meaningful attributes...
         boolean showDialog = false;
@@ -110,6 +112,7 @@
         return cancel[0] ? null : merger;
     }
 
+    @Override
     public Component getView() {
         return view;
     }

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoModel.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoModel.java?rev=673241&r1=673240&r2=673241&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoModel.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjRelationshipInfoModel.java
Tue Jul  1 17:12:36 2008
@@ -19,21 +19,10 @@
 
 package org.apache.cayenne.modeler.dialog.objentity;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-
 import org.apache.cayenne.CayenneRuntimeException;
-import org.apache.cayenne.map.DbEntity;
-import org.apache.cayenne.map.DbRelationship;
-import org.apache.cayenne.map.Entity;
-import org.apache.cayenne.map.ObjAttribute;
-import org.apache.cayenne.map.ObjEntity;
-import org.apache.cayenne.map.ObjRelationship;
-import org.apache.cayenne.map.Relationship;
+import org.apache.cayenne.map.*;
 import org.apache.cayenne.modeler.util.Comparators;
+import org.apache.cayenne.modeler.util.DeleteRuleUpdater;
 import org.apache.cayenne.util.Util;
 import org.scopemvc.core.IntIndexSelector;
 import org.scopemvc.core.ModelChangeEvent;
@@ -42,6 +31,8 @@
 import org.scopemvc.model.basic.BasicModel;
 import org.scopemvc.model.collection.ListModel;
 
+import java.util.*;
+
 /**
  * A Scope model for mapping an ObjRelationship to one or more DbRelationships.
  * 
@@ -221,6 +212,7 @@
         this.relationshipName = relationshipName;
     }
 
+    @Override
     public void modelChanged(ModelChangeEvent event) {
 
         // if a different relationship was selected, we may need to rebuild the list
@@ -274,6 +266,8 @@
      */
     public synchronized boolean savePath() {
         boolean hasChanges = false;
+        
+        boolean oldToMany = relationship.isToMany();
 
         if (!Util.nullSafeEquals(relationship.getName(), relationshipName)) {
             hasChanges = true;
@@ -323,6 +317,14 @@
             hasChanges = true;
             relationship.setMapKey(mapKey);
         }
+        
+        /**
+         * As of CAY-436 here we check if to-many property has changed during the editing,
+         * and if so, delete rule must be reset to default value
+         */
+        if (hasChanges && relationship.isToMany() != oldToMany) {
+            DeleteRuleUpdater.updateObjRelationship(relationship);
+        }
 
         return hasChanges;
     }

Added: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DeleteRuleUpdater.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DeleteRuleUpdater.java?rev=673241&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DeleteRuleUpdater.java
(added)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/util/DeleteRuleUpdater.java
Tue Jul  1 17:12:36 2008
@@ -0,0 +1,85 @@
+/*****************************************************************
+ *   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.modeler.util;
+
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.ObjRelationship;
+import org.apache.cayenne.modeler.ModelerPreferences;
+import org.apache.cayenne.util.EntityMergeListener;
+
+import java.util.Collection;
+
+/**
+ * DeleteRuleUpdater is responsible for auto-setting delete rules for object relationships
+ * @author Andrey Razumovsky
+ */
+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() ? ModelerPreferences.DEF_DELETE_RULE_TO_MANY :
+            ModelerPreferences.DEF_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;
+    }
+}



Mime
View raw message