use of org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator in project cayenne by apache.
the class EntityMergeSupportIT method testMerging.
@Test
public void testMerging() {
DbEntity dbEntity1 = new DbEntity("NEW_TABLE");
DbAttribute e1col1 = new DbAttribute("ID", Types.INTEGER, dbEntity1);
e1col1.setMandatory(true);
e1col1.setPrimaryKey(true);
dbEntity1.addAttribute(e1col1);
DbAttribute e1col2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity1);
e1col2.setMaxLength(10);
e1col2.setMandatory(false);
dbEntity1.addAttribute(e1col2);
map.addDbEntity(dbEntity1);
DbEntity dbEntity2 = new DbEntity("NEW_TABLE2");
DbAttribute e2col1 = new DbAttribute("ID", Types.INTEGER, dbEntity2);
e2col1.setMandatory(true);
e2col1.setPrimaryKey(true);
dbEntity2.addAttribute(e2col1);
DbAttribute e2col2 = new DbAttribute("FK", Types.INTEGER, dbEntity2);
dbEntity2.addAttribute(e2col2);
map.addDbEntity(dbEntity2);
// create db relationships
DbRelationship rel1To2 = new DbRelationship("rel1To2");
rel1To2.setSourceEntity(dbEntity1);
rel1To2.setTargetEntityName(dbEntity2);
rel1To2.setToMany(true);
rel1To2.addJoin(new DbJoin(rel1To2, e1col1.getName(), e2col2.getName()));
dbEntity1.addRelationship(rel1To2);
DbRelationship rel2To1 = new DbRelationship("rel2To1");
rel2To1.setSourceEntity(dbEntity2);
rel2To1.setTargetEntityName(dbEntity1);
rel2To1.setToMany(false);
rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
dbEntity2.addRelationship(rel2To1);
assertSame(rel1To2, rel2To1.getReverseRelationship());
assertSame(rel2To1, rel1To2.getReverseRelationship());
ObjEntity objEntity1 = new ObjEntity("NewTable");
objEntity1.setDbEntity(dbEntity1);
map.addObjEntity(objEntity1);
ObjEntity objEntity2 = new ObjEntity("NewTable2");
objEntity2.setDbEntity(dbEntity2);
map.addObjEntity(objEntity2);
EntityMergeSupport entityMergeSupport = new EntityMergeSupport(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()), NamePatternMatcher.EXCLUDE_ALL, true, true, false);
assertTrue(entityMergeSupport.synchronizeWithDbEntities(Arrays.asList(objEntity1, objEntity2)));
assertNotNull(objEntity1.getAttribute("name"));
assertNotNull(objEntity1.getRelationship("newTable2s"));
assertNotNull(objEntity2.getRelationship("newTable"));
assertEquals(objEntity1.getRelationship("newTable2s").getDeleteRule(), DeleteRule.DEFAULT_DELETE_RULE_TO_MANY);
assertEquals(objEntity2.getRelationship("newTable").getDeleteRule(), DeleteRule.DEFAULT_DELETE_RULE_TO_ONE);
map.removeObjEntity(objEntity2.getName());
map.removeObjEntity(objEntity1.getName());
map.removeDbEntity(dbEntity2.getName());
map.removeDbEntity(dbEntity1.getName());
}
use of org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator in project cayenne by apache.
the class MergeCase method createMergeTokens.
protected List<MergerToken> createMergeTokens(String tableFilterInclude) {
FiltersConfig filters = FiltersConfig.create(null, null, TableFilter.include(tableFilterInclude), PatternFilter.INCLUDE_NOTHING);
DbLoaderConfiguration loaderConfiguration = new DbLoaderConfiguration();
loaderConfiguration.setFiltersConfig(filters);
DataMap dbImport;
try (Connection conn = node.getDataSource().getConnection()) {
dbImport = new DbLoader(node.getAdapter(), conn, loaderConfiguration, new LoggingDbLoaderDelegate(LoggerFactory.getLogger(DbLoader.class)), new DefaultObjectNameGenerator(NoStemStemmer.getInstance())).load();
} catch (SQLException e) {
throw new CayenneRuntimeException("Can't doLoad dataMap from db.", e);
}
List<MergerToken> tokens = merger().filters(filters).build().createMergeTokens(map, dbImport);
return filter(tokens);
}
use of org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator in project cayenne by apache.
the class CreateObjEntityAction method createObjEntity.
protected void createObjEntity() {
ProjectController mediator = getProjectController();
DataMap dataMap = mediator.getCurrentDataMap();
ObjEntity entity = new ObjEntity();
entity.setName(NameBuilder.builder(entity, dataMap).name());
// init defaults
entity.setSuperClassName(dataMap.getDefaultSuperclass());
entity.setDeclaredLockType(dataMap.getDefaultLockType());
DbEntity dbEntity = mediator.getCurrentDbEntity();
if (dbEntity != null) {
entity.setDbEntity(dbEntity);
// TODO: use injectable name generator
String baseName = new DefaultObjectNameGenerator(NoStemStemmer.getInstance()).objEntityName(dbEntity);
entity.setName(NameBuilder.builder(entity, dbEntity.getDataMap()).baseName(baseName).name());
}
entity.setClassName(dataMap.getNameWithDefaultPackage(entity.getName()));
if (dataMap.isClientSupported()) {
entity.setClientClassName(dataMap.getNameWithDefaultClientPackage(entity.getName()));
entity.setClientSuperClassName(dataMap.getDefaultClientSuperclass());
}
dataMap.addObjEntity(entity);
// TODO: Modeler-controlled defaults for all the hardcoded boolean flags here.
EntityMergeSupport merger = new EntityMergeSupport(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()), NamePatternMatcher.EXCLUDE_ALL, true, true, false);
merger.setNameGenerator(new DbEntitySyncAction.PreserveRelationshipNameGenerator());
merger.addEntityMergeListener(DeleteRuleUpdater.getEntityMergeListener());
merger.synchronizeWithDbEntity(entity);
fireObjEntityEvent(this, mediator, dataMap, entity);
application.getUndoManager().addEdit(new CreateObjEntityUndoableEdit(dataMap, entity));
}
Aggregations