cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [1/7] cayenne git commit: CAY-2467 New type-aware Property API - cgen + templates
Date Thu, 27 Dec 2018 14:37:46 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master a45cf9510 -> 56b64c35e


CAY-2467 New type-aware Property API
  - cgen + templates


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

Branch: refs/heads/master
Commit: 9934fe4601ae40c4a4822eae631fdde2ae0ec4c0
Parents: a45cf95
Author: Nikita Timofeev <stariy95@gmail.com>
Authored: Thu Dec 27 16:43:05 2018 +0300
Committer: Nikita Timofeev <stariy95@gmail.com>
Committed: Thu Dec 27 16:43:05 2018 +0300

----------------------------------------------------------------------
 .../org/apache/cayenne/gen/PropertyUtils.java   | 40 +++++++++++++++-----
 .../resources/templates/v4_1/singleclass.vm     |  2 +-
 .../main/resources/templates/v4_1/superclass.vm |  2 +-
 3 files changed, 32 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/9934fe46/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java
----------------------------------------------------------------------
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java
index 4a92dcd..90f4e89 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java
@@ -24,6 +24,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.cayenne.Persistent;
 import org.apache.cayenne.dba.TypesMapping;
 import org.apache.cayenne.exp.ExpressionFactory;
 import org.apache.cayenne.exp.property.BaseProperty;
@@ -74,12 +75,21 @@ public class PropertyUtils {
         this.importUtils = importUtils;
     }
 
-    public void addImportForPK(DbEntity entity) throws ClassNotFoundException {
-        importUtils.addType(PropertyFactory.class.getName());
-        importUtils.addType(ExpressionFactory.class.getName());
+    public void addImportForPK(EntityUtils entityUtils) throws ClassNotFoundException {
+        DbEntity entity = entityUtils.objEntity.getDbEntity();
+        boolean needToCreatePK = false;
+
         for(DbAttribute attribute : entity.getPrimaryKeys()) {
-            importUtils.addType(TypesMapping.getJavaBySqlType(attribute.getType()));
-            importUtils.addType(getPropertyTypeForAttribute(attribute));
+            if(!entityUtils.declaresDbAttribute(attribute)) {
+                importUtils.addType(TypesMapping.getJavaBySqlType(attribute.getType()));
+                importUtils.addType(getPropertyTypeForAttribute(attribute));
+                needToCreatePK = true;
+            }
+        }
+
+        if(needToCreatePK) {
+            importUtils.addType(PropertyFactory.class.getName());
+            importUtils.addType(ExpressionFactory.class.getName());
         }
     }
 
@@ -91,8 +101,10 @@ public class PropertyUtils {
 
     public void addImport(ObjRelationship relationship) {
         importUtils.addType(PropertyFactory.class.getName());
-        if (relationship.getTargetEntityName() != null) {
+        if (relationship.getTargetEntityName() != null && relationship.getTargetEntity()
!= null) {
             importUtils.addType(relationship.getTargetEntity().getClassName());
+        } else {
+            importUtils.addType(Persistent.class.getName());
         }
         importUtils.addType(getPropertyTypeForJavaClass(relationship));
         if (relationship.isToMany()) {
@@ -120,6 +132,10 @@ public class PropertyUtils {
 
     private String getPropertyTypeForAttribute(DbAttribute attribute) throws ClassNotFoundException
{
         String attributeType = TypesMapping.getJavaBySqlType(attribute.getType());
+        if(TypesMapping.JAVA_BYTES.equals(attributeType)) {
+            return BaseProperty.class.getName();
+        }
+
         Class<?> javaClass = Class.forName(attributeType);
         if (Number.class.isAssignableFrom(javaClass)) {
             return NumericProperty.class.getName();
@@ -175,7 +191,9 @@ public class PropertyUtils {
         String propertyType = getPropertyTypeForJavaClass(relationship);
         String propertyFactoryMethod = factoryMethodForPropertyType(propertyType);
         String mapKeyType = EntityUtils.getMapKeyTypeInternal(relationship);
-        String attributeType = relationship.getTargetEntity().getClassName();
+        String attributeType = relationship.getTargetEntity() == null
+                ? Persistent.class.getSimpleName()
+                : relationship.getTargetEntity().getClassName();
 
         return String.format("public static final %s<%s, %s> %s = PropertyFactory.%s(\"%s\",
%s.class, %s.class);",
                 importUtils.formatJavaType(propertyType),
@@ -194,7 +212,9 @@ public class PropertyUtils {
 
         String propertyType = getPropertyTypeForJavaClass(relationship);
         String propertyFactoryMethod = factoryMethodForPropertyType(propertyType);
-        String entityType = importUtils.formatJavaType(relationship.getTargetEntity().getClassName());
+        String entityType = importUtils.formatJavaType(relationship.getTargetEntity() ==
null
+                ? Persistent.class.getSimpleName()
+                : relationship.getTargetEntity().getClassName());
 
         return String.format("public static final %s<%s> %s = PropertyFactory.%s(\"%s\",
%s.class);",
                 importUtils.formatJavaType(propertyType),
@@ -211,8 +231,8 @@ public class PropertyUtils {
 
         String propertyType = EntityProperty.class.getName();
         String propertyFactoryMethod = "createEntity";
-        String attributeType = relationship.getTargetEntityName() == null
-                ? "Object"
+        String attributeType = (relationship.getTargetEntityName() == null || relationship.getTargetEntity()
== null)
+                ? Persistent.class.getSimpleName()
                 : importUtils.formatJavaType(relationship.getTargetEntity().getClassName());
 
         return String.format("public static final %s<%s> %s = PropertyFactory.%s(\"%s\",
%s.class);",

http://git-wip-us.apache.org/repos/asf/cayenne/blob/9934fe46/cayenne-cgen/src/main/resources/templates/v4_1/singleclass.vm
----------------------------------------------------------------------
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 1d5199f..d993f85 100644
--- a/cayenne-cgen/src/main/resources/templates/v4_1/singleclass.vm
+++ b/cayenne-cgen/src/main/resources/templates/v4_1/singleclass.vm
@@ -40,7 +40,7 @@ ${importUtils.addType("java.io.IOException")}##
 ${importUtils.addType("java.io.ObjectInputStream")}##
 ${importUtils.addType("java.io.ObjectOutputStream")}##
 #if( $createPKProperties )
-$propertyUtils.addImportForPK($object.DbEntity)##
+$propertyUtils.addImportForPK($entityUtils)##
 #end
 #foreach( $attr in ${object.DeclaredAttributes} )
 $propertyUtils.addImport($attr)##

http://git-wip-us.apache.org/repos/asf/cayenne/blob/9934fe46/cayenne-cgen/src/main/resources/templates/v4_1/superclass.vm
----------------------------------------------------------------------
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 96dd580..244ce25 100644
--- a/cayenne-cgen/src/main/resources/templates/v4_1/superclass.vm
+++ b/cayenne-cgen/src/main/resources/templates/v4_1/superclass.vm
@@ -42,7 +42,7 @@ ${importUtils.addType("java.io.IOException")}##
 ${importUtils.addType("java.io.ObjectInputStream")}##
 ${importUtils.addType("java.io.ObjectOutputStream")}##
 #if( $createPKProperties )
-$propertyUtils.addImportForPK($object.DbEntity)##
+$propertyUtils.addImportForPK($entityUtils)##
 #end
 #foreach( $attr in ${object.DeclaredAttributes} )
 $propertyUtils.addImport($attr)##


Mime
View raw message