cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject [1/3] cayenne git commit: CAY-2116 Split schema synchronization code in a separate module
Date Sun, 02 Oct 2016 18:31:30 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 827056e81 -> 38e4e7fda


http://git-wip-us.apache.org/repos/asf/cayenne/blob/38e4e7fd/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/ReverseEngineeringAction.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/ReverseEngineeringAction.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/ReverseEngineeringAction.java
index 08c0c1f..b531171 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/ReverseEngineeringAction.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/ReverseEngineeringAction.java
@@ -19,35 +19,33 @@
 
 package org.apache.cayenne.modeler.action;
 
-import java.awt.event.ActionEvent;
-
 import org.apache.cayenne.configuration.DataChannelDescriptor;
 import org.apache.cayenne.configuration.event.DataMapEvent;
+import org.apache.cayenne.dbsync.naming.NameBuilder;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.event.MapEvent;
-import org.apache.cayenne.dbsync.naming.DuplicateNameResolver;
-import org.apache.cayenne.dbsync.naming.NameCheckers;
 import org.apache.cayenne.modeler.Application;
 import org.apache.cayenne.modeler.CayenneModelerController;
 import org.apache.cayenne.modeler.ProjectController;
 import org.apache.cayenne.modeler.util.CayenneAction;
 
+import java.awt.event.ActionEvent;
+
 /**
  * Action that imports database structure into a DataMap.
  */
 public class ReverseEngineeringAction extends CayenneAction {
 
-    public static String getActionName() {
-        return "Reengineer Database Schema";
-    }
-
     public ReverseEngineeringAction(Application application) {
         super(getActionName(), application);
     }
 
+    public static String getActionName() {
+        return "Reengineer Database Schema";
+    }
+
     /**
-     * Connects to DB and delegates processing to DbLoaderController, starting it
-     * asynchronously.
+     * Connects to DB and delegates processing to DbLoaderController, starting it asynchronously.
      */
     @Override
     public void performAction(ActionEvent event) {
@@ -55,8 +53,10 @@ public class ReverseEngineeringAction extends CayenneAction {
         DataMap dataMap = projectController.getCurrentDataMap();
         DataChannelDescriptor dataChannelDescriptor = projectController.getCurrentDataChanel();
         if (dataMap == null) {
-            dataMap = new DataMap(DuplicateNameResolver.resolve(NameCheckers.dataMap));
-            dataMap.setName(DuplicateNameResolver.resolve(NameCheckers.dataMap, projectController.getProject().getRootNode()));
+            dataMap = new DataMap();
+            dataMap.setName(NameBuilder
+                    .builder(dataMap, dataChannelDescriptor)
+                    .name());
             dataChannelDescriptor.getDataMaps().add(dataMap);
             getProjectController().fireDataMapEvent(new DataMapEvent(this, dataMap, MapEvent.ADD));
         }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/38e4e7fd/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/ResolveDbRelationshipDialog.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/ResolveDbRelationshipDialog.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/ResolveDbRelationshipDialog.java
index 9284881..885c4c2 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/ResolveDbRelationshipDialog.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/ResolveDbRelationshipDialog.java
@@ -19,35 +19,16 @@
 
 package org.apache.cayenne.modeler.dialog;
 
-import java.awt.BorderLayout;
-import java.awt.Dimension;
-import java.awt.FlowLayout;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-
-import javax.swing.JButton;
-import javax.swing.JComboBox;
-import javax.swing.JOptionPane;
-import javax.swing.JPanel;
-import javax.swing.JScrollPane;
-import javax.swing.JTextField;
-import javax.swing.ListSelectionModel;
-import javax.swing.table.TableColumn;
-
+import com.jgoodies.forms.builder.PanelBuilder;
+import com.jgoodies.forms.layout.CellConstraints;
+import com.jgoodies.forms.layout.FormLayout;
 import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.dbsync.naming.NameBuilder;
 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.Relationship;
 import org.apache.cayenne.map.event.MapEvent;
 import org.apache.cayenne.map.event.RelationshipEvent;
-import org.apache.cayenne.dbsync.naming.DuplicateNameResolver;
-import org.apache.cayenne.dbsync.naming.NameCheckers;
 import org.apache.cayenne.modeler.Application;
 import org.apache.cayenne.modeler.pref.TableColumnPreferences;
 import org.apache.cayenne.modeler.undo.RelationshipUndoableEdit;
@@ -58,9 +39,15 @@ import org.apache.cayenne.modeler.util.PanelFactory;
 import org.apache.cayenne.modeler.util.combo.AutoCompletion;
 import org.apache.cayenne.util.Util;
 
-import com.jgoodies.forms.builder.PanelBuilder;
-import com.jgoodies.forms.layout.CellConstraints;
-import com.jgoodies.forms.layout.FormLayout;
+import javax.swing.*;
+import javax.swing.table.TableColumn;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
 
 /**
  * Editor of DbRelationship joins.
@@ -156,7 +143,7 @@ public class ResolveDbRelationshipDialog extends CayenneDialog {
         builder.add(buttons, cc.xywh(5, 9, 1, 3));
 
         getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
-        getContentPane().add(PanelFactory.createButtonPanel(new JButton[] {
+        getContentPane().add(PanelFactory.createButtonPanel(new JButton[]{
                 saveButton, cancelButton
         }), BorderLayout.SOUTH);
     }
@@ -286,44 +273,19 @@ public class ResolveDbRelationshipDialog extends CayenneDialog {
         stopEditing();
 
         // extract names...
-        String sourceEntityName = name.getText();
-        if (sourceEntityName.length() == 0) {
-            sourceEntityName = null;
-        }
-
-        if (sourceEntityName == null) {
-            sourceEntityName = DuplicateNameResolver.resolve(NameCheckers.dbRelationship,
relationship.getSourceEntity());
-        }
-
-        if (!validateName(relationship.getSourceEntity(), relationship, sourceEntityName))
{
-            return;
-        }
-
-        String targetEntityName = reverseName.getText().trim();
-        if (targetEntityName.length() == 0) {
-            targetEntityName = null;
-        }
-
-        if (targetEntityName == null) {
-            targetEntityName = DuplicateNameResolver.resolve(NameCheckers.dbRelationship,
relationship.getTargetEntity());
-        }
+        String sourceEntityName = NameBuilder
+                .builder(relationship, relationship.getSourceEntity())
+                .baseName(name.getText().trim())
+                .name();
 
         // check if reverse name is valid
         DbJoinTableModel model = (DbJoinTableModel) table.getModel();
         boolean updatingReverse = model.getObjectList().size() > 0;
 
-        if (updatingReverse
-                && !validateName(
-                        relationship.getTargetEntity(),
-                        reverseRelationship,
-                        targetEntityName)) {
-            return;
-        }
-
         // handle name update
         if (!Util.nullSafeEquals(sourceEntityName, relationship.getName())) {
             String oldName = relationship.getName();
-            
+
             relationship.setName(sourceEntityName);
 
             undo.addNameUndo(relationship, oldName, sourceEntityName);
@@ -347,7 +309,12 @@ public class ResolveDbRelationshipDialog extends CayenneDialog {
 
             // If didn't find anything, create reverseDbRel
             if (reverseRelationship == null) {
-                reverseRelationship = new DbRelationship(targetEntityName);
+                reverseRelationship = new DbRelationship();
+                reverseRelationship.setName(NameBuilder
+                        .builder(reverseRelationship, relationship.getTargetEntity())
+                        .baseName(reverseName.getText().trim())
+                        .name());
+
                 reverseRelationship.setSourceEntity(relationship.getTargetEntity());
                 reverseRelationship.setTargetEntityName(relationship.getSourceEntity());
                 reverseRelationship.setToMany(!relationship.isToMany());
@@ -363,22 +330,26 @@ public class ResolveDbRelationshipDialog extends CayenneDialog {
                                     reverseRelationship.getSourceEntity(),
                                     MapEvent.ADD));
                 }
-            }
-            else if (!Util
-                    .nullSafeEquals(targetEntityName, reverseRelationship.getName())) {
+            } else {
 
-                String oldName = reverseRelationship.getName();
-                
-                reverseRelationship.setName(targetEntityName);
+                String targetEntityName = NameBuilder
+                        .builder(reverseRelationship, relationship.getTargetEntity())
+                        .baseName(reverseName.getText().trim())
+                        .name();
 
-                undo.addNameUndo(reverseRelationship, oldName, targetEntityName);
+                if (!Util.nullSafeEquals(targetEntityName, reverseRelationship.getName()))
{
 
-                getMediator().fireDbRelationshipEvent(
-                        new RelationshipEvent(
-                                this,
-                                reverseRelationship,
-                                reverseRelationship.getSourceEntity(),
-                                oldName));
+                    String oldName = reverseRelationship.getName();
+                    reverseRelationship.setName(targetEntityName);
+                    undo.addNameUndo(reverseRelationship, oldName, targetEntityName);
+
+                    getMediator().fireDbRelationshipEvent(
+                            new RelationshipEvent(
+                                    this,
+                                    reverseRelationship,
+                                    reverseRelationship.getSourceEntity(),
+                                    oldName));
+                }
             }
 
             Collection reverseJoins = getReverseJoins();
@@ -398,20 +369,6 @@ public class ResolveDbRelationshipDialog extends CayenneDialog {
                                 .getSourceEntity()));
     }
 
-    private boolean validateName(Entity entity, Relationship aRelationship, String newName)
{
-        Relationship existing = entity.getRelationship(newName);
-        if (existing != null && (aRelationship == null || aRelationship != existing))
{
-            JOptionPane.showMessageDialog(
-                    this,
-                    "There is an existing relationship named \""
-                            + newName
-                            + "\". Select a different name.");
-            return false;
-        }
-
-        return true;
-    }
-
     private Collection getReverseJoins() {
         Collection<DbJoin> joins = relationship.getJoins();
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/38e4e7fd/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/DbLoaderHelper.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/DbLoaderHelper.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/DbLoaderHelper.java
index acaafdf..7ee114c 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/DbLoaderHelper.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/DbLoaderHelper.java
@@ -20,13 +20,15 @@
 package org.apache.cayenne.modeler.dialog.db;
 
 import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.configuration.ConfigurationNode;
 import org.apache.cayenne.configuration.DataChannelDescriptor;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dbimport.ReverseEngineering;
 import org.apache.cayenne.dbsync.CayenneDbSyncModule;
+import org.apache.cayenne.dbsync.naming.NameBuilder;
+import org.apache.cayenne.dbsync.reverse.FiltersConfigBuilder;
 import org.apache.cayenne.dbsync.reverse.db.DbLoader;
 import org.apache.cayenne.dbsync.reverse.db.DefaultDbLoaderDelegate;
-import org.apache.cayenne.dbsync.reverse.FiltersConfigBuilder;
 import org.apache.cayenne.di.DIBootstrap;
 import org.apache.cayenne.di.Injector;
 import org.apache.cayenne.map.DataMap;
@@ -35,8 +37,6 @@ import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.event.EntityEvent;
 import org.apache.cayenne.map.event.MapEvent;
-import org.apache.cayenne.dbsync.naming.DuplicateNameResolver;
-import org.apache.cayenne.dbsync.naming.NameCheckers;
 import org.apache.cayenne.modeler.Application;
 import org.apache.cayenne.modeler.ProjectController;
 import org.apache.cayenne.modeler.pref.DBConnectionInfo;
@@ -61,13 +61,11 @@ import java.util.List;
  */
 public class DbLoaderHelper {
 
-    private static Log logObj = LogFactory.getLog(DbLoaderHelper.class);
-
     // TODO: this is a temp hack... need to delegate to DbAdapter, or
     // configurable in
     // preferences...
     private static final Collection<String> EXCLUDED_TABLES = Arrays.asList("AUTO_PK_SUPPORT",
"auto_pk_support");
-
+    private static Log logObj = LogFactory.getLog(DbLoaderHelper.class);
     protected boolean stoppingReverseEngineering;
     protected boolean existingMap;
 
@@ -116,14 +114,14 @@ public class DbLoaderHelper {
         }
     }
 
-    public void setStoppingReverseEngineering(boolean stopReverseEngineering) {
-        this.stoppingReverseEngineering = stopReverseEngineering;
-    }
-
     public boolean isStoppingReverseEngineering() {
         return stoppingReverseEngineering;
     }
 
+    public void setStoppingReverseEngineering(boolean stopReverseEngineering) {
+        this.stoppingReverseEngineering = stopReverseEngineering;
+    }
+
     public DataMap getDataMap() {
         return dataMap;
     }
@@ -172,6 +170,13 @@ public class DbLoaderHelper {
         });
     }
 
+    protected ProjectController getMediator() {
+        return mediator;
+    }
+
+    protected DbLoader getLoader() {
+        return loader;
+    }
 
     private final class LoaderDelegate extends DefaultDbLoaderDelegate {
 
@@ -319,7 +324,6 @@ public class DbLoaderHelper {
         }
     }
 
-
     public final class LoadDataMapTask extends DbLoaderTask {
 
         public LoadDataMapTask(JFrame frame, String title) {
@@ -335,8 +339,11 @@ public class DbLoaderHelper {
             DbLoaderHelper.this.existingMap = dataMap != null;
 
             if (!existingMap) {
-                dataMap = new DataMap(DuplicateNameResolver.resolve(NameCheckers.dataMap));
-                dataMap.setName(DuplicateNameResolver.resolve(NameCheckers.dataMap, mediator.getProject().getRootNode()));
+
+                ConfigurationNode root = mediator.getProject().getRootNode();
+
+                dataMap = new DataMap();
+                dataMap.setName(NameBuilder.builder(dataMap, root).name());
             }
 
             if (isCanceled()) {
@@ -351,7 +358,7 @@ public class DbLoaderHelper {
                     reverseEngineering.setConfigurationSource(dataMap.getReverseEngineering().getConfigurationSource());
                 }
             } else {
-                reverseEngineering.setName(DuplicateNameResolver.resolve(NameCheckers.reverseEngineering,
dataChannelDescriptor));
+                reverseEngineering.setName(NameBuilder.builder(reverseEngineering, dataChannelDescriptor).name());
             }
 
             if (dataMap.getConfigurationSource() != null) {
@@ -377,12 +384,4 @@ public class DbLoaderHelper {
         }
     }
 
-    protected ProjectController getMediator() {
-        return mediator;
-    }
-
-    protected DbLoader getLoader() {
-        return loader;
-    }
-	
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/38e4e7fd/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/query/QueryType.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/query/QueryType.java
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/query/QueryType.java
index 8b0dd85..33bff77 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/query/QueryType.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/query/QueryType.java
@@ -18,24 +18,22 @@
  ****************************************************************/
 package org.apache.cayenne.modeler.dialog.query;
 
-import java.awt.Component;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-
-import javax.swing.WindowConstants;
-
 import org.apache.cayenne.configuration.DataChannelDescriptor;
 import org.apache.cayenne.configuration.event.QueryEvent;
+import org.apache.cayenne.dbsync.naming.NameBuilder;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.QueryDescriptor;
 import org.apache.cayenne.map.event.MapEvent;
-import org.apache.cayenne.dbsync.naming.DuplicateNameResolver;
-import org.apache.cayenne.dbsync.naming.NameCheckers;
 import org.apache.cayenne.modeler.ProjectController;
 import org.apache.cayenne.modeler.event.QueryDisplayEvent;
 import org.apache.cayenne.modeler.undo.CreateQueryUndoableEdit;
 import org.apache.cayenne.modeler.util.CayenneController;
 
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
 
 public class QueryType extends CayenneController{
 
@@ -119,11 +117,8 @@ public class QueryType extends CayenneController{
         String queryType = getSelectedQuery();
 
         // update query...
-        String queryName = DuplicateNameResolver.resolve(NameCheckers.query, dataMap);
-
         QueryDescriptor query = QueryDescriptor.descriptor(queryType);
-
-        query.setName(queryName);
+        query.setName(NameBuilder.builder(query, dataMap).name());
         query.setDataMap(dataMap);
         
         dataMap.addQueryDescriptor(query);

http://git-wip-us.apache.org/repos/asf/cayenne/blob/38e4e7fd/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOModelProcessor.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOModelProcessor.java
b/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOModelProcessor.java
index fac6202..f3d858d 100644
--- a/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOModelProcessor.java
+++ b/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOModelProcessor.java
@@ -20,8 +20,7 @@
 package org.apache.cayenne.wocompat;
 
 import org.apache.cayenne.dba.TypesMapping;
-import org.apache.cayenne.dbsync.naming.DuplicateNameResolver;
-import org.apache.cayenne.dbsync.naming.NameCheckers;
+import org.apache.cayenne.dbsync.naming.NameBuilder;
 import org.apache.cayenne.exp.ExpressionException;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.DbEntity;
@@ -726,10 +725,10 @@ public class EOModelProcessor {
 
 			if (relationship.getReverseRelationship() == null) {
 				DbRelationship reverse = relationship.createReverseRelationship();
-
-				String name = DuplicateNameResolver.resolve(NameCheckers.dbRelationship,
-						reverse.getSourceEntity(), relationship.getName() + "Reverse");
-				reverse.setName(name);
+				reverse.setName(NameBuilder.builder(reverse, reverse.getSourceEntity())
+						// TODO: we can do better with ObjectNameGenerator
+						.baseName(relationship.getName() + "Reverse")
+						.name());
 				relationship.getTargetEntity().addRelationship(reverse);
 			}
 		}


Mime
View raw message