cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From abulat...@apache.org
Subject [cayenne] 02/03: CAY-2569 Custom 'Naming Strategy' in Cayenne Modeler
Date Wed, 24 Apr 2019 11:07:45 GMT
This is an automated email from the ASF dual-hosted git repository.

abulatski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git

commit 9fa1eb94cb598459a69256cd61dddfd65d4b32b2
Author: Arseni Bulatski <ancarseni@gmail.com>
AuthorDate: Wed Apr 24 13:57:15 2019 +0300

    CAY-2569 Custom 'Naming Strategy' in Cayenne Modeler
---
 .../tools/utils/CustomObjectNameGenerator.java     |  3 -
 .../cayenne/stubs/CustomObjectNameGenerator.java   | 83 +---------------------
 .../apache/cayenne/tools/DbImporterMojoTest.java   | 35 +++++----
 .../tools/dbimport/testDefaultPackage-pom.xml      |  1 -
 ...tPackage-pom.xml => testNamingStrategy-pom.xml} | 40 +++++------
 .../dbimport/testNamingStrategy.map.xml-result     | 31 ++++++++
 .../cayenne/tools/dbimport/testNamingStrategy.sql  | 22 ++++++
 7 files changed, 94 insertions(+), 121 deletions(-)

diff --git a/cayenne-ant/src/test/java/org/apache/cayenne/tools/utils/CustomObjectNameGenerator.java
b/cayenne-ant/src/test/java/org/apache/cayenne/tools/utils/CustomObjectNameGenerator.java
index 8305c19..4b3336b 100644
--- a/cayenne-ant/src/test/java/org/apache/cayenne/tools/utils/CustomObjectNameGenerator.java
+++ b/cayenne-ant/src/test/java/org/apache/cayenne/tools/utils/CustomObjectNameGenerator.java
@@ -33,9 +33,6 @@ import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.util.Util;
 import org.jvnet.inflector.Noun;
 
-/**
- * @since 4.2
- */
 public class CustomObjectNameGenerator implements ObjectNameGenerator {
 
     private DbEntityNameStemmer dbEntityNameStemmer;
diff --git a/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/stubs/CustomObjectNameGenerator.java
b/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/stubs/CustomObjectNameGenerator.java
index 55f51f6..375cf83 100644
--- a/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/stubs/CustomObjectNameGenerator.java
+++ b/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/stubs/CustomObjectNameGenerator.java
@@ -19,8 +19,6 @@
 
 package org.apache.cayenne.stubs;
 
-import java.util.List;
-import java.util.Locale;
 import java.util.Objects;
 
 import org.apache.cayenne.dbsync.naming.DbEntityNameStemmer;
@@ -28,14 +26,9 @@ import org.apache.cayenne.dbsync.naming.NoStemStemmer;
 import org.apache.cayenne.dbsync.naming.ObjectNameGenerator;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
-import org.apache.cayenne.map.DbJoin;
 import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.util.Util;
-import org.jvnet.inflector.Noun;
 
-/**
- * @since 4.2
- */
 public class CustomObjectNameGenerator implements ObjectNameGenerator {
 
     private DbEntityNameStemmer dbEntityNameStemmer;
@@ -50,91 +43,21 @@ public class CustomObjectNameGenerator implements ObjectNameGenerator
{
 
     @Override
     public String relationshipName(DbRelationship... relationshipChain) {
-
-        if (relationshipChain == null || relationshipChain.length < 1) {
-            throw new IllegalArgumentException("At least on relationship is expected: " +
relationshipChain);
-        }
-
-        // ignore the name of DbRelationship itself (FWIW we may be generating a new name
for it here)...
-        // generate the name based on join semantics...
-
-        String name = isToMany(relationshipChain)
-                ? toManyRelationshipName(relationshipChain)
-                : toOneRelationshipName(relationshipChain);
-
-        return Util.underscoredToJava(name, false);
-    }
-
-    protected boolean isToMany(DbRelationship... relationshipChain) {
-
-        for (DbRelationship r : relationshipChain) {
-            if (r.isToMany()) {
-                return true;
-            }
-        }
-
-        return false;
+        return null;
     }
 
     protected String stemmed(String dbEntityName) {
         return dbEntityNameStemmer.stem(Objects.requireNonNull(dbEntityName));
     }
 
-    protected String toManyRelationshipName(DbRelationship... relationshipChain) {
-
-        DbRelationship last = relationshipChain[relationshipChain.length - 1];
-
-        String baseName = stemmed(last.getTargetEntityName());
-
-        try {
-            // by default we use English rules here...
-            return Noun.pluralOf(baseName.toLowerCase(), Locale.ENGLISH);
-        } catch (Exception inflectorError) {
-            //  seems that Inflector cannot be trusted. For instance, it
-            // throws an exception when invoked for word "ADDRESS" (although
-            // lower case works fine). To feel safe, we use superclass'
-            // behavior if something's gone wrong
-            return baseName;
-        }
-    }
-
-    protected String toOneRelationshipName(DbRelationship... relationshipChain) {
-
-        DbRelationship first = relationshipChain[0];
-        DbRelationship last = relationshipChain[relationshipChain.length - 1];
-
-        List<DbJoin> joins = first.getJoins();
-        if (joins.isEmpty()) {
-            // In case, when uses EditRelationship button, relationship doesn't exist =>
it doesn't have joins
-            // and just return targetName
-            return stemmed(last.getTargetEntityName());
-        }
-
-        DbJoin join1 = joins.get(0);
-
-        // TODO: multi-join relationships
-
-        // return the name of the FK column sans ID
-        String fkColName = join1.getSourceName();
-        if (fkColName == null) {
-            return stemmed(last.getTargetEntityName());
-        } else if (fkColName.toUpperCase().endsWith("_ID") && fkColName.length()
> 3) {
-            return fkColName.substring(0, fkColName.length() - 3);
-        } else if (fkColName.toUpperCase().endsWith("ID") && fkColName.length() >
2) {
-            return fkColName.substring(0, fkColName.length() - 2);
-        } else {
-            return stemmed(last.getTargetEntityName());
-        }
-    }
-
     @Override
     public String objEntityName(DbEntity dbEntity) {
         String baseName = stemmed(dbEntity.getName());
-        return Util.underscoredToJava(baseName, true);
+        return "Custom" + Util.underscoredToJava(baseName, true);
     }
 
     @Override
     public String objAttributeName(DbAttribute attr) {
-        return Util.underscoredToJava(attr.getName(), false);
+        return "custom_" + Util.underscoredToJava(attr.getName(), false);
     }
 }
diff --git a/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
b/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
index dd0e81a..a836910 100644
--- a/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
+++ b/maven-plugins/cayenne-maven-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
@@ -18,6 +18,18 @@
  ****************************************************************/
 package org.apache.cayenne.tools;
 
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.URL;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Iterator;
+import java.util.Objects;
+
 import org.apache.cayenne.dbsync.reverse.dbimport.Catalog;
 import org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfiguration;
 import org.apache.cayenne.dbsync.reverse.dbimport.IncludeTable;
@@ -26,12 +38,11 @@ import org.apache.cayenne.test.jdbc.SQLReader;
 import org.apache.cayenne.test.resource.ResourceUtil;
 import org.apache.maven.execution.DefaultMavenExecutionRequest;
 import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.ProjectBuilder;
 import org.apache.maven.project.ProjectBuildingRequest;
-import org.slf4j.Logger;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.codehaus.plexus.util.FileUtils;
 import org.custommonkey.xmlunit.DetailedDiff;
 import org.custommonkey.xmlunit.Diff;
@@ -41,18 +52,7 @@ import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
-
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.net.URL;
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.Iterator;
-import java.util.Objects;
+import org.slf4j.Logger;
 
 import static org.apache.cayenne.util.Util.isBlank;
 import static org.mockito.Mockito.mock;
@@ -178,6 +178,11 @@ public class DbImporterMojoTest extends AbstractMojoTestCase {
         test("testExcludeRelationshipFirst");
     }
 
+    @Test
+    public void testNamingStrategy() throws Exception {
+        test("testNamingStrategy");
+    }
+
     /**
      * Q: what happens if an attribute or relationship is unmapped in the object layer, but
then the underlying table
      * changes.
diff --git a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testDefaultPackage-pom.xml
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testDefaultPackage-pom.xml
index f8bcebb..b4bc513 100644
--- a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testDefaultPackage-pom.xml
+++ b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testDefaultPackage-pom.xml
@@ -42,7 +42,6 @@
                     <dbimport>
                         <defaultPackage>com.example.test</defaultPackage>
                         <schema>SCHEMA_01</schema>
-                        <namingStrategy>org.apache.cayenne.stubs.CustomObjectNameGenerator</namingStrategy>
                     </dbimport>
                     <project implementation="org.apache.cayenne.stubs.CayenneProjectStub"/>
 
diff --git a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testDefaultPackage-pom.xml
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testNamingStrategy-pom.xml
similarity index 55%
copy from maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testDefaultPackage-pom.xml
copy to maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testNamingStrategy-pom.xml
index f8bcebb..0cbf06b 100644
--- a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testDefaultPackage-pom.xml
+++ b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testNamingStrategy-pom.xml
@@ -1,22 +1,22 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!--
-  ~ 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.
-  -->
+	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.
+-->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 	http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -32,16 +32,12 @@
             <plugin>
                 <artifactId>cayenne-maven-plugin</artifactId>
                 <configuration>
-                    <map>target/test-classes/org/apache/cayenne/tools/dbimport/testDefaultPackage.map.xml</map>
-
+                    <map>target/test-classes/org/apache/cayenne/tools/dbimport/testNamingStrategy.map.xml</map>
                     <dataSource>
                         <driver>org.apache.derby.jdbc.EmbeddedDriver</driver>
                         <url>jdbc:derby:memory:DbImporterMojoTest;create=true</url>
                     </dataSource>
-
                     <dbimport>
-                        <defaultPackage>com.example.test</defaultPackage>
-                        <schema>SCHEMA_01</schema>
                         <namingStrategy>org.apache.cayenne.stubs.CustomObjectNameGenerator</namingStrategy>
                     </dbimport>
                     <project implementation="org.apache.cayenne.stubs.CayenneProjectStub"/>
diff --git a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testNamingStrategy.map.xml-result
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testNamingStrategy.map.xml-result
new file mode 100644
index 0000000..e5f3c2a
--- /dev/null
+++ b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testNamingStrategy.map.xml-result
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+	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.
+-->
+<data-map xmlns="http://cayenne.apache.org/schema/10/modelMap"
+	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	 xsi:schemaLocation="http://cayenne.apache.org/schema/10/modelMap http://cayenne.apache.org/schema/10/modelMap.xsd"
+	 project-version="10">
+	<db-entity name="TEST_TABLE" schema="APP">
+		<db-attribute name="COL1" type="INTEGER" isPrimaryKey="true" isMandatory="true" length="10"/>
+		<db-attribute name="COL2" type="CHAR" length="25"/>
+	</db-entity>
+	<obj-entity name="CustomTestTable" className="CustomTestTable" dbEntityName="TEST_TABLE">
+	    <obj-attribute name="custom_col2" type="java.lang.String" db-attribute-path="COL2"/>
+	</obj-entity>
+</data-map>
diff --git a/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testNamingStrategy.sql
b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testNamingStrategy.sql
new file mode 100644
index 0000000..11d3433
--- /dev/null
+++ b/maven-plugins/cayenne-maven-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testNamingStrategy.sql
@@ -0,0 +1,22 @@
+--  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.
+
+CREATE TABLE TEST_TABLE (
+  COL1 INTEGER NOT NULL,
+  COL2 CHAR(25),
+  PRIMARY KEY (COL1)
+)
\ No newline at end of file


Mime
View raw message