Author: aadamchik
Date: Sun Nov 11 14:30:04 2012
New Revision: 1408003
URL: http://svn.apache.org/viewvc?rev=1408003&view=rev
Log:
CAY-1771 cdbimport improvements: usePrimitives flag
basic functionality
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java
cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportParameters.java
cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DbImportActionTest.java
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java?rev=1408003&r1=1408002&r2=1408003&view=diff
==============================================================================
---
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
(original)
+++
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
Sun Nov 11 14:30:04 2012
@@ -21,8 +21,10 @@ package org.apache.cayenne.util;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import org.apache.cayenne.dba.TypesMapping;
import org.apache.cayenne.map.DataMap;
@@ -42,9 +44,23 @@ import org.apache.cayenne.map.naming.Nam
*/
public class EntityMergeSupport {
+ private static final Map<String, String> CLASS_TO_PRIMITVE;
+
+ static {
+ CLASS_TO_PRIMITVE = new HashMap<String, String>();
+ CLASS_TO_PRIMITVE.put(Byte.class.getName(), "byte");
+ CLASS_TO_PRIMITVE.put(Long.class.getName(), "long");
+ CLASS_TO_PRIMITVE.put(Double.class.getName(), "double");
+ CLASS_TO_PRIMITVE.put(Boolean.class.getName(), "boolean");
+ CLASS_TO_PRIMITVE.put(Float.class.getName(), "float");
+ CLASS_TO_PRIMITVE.put(Short.class.getName(), "short");
+ CLASS_TO_PRIMITVE.put(Integer.class.getName(), "int");
+ }
+
protected DataMap map;
protected boolean removeMeaningfulFKs;
protected boolean removeMeaningfulPKs;
+ protected boolean usePrimitives;
/**
* Strategy for choosing names for entities, attributes and relationships
@@ -63,8 +79,7 @@ public class EntityMergeSupport {
/**
* @since 3.0
*/
- public EntityMergeSupport(DataMap map, NamingStrategy namingStrategy,
- boolean removeMeaningfulPKs) {
+ public EntityMergeSupport(DataMap map, NamingStrategy namingStrategy,
boolean removeMeaningfulPKs) {
this.map = map;
this.removeMeaningfulFKs = true;
this.listeners = new ArrayList<EntityMergeListener>();
@@ -72,8 +87,8 @@ public class EntityMergeSupport {
this.namingStrategy = namingStrategy;
/**
- * Adding a listener, so that all created ObjRelationships would have
default
- * delete rule
+ * Adding a listener, so that all created ObjRelationships would have
+ * default delete rule
*/
addEntityMergeListener(DeleteRuleUpdater.getEntityMergeListener());
}
@@ -95,14 +110,14 @@ public class EntityMergeSupport {
return changed;
}
-
+
/**
* @since 3.2
*/
protected boolean removePK(DbEntity dbEntity) {
return removeMeaningfulPKs;
}
-
+
/**
* @since 3.2
*/
@@ -111,8 +126,8 @@ public class EntityMergeSupport {
}
/**
- * Updates ObjEntity attributes and relationships based on the current
state of its
- * DbEntity.
+ * Updates ObjEntity attributes and relationships based on the current
state
+ * of its DbEntity.
*
* @return true if the ObjEntity has changed as a result of
synchronization.
*/
@@ -121,7 +136,7 @@ public class EntityMergeSupport {
if (entity == null) {
return false;
}
-
+
DbEntity dbEntity = entity.getDbEntity();
if (dbEntity == null) {
return false;
@@ -130,13 +145,15 @@ public class EntityMergeSupport {
boolean changed = false;
// synchronization on DataMap is some (weak) protection
- // against simultaneous modification of the map (like double-clicking
on sync
+ // against simultaneous modification of the map (like double-clicking
on
+ // sync
// button)
synchronized (map) {
if (removeFK(dbEntity)) {
- // get rid of attributes that are now src attributes for
relationships
+ // get rid of attributes that are now src attributes for
+ // relationships
for (DbAttribute da : getMeaningfulFKs(entity)) {
ObjAttribute oa = entity.getAttributeForDbAttribute(da);
while (oa != null) {
@@ -153,13 +170,17 @@ public class EntityMergeSupport {
String attrName = namingStrategy.createObjAttributeName(da);
// avoid duplicate names
- attrName = NamedObjectFactory.createName(
- ObjAttribute.class,
- entity,
- attrName);
+ attrName = NamedObjectFactory.createName(ObjAttribute.class,
entity, attrName);
String type = TypesMapping.getJavaBySqlType(da.getType());
+ if (usePrimitives) {
+ String primitive = CLASS_TO_PRIMITVE.get(type);
+ if (primitive != null) {
+ type = primitive;
+ }
+ }
+
ObjAttribute oa = new ObjAttribute(attrName, type, entity);
oa.setDbAttributePath(da.getName());
entity.addAttribute(oa);
@@ -174,12 +195,8 @@ public class EntityMergeSupport {
for (Entity mappedTarget :
map.getMappedEntities(targetEntity)) {
// avoid duplicate names
- String relationshipName = namingStrategy
- .createObjRelationshipName(dr);
- relationshipName = NamedObjectFactory.createName(
- ObjRelationship.class,
- entity,
- relationshipName);
+ String relationshipName =
namingStrategy.createObjRelationshipName(dr);
+ relationshipName =
NamedObjectFactory.createName(ObjRelationship.class, entity, relationshipName);
ObjRelationship or = new ObjRelationship(relationshipName);
or.addDbRelationship(dr);
@@ -217,8 +234,8 @@ public class EntityMergeSupport {
}
/**
- * Returns a list of attributes that exist in the DbEntity, but are
missing from the
- * ObjEntity.
+ * Returns a list of attributes that exist in the DbEntity, but are missing
+ * from the ObjEntity.
*/
protected List<DbAttribute> getAttributesToAdd(ObjEntity objEntity) {
DbEntity dbEntity = objEntity.getDbEntity();
@@ -399,4 +416,19 @@ public class EntityMergeSupport {
public NamingStrategy getNamingStrategy() {
return namingStrategy;
}
+
+ /**
+ * @since 3.2
+ */
+ public boolean isUsePrimitives() {
+ return usePrimitives;
+ }
+
+ /**
+ * @since 3.2
+ * @param usePrimitives
+ */
+ public void setUsePrimitives(boolean usePrimitives) {
+ this.usePrimitives = usePrimitives;
+ }
}
Modified:
cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java?rev=1408003&r1=1408002&r2=1408003&view=diff
==============================================================================
---
cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java
(original)
+++
cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportAction.java
Sun Nov 11 14:30:04 2012
@@ -149,7 +149,7 @@ public class DbImportAction {
return dataMap;
}
- DbLoader createLoader(DbImportParameters parameters, DbAdapter adapter,
Connection connection,
+ DbLoader createLoader(final DbImportParameters parameters, DbAdapter
adapter, Connection connection,
DbLoaderDelegate loaderDelegate) throws InstantiationException,
IllegalAccessException,
ClassNotFoundException {
@@ -168,13 +168,16 @@ public class DbImportAction {
@Override
protected EntityMergeSupport createEntityMerger(DataMap map) {
- return new EntityMergeSupport(map, namingStrategy, true) {
+ EntityMergeSupport emSupport = new EntityMergeSupport(map,
namingStrategy, true) {
@Override
protected boolean removePK(DbEntity dbEntity) {
return
!meaningfulPkFilter.isIncluded(dbEntity.getName());
}
};
+
+ emSupport.setUsePrimitives(parameters.isUsePrimitives());
+ return emSupport;
}
};
Modified:
cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportParameters.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportParameters.java?rev=1408003&r1=1408002&r2=1408003&view=diff
==============================================================================
---
cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportParameters.java
(original)
+++
cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/dbimport/DbImportParameters.java
Sun Nov 11 14:30:04 2012
@@ -106,6 +106,8 @@ public class DbImportParameters {
private String includeTables;
private String excludeTables;
+ private boolean usePrimitives;
+
public File getDataMapFile() {
return dataMapFile;
}
@@ -246,4 +248,12 @@ public class DbImportParameters {
public void setMeaningfulPkTables(String meaningfulPkTables) {
this.meaningfulPkTables = meaningfulPkTables;
}
+
+ public boolean isUsePrimitives() {
+ return usePrimitives;
+ }
+
+ public void setUsePrimitives(boolean usePrimitives) {
+ this.usePrimitives = usePrimitives;
+ }
}
Modified:
cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DbImportActionTest.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DbImportActionTest.java?rev=1408003&r1=1408002&r2=1408003&view=diff
==============================================================================
---
cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DbImportActionTest.java
(original)
+++
cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/tools/dbimport/DbImportActionTest.java
Sun Nov 11 14:30:04 2012
@@ -39,6 +39,7 @@ import org.apache.cayenne.di.Injector;
import org.apache.cayenne.map.DataMap;
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.apache.cayenne.resource.URLResource;
import org.apache.cayenne.tools.configuration.ToolsModule;
@@ -160,7 +161,7 @@ public class DbImportActionTest extends
map.addDbEntity(e1);
entities.add(e1);
-
+
DbEntity a1 = new DbEntity("a1");
DbAttribute apk = new DbAttribute("pk", Types.INTEGER, a1);
apk.setPrimaryKey(true);
@@ -176,13 +177,81 @@ public class DbImportActionTest extends
ObjEntity oe1 = map.getObjEntity("E1");
assertEquals(1, oe1.getAttributes().size());
assertNotNull(oe1.getAttribute("nonPk"));
-
+
ObjEntity oe2 = map.getObjEntity("A1");
assertEquals(2, oe2.getAttributes().size());
assertNotNull(oe2.getAttribute("nonPk"));
assertNotNull(oe2.getAttribute("pk"));
}
+ public void testCreateLoader_UsePrimitives_False() throws Exception {
+ Log log = mock(Log.class);
+ Injector i = DIBootstrap.createInjector(new ToolsModule(log), new
DbImportModule());
+
+ DbImportAction action = i.getInstance(DbImportAction.class);
+
+ DbImportParameters parameters = new DbImportParameters();
+ parameters.setUsePrimitives(false);
+
+ DbLoader loader1 = action.createLoader(parameters,
mock(DbAdapter.class), mock(Connection.class),
+ mock(DbLoaderDelegate.class));
+
+ DataMap map = new DataMap();
+
+ DbEntity e1 = new DbEntity("e1");
+ DbAttribute nonPk = new DbAttribute("nonPk", Types.INTEGER, e1);
+ e1.addAttribute(nonPk);
+
+ map.addDbEntity(e1);
+
+ // DbLoader is so ugly and hard to test..
+ Field dbEntityList = DbLoader.class.getDeclaredField("dbEntityList");
+ dbEntityList.setAccessible(true);
+ List<DbEntity> entities = (List<DbEntity>) dbEntityList.get(loader1);
+ entities.add(e1);
+
+ loader1.loadObjEntities(map);
+
+ ObjEntity oe1 = map.getObjEntity("E1");
+
+ ObjAttribute oa1 = (ObjAttribute) oe1.getAttribute("nonPk");
+ assertEquals("java.lang.Integer", oa1.getType());
+ }
+
+ public void testCreateLoader_UsePrimitives_True() throws Exception {
+ Log log = mock(Log.class);
+ Injector i = DIBootstrap.createInjector(new ToolsModule(log), new
DbImportModule());
+
+ DbImportAction action = i.getInstance(DbImportAction.class);
+
+ DbImportParameters parameters = new DbImportParameters();
+ parameters.setUsePrimitives(true);
+
+ DbLoader loader1 = action.createLoader(parameters,
mock(DbAdapter.class), mock(Connection.class),
+ mock(DbLoaderDelegate.class));
+
+ DataMap map = new DataMap();
+
+ DbEntity e1 = new DbEntity("e1");
+ DbAttribute nonPk = new DbAttribute("nonPk", Types.INTEGER, e1);
+ e1.addAttribute(nonPk);
+
+ map.addDbEntity(e1);
+
+ // DbLoader is so ugly and hard to test..
+ Field dbEntityList = DbLoader.class.getDeclaredField("dbEntityList");
+ dbEntityList.setAccessible(true);
+ List<DbEntity> entities = (List<DbEntity>) dbEntityList.get(loader1);
+ entities.add(e1);
+
+ loader1.loadObjEntities(map);
+
+ ObjEntity oe1 = map.getObjEntity("E1");
+
+ ObjAttribute oa1 = (ObjAttribute) oe1.getAttribute("nonPk");
+ assertEquals("int", oa1.getType());
+ }
+
public void testSaveLoaded() throws Exception {
Log log = mock(Log.class);
Injector i = DIBootstrap.createInjector(new ToolsModule(log), new
DbImportModule());