cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject [3/9] cayenne git commit: add logging loader delegate
Date Thu, 11 Dec 2014 10:49:48 GMT
add logging loader delegate


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

Branch: refs/heads/master
Commit: e2734bfcdb1b0f9a1b4422e84a6dbea4311365c4
Parents: 4e08de8
Author: alexkolonitsky <Alex.Kolonitsky@gmail.com>
Authored: Tue Dec 9 16:55:47 2014 +0300
Committer: alexkolonitsky <Alex.Kolonitsky@gmail.com>
Committed: Tue Dec 9 16:55:47 2014 +0300

----------------------------------------------------------------------
 .../org/apache/cayenne/access/DbLoader.java     | 25 ++++---
 .../apache/cayenne/access/DbLoaderDelegate.java | 19 ++++++
 .../access/loader/DefaultDbLoaderDelegate.java  | 11 +++
 .../access/loader/LoggingDbLoaderDelegate.java  | 72 ++++++++++++++++++++
 .../tools/dbimport/DbImportConfiguration.java   |  7 +-
 .../dbimport/DbImportDbLoaderDelegate.java      |  7 +-
 .../modeler/dialog/db/DbLoaderHelper.java       |  3 +-
 .../apache/cayenne/tools/DbImporterMojo.java    |  1 +
 8 files changed, 127 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2734bfc/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoader.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoader.java b/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoader.java
index e1b5a5b..fc8eb2d 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoader.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoader.java
@@ -33,6 +33,7 @@ import java.util.Set;
 import java.util.TreeSet;
 
 import org.apache.cayenne.access.loader.DbLoaderConfiguration;
+import org.apache.cayenne.access.loader.DefaultDbLoaderDelegate;
 import org.apache.cayenne.access.loader.ManyToManyCandidateEntity;
 import org.apache.cayenne.access.loader.filters.EntityFilters;
 import org.apache.cayenne.access.loader.filters.Filter;
@@ -101,7 +102,7 @@ public class DbLoader {
     public DbLoader(Connection connection, DbAdapter adapter, DbLoaderDelegate delegate,
ObjectNameGenerator strategy) {
         this.adapter = adapter;
         this.connection = connection;
-        this.delegate = delegate;
+        this.delegate = delegate == null ? new DefaultDbLoaderDelegate() : delegate;
 
         setNameGenerator(strategy);
     }
@@ -303,10 +304,7 @@ public class DbLoader {
                 }
                 map.addDbEntity(dbEntity);
 
-                // notify delegate
-                if (delegate != null) {
-                    delegate.dbEntityAdded(dbEntity);
-                }
+                delegate.dbEntityAdded(dbEntity);
 
                 // delegate might have thrown this entity out... so check if it is still
                 // around before continuing processing
@@ -512,7 +510,9 @@ public class DbLoader {
                 reverseRelationship.setSourceEntity(fkEntity);
                 reverseRelationship.setTargetEntity(pkEntity);
                 reverseRelationship.setToMany(false);
-                fkEntity.addRelationship(reverseRelationship);
+                if (delegate.dbRelationshipLoaded(fkEntity, reverseRelationship)) {
+                    fkEntity.addRelationship(reverseRelationship);
+                }
 
                 boolean toPK = createAndAppendJoins(exportedKeys, pkEntity, fkEntity, forwardRelationship,
reverseRelationship);
 
@@ -523,7 +523,9 @@ public class DbLoader {
 
                 forwardRelationship.setToMany(!isOneToOne);
                 forwardRelationship.setName(generateName(pkEntity, key, !isOneToOne));
-                pkEntity.addRelationship(forwardRelationship);
+                if (delegate.dbRelationshipLoaded(pkEntity, forwardRelationship)) {
+                    pkEntity.addRelationship(forwardRelationship);
+                }
             }
         }
     }
@@ -562,6 +564,10 @@ public class DbLoader {
         Map<String, Set<ExportedKey>> keys = new HashMap<String, Set<ExportedKey>>();
 
         for (DbEntity dbEntity : tables.values()) {
+            if (!delegate.dbRelationship(dbEntity)) {
+                continue;
+            }
+
             ResultSet rs;
             try {
                 rs = getMetaData().getExportedKeys(dbPath.catalog, dbPath.schema, dbEntity.getName());
@@ -645,10 +651,7 @@ public class DbLoader {
 
     private void fireObjEntitiesAddedEvents(DataMap map) {
         for (ObjEntity curEntity : map.getObjEntities()) {
-            // notify delegate
-            if (delegate != null) {
-                delegate.objEntityAdded(curEntity);
-            }
+            delegate.objEntityAdded(curEntity);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2734bfc/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoaderDelegate.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoaderDelegate.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoaderDelegate.java
index c1e457a..f5833d7 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoaderDelegate.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/DbLoaderDelegate.java
@@ -21,6 +21,7 @@ package org.apache.cayenne.access;
 
 import org.apache.cayenne.CayenneException;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.ObjEntity;
 
 /**
@@ -41,6 +42,24 @@ public interface DbLoaderDelegate {
 
     void dbEntityRemoved(DbEntity entity);
 
+    /**
+     * Called before relationship loading for db-entity
+     * @param entity
+     *
+     * @return true in case you want process relationships for this entity
+     *         false otherwise
+     */
+    boolean dbRelationship(DbEntity entity);
+
+    /**
+     * Called before relationship will be added into db-entity but after it was loaded from
db
+     * @param entity
+     *
+     * @return true in case you want add this relationship into entity
+     *         false otherwise
+     */
+    boolean dbRelationshipLoaded(DbEntity entity, DbRelationship relationship);
+
     void objEntityAdded(ObjEntity entity);
 
     void objEntityRemoved(ObjEntity entity);

http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2734bfc/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DefaultDbLoaderDelegate.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DefaultDbLoaderDelegate.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DefaultDbLoaderDelegate.java
index bc91162..e512e90 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DefaultDbLoaderDelegate.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/DefaultDbLoaderDelegate.java
@@ -21,6 +21,7 @@ package org.apache.cayenne.access.loader;
 import org.apache.cayenne.CayenneException;
 import org.apache.cayenne.access.DbLoaderDelegate;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.ObjEntity;
 
 /**
@@ -44,6 +45,16 @@ public class DefaultDbLoaderDelegate implements DbLoaderDelegate {
     }
 
     @Override
+    public boolean dbRelationship(DbEntity entity) {
+        return true;
+    }
+
+    @Override
+    public boolean dbRelationshipLoaded(DbEntity entity, DbRelationship relationship) {
+        return true;
+    }
+
+    @Override
     public void objEntityAdded(ObjEntity entity) {
 
     }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2734bfc/cayenne-server/src/main/java/org/apache/cayenne/access/loader/LoggingDbLoaderDelegate.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/loader/LoggingDbLoaderDelegate.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/LoggingDbLoaderDelegate.java
new file mode 100644
index 0000000..d5e69f4
--- /dev/null
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/loader/LoggingDbLoaderDelegate.java
@@ -0,0 +1,72 @@
+/*
+ * 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.access.loader;
+
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbRelationship;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.commons.logging.Log;
+
+/**
+ * @since 4.0
+ */
+public class LoggingDbLoaderDelegate extends DefaultDbLoaderDelegate {
+
+    private final Log logger;
+
+    public LoggingDbLoaderDelegate(Log logger) {
+        this.logger = logger;
+    }
+
+    @Override
+    public void dbEntityAdded(DbEntity entity) {
+        logger.info("Add dbEntity: " + entity.getFullyQualifiedName());
+    }
+
+    @Override
+    public void dbEntityRemoved(DbEntity entity) {
+        logger.info("Del dbEntity: " + entity.getFullyQualifiedName());
+    }
+
+    @Override
+    public boolean dbRelationship(DbEntity entity) {
+        logger.info("    Relationships for " + entity.getFullyQualifiedName());
+
+        return true;
+    }
+
+    @Override
+    public boolean dbRelationshipLoaded(DbEntity entity, DbRelationship relationship) {
+        if (logger.isDebugEnabled()) {
+            logger.debug("    Relationship : " + entity.getName() + "; " + relationship.toString());
+        }
+
+        return true;
+    }
+
+    @Override
+    public void objEntityAdded(ObjEntity entity) {
+        logger.info("Add objEntity: " + entity.getName());
+    }
+
+    @Override
+    public void objEntityRemoved(ObjEntity entity) {
+        logger.info("Del objEntity: " + entity.getName());
+    }
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2734bfc/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 35fba65..dff86ae 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
@@ -23,6 +23,7 @@ import org.apache.cayenne.access.DbLoader;
 import org.apache.cayenne.access.loader.DbLoaderConfiguration;
 import org.apache.cayenne.access.DbLoaderDelegate;
 import org.apache.cayenne.access.loader.DefaultDbLoaderDelegate;
+import org.apache.cayenne.access.loader.LoggingDbLoaderDelegate;
 import org.apache.cayenne.access.loader.NameFilter;
 import org.apache.cayenne.access.loader.filters.DbPath;
 import org.apache.cayenne.access.loader.filters.FiltersConfig;
@@ -292,7 +293,11 @@ public class DbImportConfiguration {
     }
 
     public DbLoaderDelegate createLoaderDelegate() {
-        return new DefaultDbLoaderDelegate();
+        if (getLogger() != null) {
+            return new LoggingDbLoaderDelegate(getLogger());
+        } else {
+            return new DefaultDbLoaderDelegate();
+        }
     }
 
     public DbLoaderConfiguration getDbLoaderConfig() {

http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2734bfc/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportDbLoaderDelegate.java
----------------------------------------------------------------------
diff --git a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportDbLoaderDelegate.java
b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportDbLoaderDelegate.java
index af1bf26..99f9bc8 100644
--- a/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportDbLoaderDelegate.java
+++ b/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportDbLoaderDelegate.java
@@ -25,13 +25,14 @@ import java.util.List;
 
 import org.apache.cayenne.CayenneException;
 import org.apache.cayenne.access.DbLoaderDelegate;
+import org.apache.cayenne.access.loader.DefaultDbLoaderDelegate;
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.ObjEntity;
 
 /**
  * @since 4.0
  */
-class DbImportDbLoaderDelegate implements DbLoaderDelegate {
+class DbImportDbLoaderDelegate extends DefaultDbLoaderDelegate {
 
     private final List<DbEntity> addedDbEntities;
     private final List<DbEntity> removedDbEntities;
@@ -45,10 +46,6 @@ class DbImportDbLoaderDelegate implements DbLoaderDelegate {
         removedObjEntities = new ArrayList<ObjEntity>();
     }
 
-    public boolean overwriteDbEntity(DbEntity ent) throws CayenneException {
-        return false;
-    }
-
     public void dbEntityAdded(DbEntity ent) {
         ent.getDataMap().addDbEntity(ent);
         addedDbEntities.add(ent);

http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2734bfc/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 64fc8c6..d9e01d1 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
@@ -35,6 +35,7 @@ import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.access.DbLoader;
 import org.apache.cayenne.access.DbLoaderDelegate;
 import org.apache.cayenne.access.loader.DbLoaderConfiguration;
+import org.apache.cayenne.access.loader.DefaultDbLoaderDelegate;
 import org.apache.cayenne.access.loader.filters.EntityFilters;
 import org.apache.cayenne.access.loader.filters.FilterFactory;
 import org.apache.cayenne.access.loader.filters.FiltersConfig;
@@ -212,7 +213,7 @@ public class DbLoaderHelper {
         }
     }
 
-    final class LoaderDelegate implements DbLoaderDelegate {
+    private final class LoaderDelegate extends DefaultDbLoaderDelegate {
 
         public boolean overwriteDbEntity(DbEntity ent) throws CayenneException {
             checkCanceled();

http://git-wip-us.apache.org/repos/asf/cayenne/blob/e2734bfc/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
----------------------------------------------------------------------
diff --git a/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
b/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
index 4fcf401..cf5b59d 100644
--- a/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
+++ b/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
@@ -242,6 +242,7 @@ public class DbImporterMojo extends AbstractMojo {
         Log logger = new MavenLogger(this);
 
         DbImportConfiguration config = toParameters();
+        config.setLogger(logger);
         Injector injector = DIBootstrap.createInjector(new ToolsModule(logger), new DbImportModule());
 
         try {


Mime
View raw message