You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by nt...@apache.org on 2018/12/29 08:16:23 UTC

cayenne git commit: CAY-2467 New type-aware Property API - update embeddable templates

Repository: cayenne
Updated Branches:
  refs/heads/master 2cca2445f -> cf11a7ef3


CAY-2467 New type-aware Property API
  - update embeddable templates


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

Branch: refs/heads/master
Commit: cf11a7ef3e335f9e0eacaace48b41786b3aab5dc
Parents: 2cca244
Author: Nikita Timofeev <st...@gmail.com>
Authored: Sat Dec 29 11:16:15 2018 +0300
Committer: Nikita Timofeev <st...@gmail.com>
Committed: Sat Dec 29 11:16:15 2018 +0300

----------------------------------------------------------------------
 .../org/apache/cayenne/gen/PropertyUtils.java   | 104 ++++++++++++++-----
 .../templates/v4_1/embeddable-singleclass.vm    |  12 +--
 .../templates/v4_1/embeddable-superclass.vm     |  12 +--
 .../org/apache/cayenne/access/EmbeddingIT.java  |  16 ++-
 .../testdo/embeddable/auto/_EmbedEntity1.java   |   9 +-
 .../testdo/embeddable/auto/_Embeddable1.java    |   6 +-
 6 files changed, 94 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/cf11a7ef/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 d79d502..061dd67 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
@@ -20,6 +20,7 @@
 package org.apache.cayenne.gen;
 
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -38,6 +39,9 @@ import org.apache.cayenne.exp.property.SetProperty;
 import org.apache.cayenne.exp.property.StringProperty;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.Embeddable;
+import org.apache.cayenne.map.EmbeddableAttribute;
+import org.apache.cayenne.map.EmbeddedAttribute;
 import org.apache.cayenne.map.ObjAttribute;
 import org.apache.cayenne.map.ObjRelationship;
 
@@ -81,8 +85,9 @@ public class PropertyUtils {
 
         for(DbAttribute attribute : entity.getPrimaryKeys()) {
             if(!entityUtils.declaresDbAttribute(attribute)) {
-                importUtils.addType(TypesMapping.getJavaBySqlType(attribute.getType()));
-                importUtils.addType(getPropertyTypeForAttribute(attribute));
+                String javaBySqlType = TypesMapping.getJavaBySqlType(attribute.getType());
+                importUtils.addType(javaBySqlType);
+                importUtils.addType(getPropertyTypeForType(javaBySqlType));
                 needToCreatePK = true;
             }
         }
@@ -93,12 +98,26 @@ public class PropertyUtils {
         }
     }
 
-    public void addImport(ObjAttribute attribute) {
+    public void addImport(ObjAttribute attribute) throws ClassNotFoundException {
+        if(attribute instanceof EmbeddedAttribute) {
+            addImport((EmbeddedAttribute)attribute);
+            return;
+        }
         importUtils.addType(PropertyFactory.class.getName());
         importUtils.addType(attribute.getType());
         importUtils.addType(getPropertyTypeForAttribute(attribute));
     }
 
+    public void addImport(EmbeddedAttribute attribute) throws ClassNotFoundException {
+        Embeddable embeddable = attribute.getEmbeddable();
+        importUtils.addType(embeddable.getClassName());
+        for(EmbeddableAttribute embeddableAttribute : embeddable.getAttributes()) {
+            importUtils.addType(embeddableAttribute.getType());
+            importUtils.addType(getPropertyTypeForType(embeddableAttribute.getType()));
+            importUtils.addType(ExpressionFactory.class.getName());
+        }
+    }
+
     public void addImport(ObjRelationship relationship) {
         addImport(relationship, false);
     }
@@ -123,7 +142,7 @@ public class PropertyUtils {
         StringUtils utils = StringUtils.getInstance();
 
         String attributeType = TypesMapping.getJavaBySqlType(attribute.getType());
-        String propertyType = getPropertyTypeForAttribute(attribute);
+        String propertyType = getPropertyTypeForType(TypesMapping.getJavaBySqlType(attribute.getType()));
         String propertyFactoryMethod = factoryMethodForPropertyType(propertyType);
         attributeType = importUtils.formatJavaType(attributeType, false);
 
@@ -137,29 +156,11 @@ 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();
-        }
-
-        if (CharSequence.class.isAssignableFrom(javaClass)) {
-            return StringProperty.class.getName();
+    public String propertyDefinition(ObjAttribute attribute, boolean client) throws ClassNotFoundException {
+        if(attribute instanceof EmbeddedAttribute) {
+            return propertyDefinition((EmbeddedAttribute)attribute);
         }
 
-        if (JAVA_DATE_TYPES.contains(javaClass)) {
-            return DateProperty.class.getName();
-        }
-
-        return BaseProperty.class.getName();
-    }
-
-    public String propertyDefinition(ObjAttribute attribute, boolean client) {
         StringUtils utils = StringUtils.getInstance();
 
         String propertyType = getPropertyTypeForAttribute(attribute);
@@ -176,10 +177,40 @@ public class PropertyUtils {
         );
     }
 
-    public String propertyDefinition(ObjAttribute attribute) {
+    public String propertyDefinition(ObjAttribute attribute) throws ClassNotFoundException {
         return propertyDefinition(attribute, false);
     }
 
+    public String propertyDefinition(EmbeddedAttribute attribute) throws ClassNotFoundException {
+        StringUtils utils = StringUtils.getInstance();
+
+        Embeddable embeddable = attribute.getEmbeddable();
+        Collection<EmbeddableAttribute> attributes = embeddable.getAttributes();
+
+        String[] attributesDefinitions = new String[attributes.size()];
+        int i = 0;
+        for(EmbeddableAttribute embeddableAttribute : attributes) {
+            String propertyType = getPropertyTypeForType(embeddableAttribute.getType());
+            String propertyFactoryMethod = factoryMethodForPropertyType(propertyType);
+            String attributeType = utils.stripGeneric(importUtils.formatJavaType(embeddableAttribute.getType(), false));
+            String path = attribute.getAttributeOverrides()
+                    .getOrDefault(embeddableAttribute.getName(), embeddableAttribute.getDbAttributeName());
+
+            String propertyName = utils.capitalizedAsConstant(attribute.getName()) + "_" + utils.capitalizedAsConstant(embeddableAttribute.getName());
+            attributesDefinitions[i++] =  String.format("public static final %s<%s> %s " +
+                            "= PropertyFactory.%s(ExpressionFactory.dbPathExp(\"%s\"), %s.class);",
+                    importUtils.formatJavaType(propertyType),
+                    attributeType,
+                    propertyName,
+                    propertyFactoryMethod,
+                    path,
+                    attributeType
+            );
+        }
+
+        return String.join("\n    ", attributesDefinitions);
+    }
+
     public String propertyDefinition(ObjRelationship relationship, boolean client) {
         if (relationship.isToMany()) {
             return toManyRelationshipDefinition(relationship, client);
@@ -268,6 +299,27 @@ public class PropertyUtils {
         return FACTORY_METHODS.get(propertyType);
     }
 
+    private String getPropertyTypeForType(String attributeType) throws ClassNotFoundException {
+        if(TypesMapping.JAVA_BYTES.equals(attributeType)) {
+            return BaseProperty.class.getName();
+        }
+
+        Class<?> javaClass = Class.forName(attributeType);
+        if (Number.class.isAssignableFrom(javaClass)) {
+            return NumericProperty.class.getName();
+        }
+
+        if (CharSequence.class.isAssignableFrom(javaClass)) {
+            return StringProperty.class.getName();
+        }
+
+        if (JAVA_DATE_TYPES.contains(javaClass)) {
+            return DateProperty.class.getName();
+        }
+
+        return BaseProperty.class.getName();
+    }
+
     private String getPropertyTypeForJavaClass(ObjRelationship relationship) {
         if (relationship.isToMany()) {
             String collectionType = relationship.getCollectionType();

http://git-wip-us.apache.org/repos/asf/cayenne/blob/cf11a7ef/cayenne-cgen/src/main/resources/templates/v4_1/embeddable-singleclass.vm
----------------------------------------------------------------------
diff --git a/cayenne-cgen/src/main/resources/templates/v4_1/embeddable-singleclass.vm b/cayenne-cgen/src/main/resources/templates/v4_1/embeddable-singleclass.vm
index a30dcec..3e003bf 100644
--- a/cayenne-cgen/src/main/resources/templates/v4_1/embeddable-singleclass.vm
+++ b/cayenne-cgen/src/main/resources/templates/v4_1/embeddable-singleclass.vm
@@ -38,15 +38,13 @@ ${importUtils.setPackage($subPackageName)}##
 ${importUtils.addReservedType("${subPackageName}.${subClassName}")}##
 ${importUtils.addType("${basePackageName}.${baseClassName}")}##
 ${importUtils.addType("org.apache.cayenne.Persistent")}##
-#if(${object.Attributes} && !${object.Attributes.isEmpty()})
-${importUtils.addType('org.apache.cayenne.exp.Property')}##
-#end
 #foreach( $attr in ${object.Attributes} )
 $importUtils.addType(${attr.Type})##
 #end
 ${importUtils.generate()}
 
-public abstract class ${subClassName} extends ${baseClassName} {
+##  extends ${baseClassName} - always Object, so skip it
+public abstract class ${subClassName} {
 
 ## Create property names
 #if( $createPropertyNames )
@@ -55,12 +53,6 @@ public abstract class ${subClassName} extends ${baseClassName} {
 #end
 
 #end
-## Create Properties
-#foreach( $attr in ${object.Attributes} )
-    #set ( $type = "$importUtils.formatJavaType(${attr.Type}, false)" )
-    public static final Property<$type> ${stringUtils.capitalizedAsConstant($attr.Name)} = Property.create("${attr.Name}", ${stringUtils.stripGeneric($type)}.class);
-#end
-
     // special properties injected by Cayenne
     private Persistent owner;
     private String embeddedProperty;

http://git-wip-us.apache.org/repos/asf/cayenne/blob/cf11a7ef/cayenne-cgen/src/main/resources/templates/v4_1/embeddable-superclass.vm
----------------------------------------------------------------------
diff --git a/cayenne-cgen/src/main/resources/templates/v4_1/embeddable-superclass.vm b/cayenne-cgen/src/main/resources/templates/v4_1/embeddable-superclass.vm
index 95e39dc..0e8c4ce 100644
--- a/cayenne-cgen/src/main/resources/templates/v4_1/embeddable-superclass.vm
+++ b/cayenne-cgen/src/main/resources/templates/v4_1/embeddable-superclass.vm
@@ -38,9 +38,6 @@ ${importUtils.setPackage($superPackageName)}##
 ${importUtils.addReservedType("${superPackageName}.${superClassName}")}##
 ${importUtils.addType("${basePackageName}.${baseClassName}")}##
 ${importUtils.addType("org.apache.cayenne.Persistent")}##
-#if(${object.Attributes} && !${object.Attributes.isEmpty()})
-${importUtils.addType('org.apache.cayenne.exp.Property')}##
-#end
 #foreach( $attr in ${object.Attributes} )
 $importUtils.addType(${attr.Type})##
 #end
@@ -52,7 +49,8 @@ ${importUtils.generate()}
  * since it may be overwritten next time code is regenerated. 
  * If you need to make any customizations, please use subclass. 
  */
-public abstract class ${superClassName} extends ${baseClassName} {
+## extends ${baseClassName} - always Object, so skip it
+public abstract class ${superClassName} {
 
 ## Create property names
 #if( $createPropertyNames )
@@ -61,12 +59,6 @@ public abstract class ${superClassName} extends ${baseClassName} {
 #end
 
 #end
-## Create Properties
-#foreach( $attr in ${object.Attributes} )
-    #set ( $type = "$importUtils.formatJavaType(${attr.Type}, false)" )
-    public static final Property<$type> ${stringUtils.capitalizedAsConstant($attr.Name)} = Property.create("${attr.Name}", ${stringUtils.stripGeneric($type)}.class);
-#end
-
     // special properties injected by Cayenne
     private Persistent owner;
     private String embeddedProperty;

http://git-wip-us.apache.org/repos/asf/cayenne/blob/cf11a7ef/cayenne-server/src/test/java/org/apache/cayenne/access/EmbeddingIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/EmbeddingIT.java b/cayenne-server/src/test/java/org/apache/cayenne/access/EmbeddingIT.java
index 883bca0..9f7570f 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/access/EmbeddingIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/access/EmbeddingIT.java
@@ -108,17 +108,15 @@ public class EmbeddingIT extends ServerCase {
     }
 
     @Test
-    public void testEmbeddableInWhere() throws Exception {
+    public void testEmbeddablePropertiesInWhere() throws Exception {
         createSelectDataSet();
 
-        Embeddable1 embeddable1 = new Embeddable1();
-        embeddable1.setEmbedded10("e1");
-        embeddable1.setEmbedded20("e2");
-
-        Expression exp = ExpressionFactory.matchDbExp(Embeddable1.EMBEDDED10.getName().toUpperCase(), "e1");
-
-
-        ObjectSelect.query(EmbedEntity1.class).where(exp).select(context);
+        List<EmbedEntity1> result = ObjectSelect.query(EmbedEntity1.class)
+                .where(EmbedEntity1.EMBEDDED1_EMBEDDED10.eq("e1"))
+                .orderBy(EmbedEntity1.EMBEDDED2_EMBEDDED10.asc())
+                .select(context);
+        assertEquals(1, result.size());
+        assertEquals("e1", result.get(0).getEmbedded1().getEmbedded10());
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/cayenne/blob/cf11a7ef/cayenne-server/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_EmbedEntity1.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_EmbedEntity1.java b/cayenne-server/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_EmbedEntity1.java
index 6baf007..53fd122 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_EmbedEntity1.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_EmbedEntity1.java
@@ -6,8 +6,6 @@ import java.io.ObjectOutputStream;
 
 import org.apache.cayenne.BaseDataObject;
 import org.apache.cayenne.exp.ExpressionFactory;
-import org.apache.cayenne.exp.property.BaseProperty;
-import org.apache.cayenne.exp.property.NumericProperty;
 import org.apache.cayenne.exp.property.PropertyFactory;
 import org.apache.cayenne.exp.property.StringProperty;
 import org.apache.cayenne.testdo.embeddable.Embeddable1;
@@ -22,11 +20,12 @@ public abstract class _EmbedEntity1 extends BaseDataObject {
 
     private static final long serialVersionUID = 1L; 
 
-    public static final NumericProperty<Integer> ID_PK_PROPERTY = PropertyFactory.createNumeric(ExpressionFactory.dbPathExp("ID"), Integer.class);
     public static final String ID_PK_COLUMN = "ID";
 
-    public static final BaseProperty<Embeddable1> EMBEDDED1 = PropertyFactory.createBase("embedded1", Embeddable1.class);
-    public static final BaseProperty<Embeddable1> EMBEDDED2 = PropertyFactory.createBase("embedded2", Embeddable1.class);
+    public static final StringProperty<String> EMBEDDED1_EMBEDDED20 = PropertyFactory.createString(ExpressionFactory.dbPathExp("EMBEDDED20"), String.class);
+    public static final StringProperty<String> EMBEDDED1_EMBEDDED10 = PropertyFactory.createString(ExpressionFactory.dbPathExp("EMBEDDED10"), String.class);
+    public static final StringProperty<String> EMBEDDED2_EMBEDDED20 = PropertyFactory.createString(ExpressionFactory.dbPathExp("EMBEDDED40"), String.class);
+    public static final StringProperty<String> EMBEDDED2_EMBEDDED10 = PropertyFactory.createString(ExpressionFactory.dbPathExp("EMBEDDED30"), String.class);
     public static final StringProperty<String> NAME = PropertyFactory.createString("name", String.class);
 
     protected Embeddable1 embedded1;

http://git-wip-us.apache.org/repos/asf/cayenne/blob/cf11a7ef/cayenne-server/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_Embeddable1.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_Embeddable1.java b/cayenne-server/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_Embeddable1.java
index 8008bb0..8e280f4 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_Embeddable1.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/testdo/embeddable/auto/_Embeddable1.java
@@ -1,7 +1,6 @@
 package org.apache.cayenne.testdo.embeddable.auto;
 
 import org.apache.cayenne.Persistent;
-import org.apache.cayenne.exp.Property;
 
 /** 
  * Embeddable class _Embeddable1 was generated by Cayenne.
@@ -9,10 +8,7 @@ import org.apache.cayenne.exp.Property;
  * since it may be overwritten next time code is regenerated. 
  * If you need to make any customizations, please use subclass. 
  */
-public abstract class _Embeddable1 extends Object {
-
-    public static final Property<String> EMBEDDED20 = Property.create("embedded20", String.class);
-    public static final Property<String> EMBEDDED10 = Property.create("embedded10", String.class);
+public abstract class _Embeddable1 {
 
     // special properties injected by Cayenne
     private Persistent owner;