Search in sources :

Example 1 with EntityMergeSupport

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

the class DbEntitySyncAction method syncDbEntity.

protected void syncDbEntity() {
    ProjectController mediator = getProjectController();
    DbEntity dbEntity = mediator.getCurrentDbEntity();
    if (dbEntity != null) {
        Collection<ObjEntity> entities = dbEntity.getDataMap().getMappedEntities(dbEntity);
        if (entities.isEmpty()) {
            return;
        }
        EntityMergeSupport merger = new EntitySyncController(Application.getInstance().getFrameController(), dbEntity).createMerger();
        if (merger == null) {
            return;
        }
        merger.setNameGenerator(new PreserveRelationshipNameGenerator());
        DbEntitySyncUndoableEdit undoableEdit = new DbEntitySyncUndoableEdit((DataChannelDescriptor) mediator.getProject().getRootNode(), mediator.getCurrentDataMap());
        // filter out inherited entities, as we need to add attributes only to the roots
        filterInheritedEntities(entities);
        for (ObjEntity entity : entities) {
            DbEntitySyncUndoableEdit.EntitySyncUndoableListener listener = undoableEdit.new EntitySyncUndoableListener(entity);
            merger.addEntityMergeListener(listener);
            // we should not be trying to introspect the merger
            if (merger.isRemovingMeaningfulFKs()) {
                undoableEdit.addEdit(undoableEdit.new MeaningfulFKsUndoableEdit(entity, merger.getMeaningfulFKs(entity)));
            }
            if (merger.synchronizeWithDbEntity(entity)) {
                mediator.fireObjEntityEvent(new EntityEvent(this, entity, MapEvent.CHANGE));
            }
            merger.removeEntityMergeListener(listener);
        }
        application.getUndoManager().addEdit(undoableEdit);
    }
}
Also used : EntityMergeSupport(org.apache.cayenne.dbsync.merge.context.EntityMergeSupport) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) EntityEvent(org.apache.cayenne.map.event.EntityEvent) DbEntitySyncUndoableEdit(org.apache.cayenne.modeler.undo.DbEntitySyncUndoableEdit) ProjectController(org.apache.cayenne.modeler.ProjectController) EntitySyncController(org.apache.cayenne.modeler.dialog.objentity.EntitySyncController)

Example 2 with EntityMergeSupport

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

the class ObjEntitySyncAction method syncObjEntity.

protected void syncObjEntity() {
    ProjectController mediator = getProjectController();
    ObjEntity entity = mediator.getCurrentObjEntity();
    if (entity != null && entity.getDbEntity() != null) {
        EntityMergeSupport merger = new EntitySyncController(Application.getInstance().getFrameController(), entity).createMerger();
        if (merger == null) {
            return;
        }
        merger.setNameGenerator(new DbEntitySyncAction.PreserveRelationshipNameGenerator());
        if (merger.synchronizeWithDbEntity(entity)) {
            mediator.fireObjEntityEvent(new EntityEvent(this, entity, MapEvent.CHANGE));
            mediator.fireObjEntityDisplayEvent(new EntityDisplayEvent(this, entity, entity.getDataMap(), (DataChannelDescriptor) mediator.getProject().getRootNode()));
        }
    }
}
Also used : EntityMergeSupport(org.apache.cayenne.dbsync.merge.context.EntityMergeSupport) ObjEntity(org.apache.cayenne.map.ObjEntity) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) EntityDisplayEvent(org.apache.cayenne.modeler.event.EntityDisplayEvent) EntityEvent(org.apache.cayenne.map.event.EntityEvent) ProjectController(org.apache.cayenne.modeler.ProjectController) EntitySyncController(org.apache.cayenne.modeler.dialog.objentity.EntitySyncController)

Example 3 with EntityMergeSupport

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

the class EntitySyncController method createMerger.

public EntityMergeSupport createMerger() {
    Collection<ObjEntity> entities = getObjEntities();
    if (entities.isEmpty()) {
        return null;
    }
    ObjectNameGenerator namingStrategy;
    try {
        namingStrategy = NameGeneratorPreferences.getInstance().createNamingStrategy(application);
    } catch (Throwable e) {
        namingStrategy = NameGeneratorPreferences.defaultNameGenerator();
    }
    // TODO: Modeler-controlled defaults for all the hardcoded boolean flags here.
    EntityMergeSupport merger = new EntityMergeSupport(namingStrategy, NamePatternMatcher.EXCLUDE_ALL, true, true, false);
    // see if we need to remove meaningful attributes...
    for (ObjEntity entity : entities) {
        if (!merger.getMeaningfulFKs(entity).isEmpty()) {
            return confirmMeaningfulFKs(namingStrategy);
        }
    }
    return merger;
}
Also used : EntityMergeSupport(org.apache.cayenne.dbsync.merge.context.EntityMergeSupport) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjectNameGenerator(org.apache.cayenne.dbsync.naming.ObjectNameGenerator)

Example 4 with EntityMergeSupport

use of org.apache.cayenne.dbsync.merge.context.EntityMergeSupport 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 5 with EntityMergeSupport

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

the class EntitySyncController method confirmMeaningfulFKs.

/**
 * Displays merger config dialog, returning a merger configured by the user. Returns
 * null if the dialog was canceled.
 */
protected EntityMergeSupport confirmMeaningfulFKs(ObjectNameGenerator namingStrategy) {
    final boolean[] cancel = { false };
    final boolean[] removeFKs = { true };
    view = new EntitySyncDialog();
    view.getUpdateButton().addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            removeFKs[0] = view.getRemoveFKs().isSelected();
            view.dispose();
        }
    });
    view.getCancelButton().addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            cancel[0] = true;
            view.dispose();
        }
    });
    view.pack();
    view.setModal(true);
    centerView();
    makeCloseableOnEscape();
    view.setVisible(true);
    // TODO: Modeler-controlled defaults for all the hardcoded flags here.
    return cancel[0] ? null : new EntityMergeSupport(namingStrategy, NamePatternMatcher.EXCLUDE_ALL, removeFKs[0], true, false);
}
Also used : EntityMergeSupport(org.apache.cayenne.dbsync.merge.context.EntityMergeSupport) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent)

Aggregations

EntityMergeSupport (org.apache.cayenne.dbsync.merge.context.EntityMergeSupport)6 ObjEntity (org.apache.cayenne.map.ObjEntity)5 DbEntity (org.apache.cayenne.map.DbEntity)3 ProjectController (org.apache.cayenne.modeler.ProjectController)3 DefaultObjectNameGenerator (org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator)2 EntityEvent (org.apache.cayenne.map.event.EntityEvent)2 EntitySyncController (org.apache.cayenne.modeler.dialog.objentity.EntitySyncController)2 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)1 ObjectNameGenerator (org.apache.cayenne.dbsync.naming.ObjectNameGenerator)1 DataMap (org.apache.cayenne.map.DataMap)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 DbJoin (org.apache.cayenne.map.DbJoin)1 DbRelationship (org.apache.cayenne.map.DbRelationship)1 EntityDisplayEvent (org.apache.cayenne.modeler.event.EntityDisplayEvent)1 CreateObjEntityUndoableEdit (org.apache.cayenne.modeler.undo.CreateObjEntityUndoableEdit)1 DbEntitySyncUndoableEdit (org.apache.cayenne.modeler.undo.DbEntitySyncUndoableEdit)1 Test (org.junit.Test)1