cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject [2/2] cayenne git commit: CAY-2115 DbLoader - allow loading DataMap without Obj layer
Date Mon, 03 Oct 2016 17:56:12 GMT
CAY-2115 DbLoader - allow loading DataMap without Obj layer

* Removing DbImportAction code fork for a new DataMap that bypassed tokens.
  Now we are using merge tokens to merge and to create new DataMaps all the same


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

Branch: refs/heads/master
Commit: 1d81cf6123f13bc31974e838130b8a691ebf5fb1
Parents: 616ec62
Author: Andrus Adamchik <andrus@objectstyle.com>
Authored: Mon Oct 3 18:49:03 2016 +0300
Committer: Andrus Adamchik <andrus@objectstyle.com>
Committed: Mon Oct 3 20:55:17 2016 +0300

----------------------------------------------------------------------
 .../tools/dbimport/DbImportConfiguration.java   |   8 +-
 .../tools/dbimport/DefaultDbImportAction.java   | 107 +++++++++++--------
 .../dbimport/DefaultDbImportActionTest.java     |  20 ++--
 3 files changed, 75 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/1d81cf61/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 eda833c..eae0dd1 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
@@ -219,10 +219,12 @@ public class DbImportConfiguration {
             throw new NullPointerException("Null DataMap File.");
         }
 
-        return initializeDataMap(new DataMap());
+        DataMap dataMap = new DataMap();
+        initializeDataMap(dataMap);
+        return  dataMap;
     }
 
-    public DataMap initializeDataMap(DataMap dataMap) throws MalformedURLException {
+    public void initializeDataMap(DataMap dataMap) throws MalformedURLException {
         dataMap.setName(getDataMapName());
         dataMap.setConfigurationSource(new URLResource(dataMapFile.toURI().toURL()));
         dataMap.setNamespace(new EntityResolver(Collections.singleton(dataMap)));
@@ -254,8 +256,6 @@ public class DbImportConfiguration {
                 dataMap.setDefaultSchema(schema);
             }
         }
-
-        return dataMap;
     }
 
     public String getDataMapName() {

http://git-wip-us.apache.org/repos/asf/cayenne/blob/1d81cf61/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 ab20b4e..1211e64 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
@@ -61,8 +61,6 @@ import java.util.Comparator;
 import java.util.LinkedList;
 import java.util.List;
 
-import static org.apache.commons.lang.StringUtils.isBlank;
-
 /**
  * A thin wrapper around {@link DbLoader} that encapsulates DB import logic for
  * the benefit of Ant and Maven db importers.
@@ -119,6 +117,7 @@ public class DefaultDbImportAction implements DbImportAction {
             logger.debug(config);
         }
 
+        boolean hasChanges = false;
         DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
         DataSource dataSource = dataSourceFactory.getDataSource(dataNodeDescriptor);
         DbAdapter adapter = adapterFactory.createAdapter(dataNodeDescriptor, dataSource);
@@ -133,54 +132,52 @@ public class DefaultDbImportAction implements DbImportAction {
             return;
         }
 
-        DataMap existing = loadExistingDataMap(config.getDataMapFile());
-        if (existing == null) {
-            logger.info("");
+        DataMap targetDataMap = loadExistingDataMap(config.getDataMapFile());
+        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() + "'"));
 
-            saveLoaded(config.initializeDataMap(loadedFomDb));
-        } else {
-            MergerTokenFactory mergerTokenFactory = mergerTokenFactoryProvider.get(adapter);
-
-            DbLoaderConfiguration loaderConfig = config.getDbLoaderConfig();
-            List<MergerToken> mergeTokens = DbMerger.builder(mergerTokenFactory)
-                    .filters(loaderConfig.getFiltersConfig())
-                    .skipPKTokens(loaderConfig.isSkipPrimaryKeyLoading())
-                    .skipRelationshipsTokens(loaderConfig.isSkipRelationshipsLoading())
-                    .build()
-                    .createMergeTokens(existing, loadedFomDb);
-
-            if (mergeTokens.isEmpty()) {
-                logger.info("");
-                logger.info("Detected changes: No changes to import.");
-                return;
-            }
+            targetDataMap = config.createDataMap();
+        }
 
-            if (!isBlank(config.getDefaultPackage())) {
-                existing.setDefaultPackage(config.getDefaultPackage());
-            }
+        MergerTokenFactory mergerTokenFactory = mergerTokenFactoryProvider.get(adapter);
 
-            final Collection<ObjEntity> loadedObjEntities = new LinkedList<>();
+        DbLoaderConfiguration loaderConfig = config.getDbLoaderConfig();
+        List<MergerToken> tokens = DbMerger.builder(mergerTokenFactory)
+                .filters(loaderConfig.getFiltersConfig())
+                .skipPKTokens(loaderConfig.isSkipPrimaryKeyLoading())
+                .skipRelationshipsTokens(loaderConfig.isSkipRelationshipsLoading())
+                .build()
+                .createMergeTokens(targetDataMap, loadedFomDb);
 
-            ModelMergeDelegate delegate = new ProxyModelMergeDelegate(config.createMergeDelegate())
{
-                @Override
-                public void objEntityAdded(ObjEntity ent) {
-                    loadedObjEntities.add(ent);
-                    super.objEntityAdded(ent);
-                }
-            };
+        hasChanges |= syncDataMapProperties(targetDataMap, config);
+        hasChanges |= applyTokens(config.createMergeDelegate(),
+                targetDataMap,
+                log(sort(reverse(mergerTokenFactory, tokens))),
+                config.getNameGenerator());
+
+        if (hasChanges) {
+            saveLoaded(targetDataMap);
+        }
+    }
 
-            DataMap executed = applyTokens(delegate,
-                    existing,
-                    log(sort(reverse(mergerTokenFactory, mergeTokens))),
-                    config.getNameGenerator());
+    private boolean syncDataMapProperties(DataMap targetDataMap, DbImportConfiguration config)
{
 
-            DbLoader.flattenManyToManyRelationships(executed, loadedObjEntities, config.getNameGenerator());
-            relationshipsSanity(executed);
-            saveLoaded(executed);
+        String defaultPackage = config.getDefaultPackage();
+        if (defaultPackage == null || defaultPackage.trim().length() == 0) {
+            return false;
         }
+
+        if (defaultPackage.equals(targetDataMap.getDefaultPackage())) {
+            return false;
+        }
+
+        targetDataMap.setDefaultPackage(defaultPackage);
+        return true;
     }
 
     private void relationshipsSanity(DataMap executed) {
@@ -237,12 +234,28 @@ public class DefaultDbImportAction implements DbImportAction {
         return tokens;
     }
 
-    private DataMap applyTokens(ModelMergeDelegate mergeDelegate,
-                            DataMap dataMap,
-                            Collection<MergerToken> tokens,
-                            ObjectNameGenerator nameGenerator) {
+    private boolean applyTokens(ModelMergeDelegate mergeDelegate,
+                                DataMap targetDataMap,
+                                Collection<MergerToken> tokens,
+                                ObjectNameGenerator nameGenerator) {
+
+        if (tokens.isEmpty()) {
+            logger.info("");
+            logger.info("Detected changes: No changes to import.");
+            return false;
+        }
+
+        final Collection<ObjEntity> loadedObjEntities = new LinkedList<>();
+
+        mergeDelegate = new ProxyModelMergeDelegate(mergeDelegate) {
+            @Override
+            public void objEntityAdded(ObjEntity ent) {
+                loadedObjEntities.add(ent);
+                super.objEntityAdded(ent);
+            }
+        };
 
-        MergerContext mergerContext = MergerContext.builder(dataMap)
+        MergerContext mergerContext = MergerContext.builder(targetDataMap)
                 .delegate(mergeDelegate)
                 .nameGenerator(nameGenerator)
                 .build();
@@ -270,7 +283,9 @@ public class DefaultDbImportAction implements DbImportAction {
             logger.info("Migration Complete Successfully.");
         }
 
-        return dataMap;
+        DbLoader.flattenManyToManyRelationships(targetDataMap, loadedObjEntities, nameGenerator);
+        relationshipsSanity(targetDataMap);
+        return true;
     }
 
     protected void saveLoaded(DataMap dataMap) throws FileNotFoundException {

http://git-wip-us.apache.org/repos/asf/cayenne/blob/1d81cf61/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DefaultDbImportActionTest.java
----------------------------------------------------------------------
diff --git a/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DefaultDbImportActionTest.java
b/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DefaultDbImportActionTest.java
index affe74e..4f118e5 100644
--- a/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DefaultDbImportActionTest.java
+++ b/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DefaultDbImportActionTest.java
@@ -69,6 +69,7 @@ import static org.apache.cayenne.dbsync.merge.builders.ObjectMother.objEntity;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
@@ -118,16 +119,14 @@ public class DefaultDbImportActionTest {
             }
         };
 
-        DbImportConfiguration params = mock(DbImportConfiguration.class);
-        when(params.createLoader(any(DbAdapter.class), any(Connection.class), any(DbLoaderDelegate.class)))
+        DbImportConfiguration config = mock(DbImportConfiguration.class);
+        when(config.createLoader(any(DbAdapter.class), any(Connection.class), any(DbLoaderDelegate.class)))
                 .thenReturn(dbLoader);
 
-        when(params.createDataMap()).thenReturn(new DataMap("testImport"));
-        when(params.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
-        when(params.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
-
-        final DataMap DATA_MAP = new DataMap();
-        when(params.initializeDataMap(any(DataMap.class))).thenReturn(DATA_MAP);
+        final DataMap dataMap = new DataMap();
+        when(config.createDataMap()).thenReturn(dataMap);
+        when(config.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
+        when(config.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
 
         final boolean[] haveWeTriedToSave = {false};
         DefaultDbImportAction action = buildDbImportAction(new FileProjectSaver() {
@@ -136,11 +135,11 @@ public class DefaultDbImportActionTest {
                 haveWeTriedToSave[0] = true;
 
                 // Validation phase
-                assertEquals(DATA_MAP, project.getRootNode());
+                assertSame(dataMap, project.getRootNode());
             }
         }, null);
 
-        action.execute(params);
+        action.execute(config);
 
         assertTrue("We should try to save.", haveWeTriedToSave[0]);
     }
@@ -248,6 +247,7 @@ public class DefaultDbImportActionTest {
 
         action.execute(params);
 
+        // no changes - we still
         verify(projectSaver, never()).save(any(Project.class));
         verify(mapLoader, times(1)).loadDataMap(any(InputSource.class));
     }


Mime
View raw message