cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject [1/2] cayenne git commit: CAY-2116 Split schema synchronization code in a separate module
Date Thu, 03 Nov 2016 16:22:20 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 53a339978 -> 2a06041df


CAY-2116 Split schema synchronization code in a separate module

* refactoring


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/30f92797
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/30f92797
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/30f92797

Branch: refs/heads/master
Commit: 30f92797404abd7e0743485d1cb887efebe6eddd
Parents: 53a3399
Author: Andrus Adamchik <andrus@objectstyle.com>
Authored: Thu Nov 3 19:03:11 2016 +0300
Committer: Andrus Adamchik <andrus@objectstyle.com>
Committed: Thu Nov 3 19:03:11 2016 +0300

----------------------------------------------------------------------
 .../tools/dbimport/DbImportConfiguration.java   |  72 ++----------
 .../tools/dbimport/DefaultDbImportAction.java   | 112 +++++++++++++------
 2 files changed, 86 insertions(+), 98 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/30f92797/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportConfiguration.java
----------------------------------------------------------------------
diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportConfiguration.java
b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportConfiguration.java
index 2b62b06..17537ca 100644
--- a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportConfiguration.java
+++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportConfiguration.java
@@ -33,18 +33,11 @@ import org.apache.cayenne.dbsync.reverse.db.DbLoaderConfiguration;
 import org.apache.cayenne.dbsync.reverse.db.DbLoaderDelegate;
 import org.apache.cayenne.dbsync.reverse.db.DefaultDbLoaderDelegate;
 import org.apache.cayenne.dbsync.reverse.db.LoggingDbLoaderDelegate;
-import org.apache.cayenne.dbsync.reverse.filters.CatalogFilter;
 import org.apache.cayenne.dbsync.reverse.filters.FiltersConfig;
-import org.apache.cayenne.map.DataMap;
-import org.apache.cayenne.map.EntityResolver;
-import org.apache.cayenne.resource.URLResource;
 import org.apache.commons.logging.Log;
 
 import java.io.File;
-import java.io.IOException;
-import java.net.MalformedURLException;
 import java.sql.Connection;
-import java.util.Collections;
 import java.util.regex.Pattern;
 
 /**
@@ -54,24 +47,13 @@ public class DbImportConfiguration {
 
     private static final String DATA_MAP_LOCATION_SUFFIX = ".map.xml";
     private final DataSourceInfo dataSourceInfo = new DataSourceInfo();
-    /**
-     * DB schema to use for DB importing.
-     */
     private final DbLoaderConfiguration dbLoaderConfiguration = new DbLoaderConfiguration();
-    /**
-     * DataMap XML file to use as a base for DB importing.
-     */
     private File dataMapFile;
     /**
      * A default package for ObjEntity Java classes.
      */
     private String defaultPackage;
     private String meaningfulPkTables;
-    /**
-     * Java class implementing org.apache.cayenne.dba.DbAdapter. This attribute
-     * is optional, the default is AutoAdapter, i.e. Cayenne would try to guess
-     * the DB type.
-     */
     private String adapter;
     private boolean usePrimitives;
     private Log logger;
@@ -85,6 +67,9 @@ public class DbImportConfiguration {
         this.logger = logger;
     }
 
+    /**
+     * Retruns DataMap XML file representing the target of the DB import operation.
+     */
     public File getDataMapFile() {
         return dataMapFile;
     }
@@ -109,6 +94,10 @@ public class DbImportConfiguration {
         this.namingStrategy = namingStrategy;
     }
 
+    /**
+     * Returns the name of a Java class implementing {@link DbAdapter}. This attribute is
optional, the default is
+     * {@link org.apache.cayenne.dba.AutoAdapter}, i.e. Cayenne will try to guess the DB
type.
+     */
     public String getAdapter() {
         return adapter;
     }
@@ -217,50 +206,6 @@ public class DbImportConfiguration {
         return nodeDescriptor;
     }
 
-    public DataMap createDataMap() throws IOException {
-        if (dataMapFile == null) {
-            throw new NullPointerException("Null DataMap File.");
-        }
-
-        DataMap dataMap = new DataMap();
-        initializeDataMap(dataMap);
-        return dataMap;
-    }
-
-    protected void initializeDataMap(DataMap dataMap) throws MalformedURLException {
-        dataMap.setName(getDataMapName());
-        dataMap.setConfigurationSource(new URLResource(dataMapFile.toURI().toURL()));
-        dataMap.setNamespace(new EntityResolver(Collections.singleton(dataMap)));
-
-        // update map defaults
-
-        // do not override default package of existing DataMap unless it is
-        // explicitly requested by the plugin caller
-        String defaultPackage = getDefaultPackage();
-        if (defaultPackage != null && defaultPackage.length() > 0) {
-            dataMap.setDefaultPackage(defaultPackage);
-        }
-
-        CatalogFilter[] catalogs = dbLoaderConfiguration.getFiltersConfig().getCatalogs();
-        if (catalogs.length > 0) {
-            // do not override default catalog of existing DataMap unless it is
-            // explicitly requested by the plugin caller, and the provided catalog is
-            // not a pattern
-            String catalog = catalogs[0].name;
-            if (catalog != null && catalog.length() > 0 && catalog.indexOf('%')
< 0) {
-                dataMap.setDefaultCatalog(catalog);
-            }
-
-            // do not override default schema of existing DataMap unless it is
-            // explicitly requested by the plugin caller, and the provided schema is
-            // not a pattern
-            String schema = catalogs[0].schemas[0].name;
-            if (schema != null && schema.length() > 0 && schema.indexOf('%')
< 0) {
-                dataMap.setDefaultSchema(schema);
-            }
-        }
-    }
-
     public String getDataMapName() {
         String name = dataMapFile.getName();
         if (!name.endsWith(DATA_MAP_LOCATION_SUFFIX)) {
@@ -282,6 +227,9 @@ public class DbImportConfiguration {
         }
     }
 
+    /**
+     * Returns configuration that should be used for DB import stage when the schema is loaded
from the database.
+     */
     public DbLoaderConfiguration getDbLoaderConfig() {
         return dbLoaderConfiguration;
     }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/30f92797/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DefaultDbImportAction.java
----------------------------------------------------------------------
diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DefaultDbImportAction.java
b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DefaultDbImportAction.java
index 8e8ca9b..eab7c44 100644
--- a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DefaultDbImportAction.java
+++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DefaultDbImportAction.java
@@ -36,6 +36,7 @@ import org.apache.cayenne.dbsync.merge.factory.MergerTokenFactoryProvider;
 import org.apache.cayenne.dbsync.naming.ObjectNameGenerator;
 import org.apache.cayenne.dbsync.reverse.db.DbLoader;
 import org.apache.cayenne.dbsync.reverse.db.DbLoaderConfiguration;
+import org.apache.cayenne.dbsync.reverse.filters.CatalogFilter;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.EntityResolver;
@@ -110,6 +111,32 @@ public class DefaultDbImportAction implements DbImportAction {
         return reverse;
     }
 
+    /**
+     * Flattens many-to-many relationships in the generated model.
+     */
+    protected static void flattenManyToManyRelationships(DataMap map, Collection<ObjEntity>
loadedObjEntities,
+                                                         ObjectNameGenerator objectNameGenerator)
{
+        if (loadedObjEntities.isEmpty()) {
+            return;
+        }
+        Collection<ObjEntity> entitiesForDelete = new LinkedList<>();
+
+        for (ObjEntity curEntity : loadedObjEntities) {
+            ManyToManyCandidateEntity entity = ManyToManyCandidateEntity.build(curEntity);
+
+            if (entity != null) {
+                entity.optimizeRelationships(objectNameGenerator);
+                entitiesForDelete.add(curEntity);
+            }
+        }
+
+        // remove needed entities
+        for (ObjEntity curDeleteEntity : entitiesForDelete) {
+            map.removeObjEntity(curDeleteEntity.getName(), true);
+        }
+        loadedObjEntities.removeAll(entitiesForDelete);
+    }
+
     @Override
     public void execute(DbImportConfiguration config) throws Exception {
 
@@ -133,16 +160,14 @@ public class DefaultDbImportAction implements DbImportAction {
             return;
         }
 
-        DataMap targetDataMap = loadExistingDataMap(config.getDataMapFile());
+        DataMap targetDataMap = existingTargetMap(config);
         if (targetDataMap == null) {
-
-            hasChanges = true;
-            File file = config.getDataMapFile();
             logger.info("");
             logger.info("Map file does not exist. Loaded db model will be saved into '"
-                    + (file == null ? "null" : file.getAbsolutePath() + "'"));
+                    + (config.getDataMapFile() == null ? "null" : config.getDataMapFile().getAbsolutePath()
+ "'"));
 
-            targetDataMap = config.createDataMap();
+            hasChanges = true;
+            targetDataMap = newTargetDataMap(config);
         }
 
         MergerTokenFactory mergerTokenFactory = mergerTokenFactoryProvider.get(adapter);
@@ -212,11 +237,13 @@ public class DefaultDbImportAction implements DbImportAction {
         return tokens;
     }
 
-    protected DataMap loadExistingDataMap(File dataMapFile) throws IOException {
-        if (dataMapFile != null && dataMapFile.exists() && dataMapFile.canRead())
{
-            DataMap dataMap = mapLoader.loadDataMap(new InputSource(dataMapFile.getCanonicalPath()));
+    protected DataMap existingTargetMap(DbImportConfiguration configuration) throws IOException
{
+
+        File file = configuration.getDataMapFile();
+        if (file != null && file.exists() && file.canRead()) {
+            DataMap dataMap = mapLoader.loadDataMap(new InputSource(file.getCanonicalPath()));
             dataMap.setNamespace(new EntityResolver(Collections.singleton(dataMap)));
-            dataMap.setConfigurationSource(new URLResource(dataMapFile.toURI().toURL()));
+            dataMap.setConfigurationSource(new URLResource(file.toURI().toURL()));
 
             return dataMap;
         }
@@ -224,6 +251,45 @@ public class DefaultDbImportAction implements DbImportAction {
         return null;
     }
 
+    protected DataMap newTargetDataMap(DbImportConfiguration config) throws IOException {
+
+        DataMap dataMap = new DataMap();
+
+        dataMap.setName(config.getDataMapName());
+        dataMap.setConfigurationSource(new URLResource(config.getDataMapFile().toURI().toURL()));
+        dataMap.setNamespace(new EntityResolver(Collections.singleton(dataMap)));
+
+        // update map defaults
+
+        // do not override default package of existing DataMap unless it is
+        // explicitly requested by the plugin caller
+        String defaultPackage = config.getDefaultPackage();
+        if (defaultPackage != null && defaultPackage.length() > 0) {
+            dataMap.setDefaultPackage(defaultPackage);
+        }
+
+        CatalogFilter[] catalogs = config.getDbLoaderConfig().getFiltersConfig().getCatalogs();
+        if (catalogs.length > 0) {
+            // do not override default catalog of existing DataMap unless it is
+            // explicitly requested by the plugin caller, and the provided catalog is
+            // not a pattern
+            String catalog = catalogs[0].name;
+            if (catalog != null && catalog.length() > 0 && catalog.indexOf('%')
< 0) {
+                dataMap.setDefaultCatalog(catalog);
+            }
+
+            // do not override default schema of existing DataMap unless it is
+            // explicitly requested by the plugin caller, and the provided schema is
+            // not a pattern
+            String schema = catalogs[0].schemas[0].name;
+            if (schema != null && schema.length() > 0 && schema.indexOf('%')
< 0) {
+                dataMap.setDefaultSchema(schema);
+            }
+        }
+
+        return dataMap;
+    }
+
     private List<MergerToken> reverse(MergerTokenFactory mergerTokenFactory, Iterable<MergerToken>
mergeTokens)
             throws IOException {
 
@@ -307,30 +373,4 @@ public class DefaultDbImportAction implements DbImportAction {
         loader.load(dataMap, config.getDbLoaderConfig());
         return dataMap;
     }
-
-    /**
-     * Flattens many-to-many relationships in the generated model.
-     */
-    protected static void flattenManyToManyRelationships(DataMap map, Collection<ObjEntity>
loadedObjEntities,
-                                                      ObjectNameGenerator objectNameGenerator)
{
-        if (loadedObjEntities.isEmpty()) {
-            return;
-        }
-        Collection<ObjEntity> entitiesForDelete = new LinkedList<>();
-
-        for (ObjEntity curEntity : loadedObjEntities) {
-            ManyToManyCandidateEntity entity = ManyToManyCandidateEntity.build(curEntity);
-
-            if (entity != null) {
-                entity.optimizeRelationships(objectNameGenerator);
-                entitiesForDelete.add(curEntity);
-            }
-        }
-
-        // remove needed entities
-        for (ObjEntity curDeleteEntity : entitiesForDelete) {
-            map.removeObjEntity(curDeleteEntity.getName(), true);
-        }
-        loadedObjEntities.removeAll(entitiesForDelete);
-    }
 }
\ No newline at end of file


Mime
View raw message