From 3124d1717f72d39868f13931c815fdafb9c6f0f0 Mon Sep 17 00:00:00 2001 From: kkomyak Date: Mon, 17 Sep 2018 14:54:48 +0300 Subject: [PATCH 1/2] Generate is as properties. --- .../src/main/resources/templates/v4_1/singleclass.vm | 7 ++++++- .../src/main/resources/templates/v4_1/superclass.vm | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cayenne-cgen/src/main/resources/templates/v4_1/singleclass.vm b/cayenne-cgen/src/main/resources/templates/v4_1/singleclass.vm index b9285a893d..e586651bb3 100644 --- a/cayenne-cgen/src/main/resources/templates/v4_1/singleclass.vm +++ b/cayenne-cgen/src/main/resources/templates/v4_1/singleclass.vm @@ -39,6 +39,9 @@ ${importUtils.addType("${basePackageName}.${baseClassName}")}## ${importUtils.addType("java.io.IOException")}## ${importUtils.addType("java.io.ObjectInputStream")}## ${importUtils.addType("java.io.ObjectOutputStream")}## +#if( $object.DbEntity ) +${importUtils.addType("org.apache.cayenne.exp.ExpressionFactory")}## +#end #if((${object.DeclaredAttributes} && !${object.DeclaredAttributes.isEmpty()}) || (${object.DeclaredRelationships} && !${object.DeclaredRelationships.isEmpty()})) ${importUtils.addType('org.apache.cayenne.exp.Property')}## #end @@ -71,7 +74,9 @@ public#if("true" == "${object.isAbstract()}") abstract#end class ${subClassName} #end #if( $object.DbEntity ) #foreach( $idAttr in ${object.DbEntity.PrimaryKeys} ) - public static final String ${stringUtils.capitalizedAsConstant($idAttr.Name)}_PK_COLUMN = "${idAttr.Name}"; + #if(!$object.DeclaredAttributes.toString().contains($idAttr.Name)) + public static final Property ${stringUtils.capitalizedAsConstant($idAttr.Name)}_PK_COLUMN = Property.create(ExpressionFactory.dbPathExp("db:${idAttr.Name}"), Integer.class); + #end #end #end diff --git a/cayenne-cgen/src/main/resources/templates/v4_1/superclass.vm b/cayenne-cgen/src/main/resources/templates/v4_1/superclass.vm index 91152d7df0..ab67534748 100644 --- a/cayenne-cgen/src/main/resources/templates/v4_1/superclass.vm +++ b/cayenne-cgen/src/main/resources/templates/v4_1/superclass.vm @@ -41,6 +41,9 @@ ${importUtils.addType("${basePackageName}.${baseClassName}")}## ${importUtils.addType("java.io.IOException")}## ${importUtils.addType("java.io.ObjectInputStream")}## ${importUtils.addType("java.io.ObjectOutputStream")}## +#if( $object.DbEntity ) +${importUtils.addType("org.apache.cayenne.exp.ExpressionFactory")}## +#end #if((${object.DeclaredAttributes} && !${object.DeclaredAttributes.isEmpty()}) || (${object.DeclaredRelationships} && !${object.DeclaredRelationships.isEmpty()})) ${importUtils.addType('org.apache.cayenne.exp.Property')}## #end @@ -79,7 +82,9 @@ public abstract class ${superClassName} extends ${baseClassName} { #end #if( $object.DbEntity ) #foreach( $idAttr in ${object.DbEntity.PrimaryKeys} ) - public static final String ${stringUtils.capitalizedAsConstant($idAttr.Name)}_PK_COLUMN = "${idAttr.Name}"; + #if(!$object.DeclaredAttributes.toString().contains($idAttr.Name)) + public static final Property ${stringUtils.capitalizedAsConstant($idAttr.Name)}_PK_COLUMN = Property.create(ExpressionFactory.dbPathExp("db:${idAttr.Name}"), Integer.class); + #end #end #end From 9f9cfd4b4390ed506471f365839366e7771e6b80 Mon Sep 17 00:00:00 2001 From: kkomyak Date: Tue, 25 Sep 2018 17:16:53 +0300 Subject: [PATCH 2/2] Added method to EntityUtil which checks is Object entity contain db attribute path. Refactor templates. --- .../org/apache/cayenne/gen/EntityUtils.java | 10 ++++++++++ .../resources/templates/v4_1/singleclass.vm | 2 +- .../resources/templates/v4_1/superclass.vm | 2 +- .../apache/cayenne/gen/EntityUtilsTest.java | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityUtils.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityUtils.java index cd814db365..51eefdc893 100644 --- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityUtils.java +++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityUtils.java @@ -20,6 +20,7 @@ package org.apache.cayenne.gen; import java.util.Collection; +import java.util.Objects; import org.apache.cayenne.CayenneRuntimeException; import org.apache.cayenne.ObjectId; @@ -263,6 +264,15 @@ public String getMapKeyType(final ObjRelationship relationship) { return attribute.getType(); } + /** + * @since 4.1 + * Checks is the db attribute declared for some object attribute. + * @param id - db attribute + */ + public boolean declaresDbAttribute(String id) { + return objEntity.getAttributes().stream().filter(Objects::nonNull).anyMatch(a -> id.equals(a.getDbAttributePath())); + } + /** * @return the list of all callback names registered for the entity. * @since 3.0 diff --git a/cayenne-cgen/src/main/resources/templates/v4_1/singleclass.vm b/cayenne-cgen/src/main/resources/templates/v4_1/singleclass.vm index e586651bb3..d2ce30ae13 100644 --- a/cayenne-cgen/src/main/resources/templates/v4_1/singleclass.vm +++ b/cayenne-cgen/src/main/resources/templates/v4_1/singleclass.vm @@ -74,7 +74,7 @@ public#if("true" == "${object.isAbstract()}") abstract#end class ${subClassName} #end #if( $object.DbEntity ) #foreach( $idAttr in ${object.DbEntity.PrimaryKeys} ) - #if(!$object.DeclaredAttributes.toString().contains($idAttr.Name)) + #if(!${entityUtils.declaresDbAttribute($idAttr.Name)}) public static final Property ${stringUtils.capitalizedAsConstant($idAttr.Name)}_PK_COLUMN = Property.create(ExpressionFactory.dbPathExp("db:${idAttr.Name}"), Integer.class); #end #end diff --git a/cayenne-cgen/src/main/resources/templates/v4_1/superclass.vm b/cayenne-cgen/src/main/resources/templates/v4_1/superclass.vm index ab67534748..df3d656bcd 100644 --- a/cayenne-cgen/src/main/resources/templates/v4_1/superclass.vm +++ b/cayenne-cgen/src/main/resources/templates/v4_1/superclass.vm @@ -82,7 +82,7 @@ public abstract class ${superClassName} extends ${baseClassName} { #end #if( $object.DbEntity ) #foreach( $idAttr in ${object.DbEntity.PrimaryKeys} ) - #if(!$object.DeclaredAttributes.toString().contains($idAttr.Name)) + #if(!${entityUtils.declaresDbAttribute($idAttr.Name)}) public static final Property ${stringUtils.capitalizedAsConstant($idAttr.Name)}_PK_COLUMN = Property.create(ExpressionFactory.dbPathExp("db:${idAttr.Name}"), Integer.class); #end #end diff --git a/cayenne-cgen/src/test/java/org/apache/cayenne/gen/EntityUtilsTest.java b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/EntityUtilsTest.java index 21d7162e08..6bb87ddb38 100644 --- a/cayenne-cgen/src/test/java/org/apache/cayenne/gen/EntityUtilsTest.java +++ b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/EntityUtilsTest.java @@ -21,6 +21,7 @@ import org.apache.cayenne.map.CallbackDescriptor; import org.apache.cayenne.map.DataMap; +import org.apache.cayenne.map.ObjAttribute; import org.apache.cayenne.map.ObjEntity; import org.junit.After; import org.junit.Before; @@ -29,6 +30,7 @@ import java.util.LinkedHashSet; import java.util.Set; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -72,4 +74,21 @@ public void testAllCallbackNamesUnique() throws Exception { assertTrue("Contains duplicate callback names.", hasNoDuplicates); } + + @Test + public void testDeclaresDbAttribute() throws Exception { + + String existKey = "testKey"; + String notExistKey = "testKey1"; + + ObjAttribute attribute = new ObjAttribute(existKey); + attribute.setDbAttributePath(existKey); + objEntity.addAttribute(attribute); + + entityUtils = new EntityUtils(dataMap, objEntity, "TestBaseClass", "TestSuperClass", "TestSubClass"); + + assertTrue(entityUtils.declaresDbAttribute(existKey)); + assertFalse(entityUtils.declaresDbAttribute(notExistKey)); + + } }