cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject cayenne git commit: CAY-2296 Crypto: Get java type for DbAttribute bound to ObjAttributes with the same type
Date Thu, 04 May 2017 13:43:21 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 2e0acfef6 -> 1c73a0463


CAY-2296 Crypto: Get java type for DbAttribute bound to ObjAttributes
with the same type


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

Branch: refs/heads/master
Commit: 1c73a0463401b5c1d10bec4425ae02a9e90c3cce
Parents: 2e0acfe
Author: Nikita Timofeev <stariy95@gmail.com>
Authored: Thu May 4 16:43:16 2017 +0300
Committer: Nikita Timofeev <stariy95@gmail.com>
Committed: Thu May 4 16:43:16 2017 +0300

----------------------------------------------------------------------
 cayenne-crypto/pom.xml                          |  9 ++--
 .../value/DefaultValueTransformerFactory.java   | 32 ++++++++------
 .../org/apache/cayenne/crypto/db/Table5.java    |  9 ++++
 .../org/apache/cayenne/crypto/db/Table6.java    |  9 ++++
 .../apache/cayenne/crypto/db/auto/_Table5.java  | 46 ++++++++++++++++++++
 .../apache/cayenne/crypto/db/auto/_Table6.java  | 37 ++++++++++++++++
 .../value/DefaultValueTransformerFactoryIT.java | 21 +++++++++
 .../src/test/resources/datamap.map.xml          | 16 +++++++
 docs/doc/src/main/resources/RELEASE-NOTES.txt   |  1 +
 9 files changed, 161 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/1c73a046/cayenne-crypto/pom.xml
----------------------------------------------------------------------
diff --git a/cayenne-crypto/pom.xml b/cayenne-crypto/pom.xml
index daa2e1f..4b9eb12 100644
--- a/cayenne-crypto/pom.xml
+++ b/cayenne-crypto/pom.xml
@@ -27,6 +27,10 @@
 			<artifactId>cayenne-server</artifactId>
 			<version>${project.version}</version>
 		</dependency>
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-api</artifactId>
+		</dependency>
 
 		<!-- Test dependencies -->
 		<dependency>
@@ -59,11 +63,6 @@
 		</dependency>
 		<dependency>
     		<groupId>org.slf4j</groupId>
-    		<artifactId>slf4j-api</artifactId>
-    		<scope>test</scope>
-		</dependency>
-		<dependency>
-    		<groupId>org.slf4j</groupId>
     		<artifactId>slf4j-simple</artifactId>
     		<scope>test</scope>
 		</dependency>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/1c73a046/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/DefaultValueTransformerFactory.java
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/DefaultValueTransformerFactory.java
b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/DefaultValueTransformerFactory.java
index cca9619..4086368 100644
--- a/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/DefaultValueTransformerFactory.java
+++ b/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/DefaultValueTransformerFactory.java
@@ -26,6 +26,8 @@ import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.ObjAttribute;
 import org.apache.cayenne.map.ObjEntity;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.security.Key;
 import java.util.Collection;
@@ -43,6 +45,8 @@ import java.util.concurrent.ConcurrentMap;
  */
 public class DefaultValueTransformerFactory implements ValueTransformerFactory {
 
+    private static final Logger logger = LoggerFactory.getLogger(DefaultValueTransformerFactory.class);
+
     public static final String DB_TO_BYTE_CONVERTERS_KEY =
             "org.apache.cayenne.crypto.transformer.value.DefaultValueTransformerFactory.dbToBytes";
 
@@ -148,24 +152,24 @@ public class DefaultValueTransformerFactory implements ValueTransformerFactory
{
 
         DbEntity dbEntity = a.getEntity();
         DataMap dataMap = dbEntity.getDataMap();
-        Collection<ObjEntity> objEntities = dataMap.getMappedEntities(dbEntity);
-
-        if (objEntities.size() != 1) {
-            return TypesMapping.getJavaBySqlType(a.getType());
-        }
-
-        Collection<String> javaTypes = new HashSet<String>();
-        ObjEntity objEntity = objEntities.iterator().next();
-        for (ObjAttribute oa : objEntity.getAttributes()) {
-
-            // TODO: this won't pick up flattened attributes
-            if (a.getName().equals(oa.getDbAttributePath())) {
-                javaTypes.add(oa.getType());
+        Collection<String> javaTypes = new HashSet<>();
+
+        for(ObjEntity objEntity : dataMap.getMappedEntities(dbEntity)) {
+            for (ObjAttribute oa : objEntity.getAttributes()) {
+                // TODO: this won't pick up flattened attributes
+                if (a.getName().equals(oa.getDbAttributePath())) {
+                    javaTypes.add(oa.getType());
+                }
             }
         }
 
         if (javaTypes.size() != 1) {
-            return TypesMapping.getJavaBySqlType(a.getType());
+            String javaType = TypesMapping.getJavaBySqlType(a.getType());
+            String attributeName = dbEntity.getName() + "." + a.getName();
+            String msg = javaTypes.size() > 1 ? "ObjAttributes with different java types"
: "No ObjAttributes";
+            // Warn user about this problem as there is nothing else we can do
+            logger.warn(msg + " bound to DbAttribute '" + attributeName + "', " + javaType
+ " type will be used.");
+            return javaType;
         }
 
         return javaTypes.iterator().next();

http://git-wip-us.apache.org/repos/asf/cayenne/blob/1c73a046/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/Table5.java
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/Table5.java b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/Table5.java
new file mode 100644
index 0000000..8fd74ec
--- /dev/null
+++ b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/Table5.java
@@ -0,0 +1,9 @@
+package org.apache.cayenne.crypto.db;
+
+import org.apache.cayenne.crypto.db.auto._Table5;
+
+public class Table5 extends _Table5 {
+
+    private static final long serialVersionUID = 1L; 
+
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/1c73a046/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/Table6.java
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/Table6.java b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/Table6.java
new file mode 100644
index 0000000..5b37a7b
--- /dev/null
+++ b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/Table6.java
@@ -0,0 +1,9 @@
+package org.apache.cayenne.crypto.db;
+
+import org.apache.cayenne.crypto.db.auto._Table6;
+
+public class Table6 extends _Table6 {
+
+    private static final long serialVersionUID = 1L; 
+
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/1c73a046/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/auto/_Table5.java
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/auto/_Table5.java b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/auto/_Table5.java
new file mode 100644
index 0000000..3f4127f
--- /dev/null
+++ b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/auto/_Table5.java
@@ -0,0 +1,46 @@
+package org.apache.cayenne.crypto.db.auto;
+
+import org.apache.cayenne.CayenneDataObject;
+import org.apache.cayenne.exp.Property;
+
+/**
+ * Class _Table5 was generated by Cayenne.
+ * It is probably a good idea to avoid changing this class manually,
+ * since it may be overwritten next time code is regenerated.
+ * If you need to make any customizations, please use subclass.
+ */
+public abstract class _Table5 extends CayenneDataObject {
+
+    private static final long serialVersionUID = 1L; 
+
+    public static final String ID_PK_COLUMN = "ID";
+
+    public static final Property<Integer> CRYPTO_INT1 = Property.create("cryptoInt1",
Integer.class);
+    public static final Property<Integer> CRYPTO_INT3 = Property.create("cryptoInt3",
Integer.class);
+    public static final Property<Integer> CRYPTO_INT4 = Property.create("cryptoInt4",
Integer.class);
+
+    public void setCryptoInt1(int cryptoInt1) {
+        writeProperty("cryptoInt1", cryptoInt1);
+    }
+    public int getCryptoInt1() {
+        Object value = readProperty("cryptoInt1");
+        return (value != null) ? (Integer) value : 0;
+    }
+
+    public void setCryptoInt3(int cryptoInt3) {
+        writeProperty("cryptoInt3", cryptoInt3);
+    }
+    public int getCryptoInt3() {
+        Object value = readProperty("cryptoInt3");
+        return (value != null) ? (Integer) value : 0;
+    }
+
+    public void setCryptoInt4(int cryptoInt4) {
+        writeProperty("cryptoInt4", cryptoInt4);
+    }
+    public int getCryptoInt4() {
+        Object value = readProperty("cryptoInt4");
+        return (value != null) ? (Integer) value : 0;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/1c73a046/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/auto/_Table6.java
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/auto/_Table6.java b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/auto/_Table6.java
new file mode 100644
index 0000000..cd5df66
--- /dev/null
+++ b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/db/auto/_Table6.java
@@ -0,0 +1,37 @@
+package org.apache.cayenne.crypto.db.auto;
+
+import org.apache.cayenne.CayenneDataObject;
+import org.apache.cayenne.exp.Property;
+
+/**
+ * Class _Table6 was generated by Cayenne.
+ * It is probably a good idea to avoid changing this class manually,
+ * since it may be overwritten next time code is regenerated.
+ * If you need to make any customizations, please use subclass.
+ */
+public abstract class _Table6 extends CayenneDataObject {
+
+    private static final long serialVersionUID = 1L; 
+
+    public static final String ID_PK_COLUMN = "ID";
+
+    public static final Property<Long> CRYPTO_INT1 = Property.create("cryptoInt1",
Long.class);
+    public static final Property<Integer> CRYPTO_INT4 = Property.create("cryptoInt4",
Integer.class);
+
+    public void setCryptoInt1(long cryptoInt1) {
+        writeProperty("cryptoInt1", cryptoInt1);
+    }
+    public long getCryptoInt1() {
+        Object value = readProperty("cryptoInt1");
+        return (value != null) ? (Long) value : 0;
+    }
+
+    public void setCryptoInt4(int cryptoInt4) {
+        writeProperty("cryptoInt4", cryptoInt4);
+    }
+    public int getCryptoInt4() {
+        Object value = readProperty("cryptoInt4");
+        return (value != null) ? (Integer) value : 0;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/1c73a046/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/DefaultValueTransformerFactoryIT.java
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/DefaultValueTransformerFactoryIT.java
b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/DefaultValueTransformerFactoryIT.java
index 9f42758..57db403 100644
--- a/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/DefaultValueTransformerFactoryIT.java
+++ b/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/DefaultValueTransformerFactoryIT.java
@@ -45,6 +45,7 @@ public class DefaultValueTransformerFactoryIT {
     private static DbEntity t1;
     private static DbEntity t2;
     private static DbEntity t3;
+    private static DbEntity t5;
 
     private static Map<String, BytesConverter<?>> dbToBytes, objectToBytes;
 
@@ -56,6 +57,7 @@ public class DefaultValueTransformerFactoryIT {
         t1 = runtime.getChannel().getEntityResolver().getDbEntity("TABLE1");
         t2 = runtime.getChannel().getEntityResolver().getDbEntity("TABLE2");
         t3 = runtime.getChannel().getEntityResolver().getDbEntity("TABLE3");
+        t5 = runtime.getChannel().getEntityResolver().getDbEntity("TABLE5");
 
         dbToBytes = getDefaultDbConverters();
         objectToBytes = getDefaultObjectConverters();
@@ -94,6 +96,25 @@ public class DefaultValueTransformerFactoryIT {
     }
 
     @Test
+    public void testGetAmbiguousJavaType() {
+        // this one have two bound ObjAttributes, warn should be in log
+        DbAttribute a1 = t5.getAttribute("CRYPTO_INT1");
+        assertEquals("java.lang.String", f.getJavaType(a1));
+
+        // this one doesn't have any bindings, warn should be in log
+        DbAttribute a2 = t5.getAttribute("CRYPTO_INT2");
+        assertEquals("byte[]", f.getJavaType(a2));
+
+        // this one have one binding
+        DbAttribute a3 = t5.getAttribute("CRYPTO_INT3");
+        assertEquals("int", f.getJavaType(a3));
+
+        // this one have two bindings but with the same int type
+        DbAttribute a4 = t5.getAttribute("CRYPTO_INT4");
+        assertEquals("int", f.getJavaType(a4));
+    }
+
+    @Test
     public void testCreateEncryptor() {
 
         DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");

http://git-wip-us.apache.org/repos/asf/cayenne/blob/1c73a046/cayenne-crypto/src/test/resources/datamap.map.xml
----------------------------------------------------------------------
diff --git a/cayenne-crypto/src/test/resources/datamap.map.xml b/cayenne-crypto/src/test/resources/datamap.map.xml
index 51c0840..df8c422 100644
--- a/cayenne-crypto/src/test/resources/datamap.map.xml
+++ b/cayenne-crypto/src/test/resources/datamap.map.xml
@@ -25,6 +25,13 @@
 		<db-attribute name="PLAIN_INT" type="INTEGER"/>
 		<db-attribute name="PLAIN_STRING" type="VARCHAR" length="200"/>
 	</db-entity>
+	<db-entity name="TABLE5">
+		<db-attribute name="CRYPTO_INT1" type="CLOB"/>
+		<db-attribute name="CRYPTO_INT2" type="BLOB"/>
+		<db-attribute name="CRYPTO_INT3" type="VARBINARY" length="255"/>
+		<db-attribute name="CRYPTO_INT4" type="BLOB"/>
+		<db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
+	</db-entity>
 	<obj-entity name="Table1" className="org.apache.cayenne.crypto.db.Table1" dbEntityName="TABLE1">
 		<obj-attribute name="cryptoInt" type="int" db-attribute-path="CRYPTO_INT"/>
 		<obj-attribute name="cryptoString" type="java.lang.String" db-attribute-path="CRYPTO_STRING"/>
@@ -42,4 +49,13 @@
 		<obj-attribute name="plainInt" type="int" db-attribute-path="PLAIN_INT"/>
 		<obj-attribute name="plainString" type="java.lang.String" db-attribute-path="PLAIN_STRING"/>
 	</obj-entity>
+	<obj-entity name="Table5" className="org.apache.cayenne.crypto.db.Table5" dbEntityName="TABLE5">
+		<obj-attribute name="cryptoInt1" type="int" db-attribute-path="CRYPTO_INT1"/>
+		<obj-attribute name="cryptoInt3" type="int" db-attribute-path="CRYPTO_INT3"/>
+		<obj-attribute name="cryptoInt4" type="int" db-attribute-path="CRYPTO_INT4"/>
+	</obj-entity>
+	<obj-entity name="Table6" className="org.apache.cayenne.crypto.db.Table6" dbEntityName="TABLE5">
+		<obj-attribute name="cryptoInt1" type="long" db-attribute-path="CRYPTO_INT1"/>
+		<obj-attribute name="cryptoInt4" type="int" db-attribute-path="CRYPTO_INT4"/>
+	</obj-entity>
 </data-map>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/1c73a046/docs/doc/src/main/resources/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/docs/doc/src/main/resources/RELEASE-NOTES.txt b/docs/doc/src/main/resources/RELEASE-NOTES.txt
index 40c9097..b9967ca 100644
--- a/docs/doc/src/main/resources/RELEASE-NOTES.txt
+++ b/docs/doc/src/main/resources/RELEASE-NOTES.txt
@@ -35,6 +35,7 @@ CAY-2274 Modeler: Validate case when dependent PK is marked as “generated”
 CAY-2277 Create ClientRuntime with ClientRuntimeBuilder just like ServerRuntime
 CAY-2278 Extract cayenne-postcommit module from cayenne-lifecycle
 CAY-2280 Switch from commons-logging to slf4j
+CAY-2296 cayenne-crypto: Get java type for DbAttribute bound to ObjAttributes with the same
type
 
 Bug Fixes:
 


Mime
View raw message