cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1407948 - in /cayenne/main/trunk/framework: cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/ cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/ cayenne-tools/src/main/java/org/apache/cayenne/tools/ cayenne...
Date Sun, 11 Nov 2012 11:55:09 GMT
Author: aadamchik
Date: Sun Nov 11 11:55:08 2012
New Revision: 1407948

URL: http://svn.apache.org/viewvc?rev=1407948&view=rev
Log:
CAY-1769 cdbimport improvements: meaningfulPk flag must be turned into a pattern

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DbLoader.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportParameters.java
    cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DbLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DbLoader.java?rev=1407948&r1=1407947&r2=1407948&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DbLoader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DbLoader.java
Sun Nov 11 11:55:08 2012
@@ -142,8 +142,11 @@ public class DbLoader {
         return metaData;
     }
 
-    public void setCreatingMeaningfulPK(boolean check) {
-        this.creatingMeaningfulPK = check;
+    /**
+     * @since 3.0
+     */
+    public void setCreatingMeaningfulPK(boolean creatingMeaningfulPK) {
+        this.creatingMeaningfulPK = creatingMeaningfulPK;
     }
 
     /**
@@ -209,7 +212,7 @@ public class DbLoader {
     }
 
     /**
-     * Retrieves catalogues for the database associated with this DbLoader.
+     * Retrieves catalogs for the database associated with this DbLoader.
      * 
      * @return List with the catalog names, empty Array if none found.
      */
@@ -518,8 +521,7 @@ public class DbLoader {
             return;
         }
 
-        List<ObjEntity> loadedEntities = new ArrayList<ObjEntity>(
-                dbEntityList.size());
+        List<ObjEntity> loadedEntities = new ArrayList<ObjEntity>(dbEntityList.size());
 
         String packageName = map.getDefaultPackage();
         if (Util.isEmptyString(packageName)) {
@@ -551,9 +553,8 @@ public class DbLoader {
             ObjEntity objEntity = new ObjEntity(objEntityName);
             objEntity.setDbEntity(dbEntity);
 
-            objEntity
-                    .setClassName(getGenericClassName() != null ? getGenericClassName()
-                            : packageName + objEntity.getName());
+            objEntity.setClassName(getGenericClassName() != null ? getGenericClassName()
: packageName
+                    + objEntity.getName());
             map.addObjEntity(objEntity);
             loadedEntities.add(objEntity);
             // added entity without attributes or relationships...
@@ -563,8 +564,15 @@ public class DbLoader {
         }
 
         // update ObjEntity attributes and relationships
-        new EntityMergeSupport(map, namingStrategy, !creatingMeaningfulPK)
-                .synchronizeWithDbEntities(loadedEntities);
+        EntityMergeSupport objEntityMerger = createEntityMerger(map);
+        objEntityMerger.synchronizeWithDbEntities(loadedEntities);
+    }
+    
+    /**
+     * @since 3.2
+     */
+    protected EntityMergeSupport createEntityMerger(DataMap map) {
+        return new EntityMergeSupport(map, namingStrategy, !creatingMeaningfulPK);
     }
 
     /** Loads database relationships into a DataMap. */

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=1407948&r1=1407947&r2=1407948&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
Sun Nov 11 11:55:08 2012
@@ -95,6 +95,20 @@ public class EntityMergeSupport {
 
         return changed;
     }
+    
+    /**
+     * @since 3.2
+     */
+    protected boolean removePK(DbEntity dbEntity) {
+        return removeMeaningfulPKs;
+    }
+    
+    /**
+     * @since 3.2
+     */
+    protected boolean removeFK(DbEntity dbEntity) {
+        return removeMeaningfulFKs;
+    }
 
     /**
      * Updates ObjEntity attributes and relationships based on the current state of its
@@ -104,7 +118,12 @@ public class EntityMergeSupport {
      */
     public boolean synchronizeWithDbEntity(ObjEntity entity) {
 
-        if (entity == null || entity.getDbEntity() == null) {
+        if (entity == null) {
+            return false;
+        }
+        
+        DbEntity dbEntity = entity.getDbEntity();
+        if (dbEntity == null) {
             return false;
         }
 
@@ -115,7 +134,7 @@ public class EntityMergeSupport {
         // button)
         synchronized (map) {
 
-            if (removeMeaningfulFKs) {
+            if (removeFK(dbEntity)) {
 
                 // get rid of attributes that are now src attributes for relationships
                 for (DbAttribute da : getMeaningfulFKs(entity)) {
@@ -150,9 +169,9 @@ public class EntityMergeSupport {
 
             // add missing relationships
             for (DbRelationship dr : getRelationshipsToAdd(entity)) {
-                DbEntity dbEntity = (DbEntity) dr.getTargetEntity();
+                DbEntity targetEntity = (DbEntity) dr.getTargetEntity();
 
-                for (Entity mappedTarget : map.getMappedEntities(dbEntity)) {
+                for (Entity mappedTarget : map.getMappedEntities(targetEntity)) {
 
                     // avoid duplicate names
                     String relationshipName = namingStrategy
@@ -202,21 +221,25 @@ public class EntityMergeSupport {
      * ObjEntity.
      */
     protected List<DbAttribute> getAttributesToAdd(ObjEntity objEntity) {
+        DbEntity dbEntity = objEntity.getDbEntity();
+
         List<DbAttribute> missing = new ArrayList<DbAttribute>();
-        Collection<DbRelationship> rels = objEntity.getDbEntity().getRelationships();
-        Collection<DbRelationship> incomingRels = getIncomingRelationships(objEntity
-                .getDbEntity());
 
-        for (DbAttribute dba : objEntity.getDbEntity().getAttributes()) {
-            
+        Collection<DbRelationship> rels = dbEntity.getRelationships();
+        Collection<DbRelationship> incomingRels = getIncomingRelationships(dbEntity);
+
+        for (DbAttribute dba : dbEntity.getAttributes()) {
+
             if (dba.getName() == null) {
                 continue;
             }
-            
+
             if (objEntity.getAttributeForDbAttribute(dba) != null) {
                 continue;
             }
 
+            boolean removeMeaningfulPKs = removePK(dbEntity);
+
             if (removeMeaningfulPKs && dba.isPrimaryKey()) {
                 continue;
             }
@@ -238,8 +261,7 @@ public class EntityMergeSupport {
                 if (!dba.isPrimaryKey() && isFK) {
                     continue;
                 }
-            }
-            else {
+            } else {
                 if (isFK) {
                     continue;
                 }
@@ -261,8 +283,7 @@ public class EntityMergeSupport {
                 if (!dba.isPrimaryKey() && isFK) {
                     continue;
                 }
-            }
-            else {
+            } else {
                 if (isFK) {
                     continue;
                 }

Modified: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java?rev=1407948&r1=1407947&r2=1407948&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java
(original)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java
Sun Nov 11 11:55:08 2012
@@ -42,24 +42,23 @@ public class DbImporterTask extends Task
      */
     private String schemaName;
 
+    /**
+     * @deprecated since 3.2 in favor of "meaningfulPkTable"
+     */
+    private boolean meaningfulPk;
+
     public DbImporterTask() {
         parameters = new DbImportParameters();
         parameters.setOverwrite(true);
         parameters.setImportProcedures(false);
-        parameters.setMeaningfulPk(false);
         parameters.setNamingStrategy("org.apache.cayenne.map.naming.SmartNamingStrategy");
     }
 
     @Override
     public void execute() {
 
-        if (schemaName != null) {
-            log("'schemaName' property is deprecated. Use 'schema' instead", Project.MSG_WARN);
-        }
-
-        if (parameters.getSchema() == null) {
-            parameters.setSchema(schemaName);
-        }
+        initSchema();
+        initMeaningfulPkTables();
 
         validateAttributes();
 
@@ -146,8 +145,18 @@ public class DbImporterTask extends Task
         parameters.setProcedurePattern(procedurePattern);
     }
 
+    /**
+     * @deprecated since 3.2 use {@link #setMeaningfulPkTables(String)}
+     */
     public void setMeaningfulPk(boolean meaningfulPk) {
-        parameters.setMeaningfulPk(meaningfulPk);
+        this.meaningfulPk = meaningfulPk;
+    }
+
+    /**
+     * @since 3.2
+     */
+    public void setMeaningfulPkTables(String meaningfulPkTables) {
+        parameters.setMeaningfulPkTables(meaningfulPkTables);
     }
 
     public void setNamingStrategy(String namingStrategy) {
@@ -191,4 +200,24 @@ public class DbImporterTask extends Task
     public void setExcludeTables(String excludeTables) {
         parameters.setExcludeTables(excludeTables);
     }
+
+    private void initSchema() {
+        if (schemaName != null) {
+            log("'schemaName' property is deprecated. Use 'schema' instead", Project.MSG_WARN);
+        }
+
+        if (parameters.getSchema() == null) {
+            parameters.setSchema(schemaName);
+        }
+    }
+
+    private void initMeaningfulPkTables() {
+        if (meaningfulPk) {
+            log("'meaningfulPk' property is deprecated. Use 'meaningfulPkTables' pattern
instead", Project.MSG_WARN);
+        }
+
+        if (parameters.getMeaningfulPkTables() == null && meaningfulPk) {
+            parameters.setMeaningfulPkTables("*");
+        }
+    }
 }

Modified: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java?rev=1407948&r1=1407947&r2=1407948&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java
Sun Nov 11 11:55:08 2012
@@ -37,6 +37,7 @@ import org.apache.cayenne.conn.DataSourc
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.MapLoader;
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.naming.NamingStrategy;
@@ -45,6 +46,7 @@ import org.apache.cayenne.project.Projec
 import org.apache.cayenne.resource.URLResource;
 import org.apache.cayenne.tools.NamePatternMatcher;
 import org.apache.cayenne.util.DeleteRuleUpdater;
+import org.apache.cayenne.util.EntityMergeSupport;
 import org.apache.commons.logging.Log;
 import org.xml.sax.InputSource;
 
@@ -88,7 +90,7 @@ public class DbImportAction {
             logger.debug("Importer options - tablePattern: " + parameters.getTablePattern());
             logger.debug("Importer options - importProcedures: " + parameters.isImportProcedures());
             logger.debug("Importer options - procedurePattern: " + parameters.getProcedurePattern());
-            logger.debug("Importer options - meaningfulPk: " + parameters.isMeaningfulPk());
+            logger.debug("Importer options - meaningfulPkTables: " + parameters.getMeaningfulPkTables());
             logger.debug("Importer options - namingStrategy: " + parameters.getNamingStrategy());
             logger.debug("Importer options - includeTables: " + parameters.getIncludeTables());
             logger.debug("Importer options - excludeTables: " + parameters.getExcludeTables());
@@ -153,15 +155,27 @@ public class DbImportAction {
 
         final NamePatternMatcher nameFilter = new NamePatternMatcher(logger, parameters.getIncludeTables(),
                 parameters.getExcludeTables());
+        final NamePatternMatcher meaningfulPkFilter = new NamePatternMatcher(logger,
+                parameters.getMeaningfulPkTables(), null);
 
         DbLoader loader = new DbLoader(connection, adapter, loaderDelegate) {
             @Override
             public boolean includeTableName(String tableName) {
                 return nameFilter.isIncluded(tableName);
             }
+            
+            @Override
+            protected EntityMergeSupport createEntityMerger(DataMap map) {
+                return new EntityMergeSupport(map, namingStrategy, true) {
+
+                    @Override
+                    protected boolean removePK(DbEntity dbEntity) {
+                        return !meaningfulPkFilter.isIncluded(dbEntity.getName());
+                    }
+                };
+            }
         };
 
-        loader.setCreatingMeaningfulPK(parameters.isMeaningfulPk());
 
         // TODO: load via DI AdhocObjectFactory
         String namingStrategy = parameters.getNamingStrategy();

Modified: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportParameters.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportParameters.java?rev=1407948&r1=1407947&r2=1407948&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportParameters.java
(original)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportParameters.java
Sun Nov 11 11:55:08 2012
@@ -67,11 +67,7 @@ public class DbImportParameters {
      */
     private String procedurePattern;
 
-    /**
-     * Indicates whether primary keys should be mapped as meaningful attributes
-     * in the object entities.
-     */
-    private boolean meaningfulPk;
+    private String meaningfulPkTables;
 
     /**
      * Java class implementing org.apache.cayenne.map.naming.NamingStrategy.
@@ -174,14 +170,6 @@ public class DbImportParameters {
         this.procedurePattern = procedurePattern;
     }
 
-    public boolean isMeaningfulPk() {
-        return meaningfulPk;
-    }
-
-    public void setMeaningfulPk(boolean meaningfulPk) {
-        this.meaningfulPk = meaningfulPk;
-    }
-
     public String getNamingStrategy() {
         return namingStrategy;
     }
@@ -245,4 +233,17 @@ public class DbImportParameters {
     public void setExcludeTables(String excludeTables) {
         this.excludeTables = excludeTables;
     }
+
+    /**
+     * Returns a comma-separated list of Perl5 regular expressions that match
+     * table names for which {@link DbImportAction} should include ObjAttribute
+     * for PK.
+     */
+    public String getMeaningfulPkTables() {
+        return meaningfulPkTables;
+    }
+
+    public void setMeaningfulPkTables(String meaningfulPkTables) {
+        this.meaningfulPkTables = meaningfulPkTables;
+    }
 }

Modified: cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java?rev=1407948&r1=1407947&r2=1407948&view=diff
==============================================================================
--- cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
(original)
+++ cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
Sun Nov 11 11:55:08 2012
@@ -146,10 +146,17 @@ public class DbImporterMojo extends Abst
      * Default is <code>false</code>.
      * 
      * @parameter expression="${cdbimport.meaningfulPk}" default-value="false"
+     * @deprecated since 3.2 use meaningfulPkTables
      */
     private boolean meaningfulPk;
 
     /**
+     * @parameter expression="${cdbimport.meaningfulPkTables}"
+     * @since 3.2
+     */
+    private String meaningfulPkTables;
+
+    /**
      * Java class implementing org.apache.cayenne.map.naming.NamingStrategy.
      * This is used to specify how ObjEntities will be mapped from the imported
      * DB schema.
@@ -212,7 +219,7 @@ public class DbImporterMojo extends Abst
         parameters.setDriver(driver);
         parameters.setImportProcedures(importProcedures);
         parameters.setDataMapFile(map);
-        parameters.setMeaningfulPk(meaningfulPk);
+        parameters.setMeaningfulPkTables(getMeaningfulPkTables());
         parameters.setNamingStrategy(namingStrategy);
         parameters.setOverwrite(overwrite);
         parameters.setPassword(password);
@@ -250,4 +257,16 @@ public class DbImporterMojo extends Abst
         return schema != null ? schema : schemaName;
     }
 
+    private String getMeaningfulPkTables() {
+        if (meaningfulPk) {
+            getLog().warn("'meaningfulPk' property is deprecated. Use 'meaningfulPkTables'
pattern instead");
+        }
+
+        if (meaningfulPkTables != null) {
+            return meaningfulPkTables;
+        }
+
+        return meaningfulPk ? "*" : null;
+    }
+
 }



Mime
View raw message