Search in sources :

Example 6 with DefaultObjectNameGenerator

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());
}
Also used : DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) EntityMergeSupport(org.apache.cayenne.dbsync.merge.context.EntityMergeSupport) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) DbJoin(org.apache.cayenne.map.DbJoin) Test(org.junit.Test)

Example 7 with DefaultObjectNameGenerator

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);
}
Also used : LoggingDbLoaderDelegate(org.apache.cayenne.dbsync.reverse.dbload.LoggingDbLoaderDelegate) DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) SQLException(java.sql.SQLException) FiltersConfig(org.apache.cayenne.dbsync.reverse.filters.FiltersConfig) Connection(java.sql.Connection) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration) DataMap(org.apache.cayenne.map.DataMap)

Example 8 with DefaultObjectNameGenerator

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));
}
Also used : DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) EntityMergeSupport(org.apache.cayenne.dbsync.merge.context.EntityMergeSupport) ObjEntity(org.apache.cayenne.map.ObjEntity) CreateObjEntityUndoableEdit(org.apache.cayenne.modeler.undo.CreateObjEntityUndoableEdit) DbEntity(org.apache.cayenne.map.DbEntity) ProjectController(org.apache.cayenne.modeler.ProjectController) DataMap(org.apache.cayenne.map.DataMap)

Aggregations

DefaultObjectNameGenerator (org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator)8 DataMap (org.apache.cayenne.map.DataMap)5 Test (org.junit.Test)5 DbLoader (org.apache.cayenne.dbsync.reverse.dbload.DbLoader)4 DbLoaderConfiguration (org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration)4 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)3 DbEntity (org.apache.cayenne.map.DbEntity)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 DataMapBuilder (org.apache.cayenne.dbsync.merge.builders.DataMapBuilder)2 EntityMergeSupport (org.apache.cayenne.dbsync.merge.context.EntityMergeSupport)2 DefaultModelMergeDelegate (org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate)2 LoggingDbLoaderDelegate (org.apache.cayenne.dbsync.reverse.dbload.LoggingDbLoaderDelegate)2 FiltersConfig (org.apache.cayenne.dbsync.reverse.filters.FiltersConfig)2 DbRelationship (org.apache.cayenne.map.DbRelationship)2 FileProjectSaver (org.apache.cayenne.project.FileProjectSaver)2 Project (org.apache.cayenne.project.Project)2 File (java.io.File)1 IOException (java.io.IOException)1