Search in sources :

Example 1 with MergerContext

use of org.apache.cayenne.dbsync.merge.context.MergerContext in project cayenne by apache.

the class TokensToModelExecutionTest method testCreateAndDropTable.

@Test
public void testCreateAndDropTable() throws Exception {
    DbEntity entity = dbEntity().build();
    DataMap dataMap = dataMap().build();
    assertTrue(dataMap.getDbEntityMap().isEmpty());
    assertTrue(dataMap.getObjEntityMap().isEmpty());
    MergerContext context = MergerContext.builder(dataMap).dataNode(new DataNode()).build();
    new DefaultMergerTokenFactory().createCreateTableToModel(entity).execute(context);
    assertEquals(1, dataMap.getDbEntityMap().size());
    assertEquals(1, dataMap.getObjEntities().size());
    assertEquals(entity, dataMap.getDbEntity(entity.getName()));
    new DefaultMergerTokenFactory().createDropTableToModel(entity).execute(context);
    assertTrue(dataMap.getDbEntityMap().isEmpty());
    assertTrue(dataMap.getObjEntityMap().isEmpty());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DataNode(org.apache.cayenne.access.DataNode) DefaultMergerTokenFactory(org.apache.cayenne.dbsync.merge.factory.DefaultMergerTokenFactory) MergerContext(org.apache.cayenne.dbsync.merge.context.MergerContext) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 2 with MergerContext

use of org.apache.cayenne.dbsync.merge.context.MergerContext in project cayenne by apache.

the class MergerOptions method generateSchemaAction.

/**
 * Performs configured schema operations via DbGenerator.
 */
public void generateSchemaAction() {
    refreshGeneratorAction();
    // sanity check...
    List<MergerToken> tokensToMigrate = tokens.getSelectedTokens();
    if (tokensToMigrate.isEmpty()) {
        JOptionPane.showMessageDialog(getView(), "Nothing to migrate.");
        return;
    }
    DataSource dataSource;
    try {
        dataSource = connectionInfo.makeDataSource(getApplication().getClassLoadingService());
    } catch (SQLException ex) {
        reportError("Migration Error", ex);
        return;
    }
    final Collection<ObjEntity> loadedObjEntities = new LinkedList<>();
    MergerContext mergerContext = MergerContext.builder(dataMap).syntheticDataNode(dataSource, adapter).delegate(createDelegate(loadedObjEntities)).build();
    boolean modelChanged = applyTokens(tokensToMigrate, mergerContext);
    DefaultDbImportAction.flattenManyToManyRelationships(dataMap, loadedObjEntities, mergerContext.getNameGenerator());
    notifyProjectModified(modelChanged);
    reportFailures(mergerContext);
    if (tokens.isReverse()) {
        getApplication().getUndoManager().discardAllEdits();
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) SQLException(java.sql.SQLException) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) MergerContext(org.apache.cayenne.dbsync.merge.context.MergerContext) LinkedList(java.util.LinkedList) DataSource(javax.sql.DataSource)

Example 3 with MergerContext

use of org.apache.cayenne.dbsync.merge.context.MergerContext in project cayenne by apache.

the class DefaultDbImportAction method applyTokens.

private boolean applyTokens(DataMap targetDataMap, Collection<MergerToken> tokens, DbImportConfiguration config) {
    if (tokens.isEmpty()) {
        logger.info("");
        logger.info("Detected changes: No changes to import.");
        return false;
    }
    final Collection<ObjEntity> loadedObjEntities = new LinkedList<>();
    ModelMergeDelegate mergeDelegate = new ProxyModelMergeDelegate(config.createMergeDelegate()) {

        @Override
        public void objEntityAdded(ObjEntity ent) {
            loadedObjEntities.add(ent);
            super.objEntityAdded(ent);
        }
    };
    ObjectNameGenerator nameGenerator = config.createNameGenerator();
    MergerContext mergerContext = MergerContext.builder(targetDataMap).delegate(mergeDelegate).nameGenerator(nameGenerator).usingPrimitives(config.isUsePrimitives()).usingJava7Types(config.isUseJava7Types()).meaningfulPKFilter(config.createMeaningfulPKFilter()).build();
    for (MergerToken token : tokens) {
        try {
            token.execute(mergerContext);
        } catch (Throwable th) {
            String message = "Migration Error. Can't apply changes from token: " + token.getTokenName() + " (" + token.getTokenValue() + ")";
            logger.error(message, th);
            mergerContext.getValidationResult().addFailure(new SimpleValidationFailure(th, message));
        }
    }
    ValidationResult failures = mergerContext.getValidationResult();
    if (failures.hasFailures()) {
        logger.info("Migration Complete.");
        logger.warn("Migration finished. The following problem(s) were encountered and ignored.");
        for (ValidationFailure failure : failures.getFailures()) {
            logger.warn(failure.toString());
        }
    } else {
        logger.info("Migration Complete Successfully.");
    }
    flattenManyToManyRelationships(targetDataMap, loadedObjEntities, nameGenerator);
    relationshipsSanity(targetDataMap);
    return true;
}
Also used : ObjectNameGenerator(org.apache.cayenne.dbsync.naming.ObjectNameGenerator) ModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ModelMergeDelegate) ProxyModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ProxyModelMergeDelegate) ValidationResult(org.apache.cayenne.validation.ValidationResult) MergerContext(org.apache.cayenne.dbsync.merge.context.MergerContext) LinkedList(java.util.LinkedList) SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) ObjEntity(org.apache.cayenne.map.ObjEntity) SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) ProxyModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ProxyModelMergeDelegate)

Example 4 with MergerContext

use of org.apache.cayenne.dbsync.merge.context.MergerContext in project cayenne by apache.

the class MergeCase method execute.

protected void execute(MergerToken token) throws Exception {
    MergerContext mergerContext = MergerContext.builder(map).dataNode(node).build();
    token.execute(mergerContext);
}
Also used : MergerContext(org.apache.cayenne.dbsync.merge.context.MergerContext)

Example 5 with MergerContext

use of org.apache.cayenne.dbsync.merge.context.MergerContext in project cayenne by apache.

the class TokensToModelExecutionTest method testCreateAndDropColumn.

@Test
public void testCreateAndDropColumn() throws Exception {
    DbAttribute attr = dbAttr("attr").build();
    DbEntity entity = dbEntity().build();
    DataMap dataMap = dataMap().with(entity).build();
    assertEquals(1, dataMap.getDbEntityMap().size());
    assertTrue(dataMap.getObjEntityMap().isEmpty());
    MergerContext context = MergerContext.builder(dataMap).dataNode(new DataNode()).build();
    new DefaultMergerTokenFactory().createAddColumnToModel(entity, attr).execute(context);
    assertEquals(1, dataMap.getDbEntityMap().size());
    assertEquals(1, entity.getAttributes().size());
    assertEquals(attr, entity.getAttribute(attr.getName()));
    new DefaultMergerTokenFactory().createDropColumnToModel(entity, attr).execute(context);
    assertEquals(1, dataMap.getDbEntityMap().size());
    assertTrue(entity.getAttributes().isEmpty());
    assertTrue(dataMap.getObjEntityMap().isEmpty());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DataNode(org.apache.cayenne.access.DataNode) DefaultMergerTokenFactory(org.apache.cayenne.dbsync.merge.factory.DefaultMergerTokenFactory) DbAttribute(org.apache.cayenne.map.DbAttribute) MergerContext(org.apache.cayenne.dbsync.merge.context.MergerContext) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Aggregations

MergerContext (org.apache.cayenne.dbsync.merge.context.MergerContext)5 LinkedList (java.util.LinkedList)2 DataNode (org.apache.cayenne.access.DataNode)2 DefaultMergerTokenFactory (org.apache.cayenne.dbsync.merge.factory.DefaultMergerTokenFactory)2 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)2 DataMap (org.apache.cayenne.map.DataMap)2 DbEntity (org.apache.cayenne.map.DbEntity)2 ObjEntity (org.apache.cayenne.map.ObjEntity)2 Test (org.junit.Test)2 SQLException (java.sql.SQLException)1 DataSource (javax.sql.DataSource)1 ObjectNameGenerator (org.apache.cayenne.dbsync.naming.ObjectNameGenerator)1 ModelMergeDelegate (org.apache.cayenne.dbsync.reverse.dbload.ModelMergeDelegate)1 ProxyModelMergeDelegate (org.apache.cayenne.dbsync.reverse.dbload.ProxyModelMergeDelegate)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 SimpleValidationFailure (org.apache.cayenne.validation.SimpleValidationFailure)1 ValidationFailure (org.apache.cayenne.validation.ValidationFailure)1 ValidationResult (org.apache.cayenne.validation.ValidationResult)1