Search in sources :

Example 26 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class DataMapMergerTest method testChangeColumnLength.

@Test
public void testChangeColumnLength() throws Exception {
    DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeVarchar(60))).build();
    DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeVarchar(30))).build();
    List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
    assertEquals(1, tokens.size());
    DbEntity entity = existing.getDbEntity("table1");
    DbEntity entityDb = db.getDbEntity("table1");
    assertTrue(tokens.get(0) instanceof SetColumnTypeToDb);
    assertEquals(factory().createSetColumnTypeToDb(entity, entityDb.getAttribute("attr01"), entity.getAttribute("attr01")).getTokenValue(), tokens.get(0).getTokenValue());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) SetColumnTypeToDb(org.apache.cayenne.dbsync.merge.token.db.SetColumnTypeToDb) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 27 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class DataMapMergerTest method testRemoveRelationship.

@Test
public void testRemoveRelationship() throws Exception {
    DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt())).build();
    DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt())).join("rel", "table1.attr01", "table2.attr01").build();
    List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
    assertEquals(1, tokens.size());
    DbEntity entity = db.getDbEntity("table1");
    assertEquals(factory().createDropRelationshipToDb(entity, entity.getRelationship("rel")).getTokenValue(), tokens.get(0).getTokenValue());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 28 with DataMap

use of org.apache.cayenne.map.DataMap 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 29 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class ObjEntityTab method initController.

private void initController() {
    // initialize events processing and tracking of UI updates...
    mediator.addObjEntityDisplayListener(this);
    dbEntityCombo.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // Change DbEntity for current ObjEntity
            ObjEntity entity = mediator.getCurrentObjEntity();
            DbEntity dbEntity = (DbEntity) dbEntityCombo.getSelectedItem();
            if (dbEntity != entity.getDbEntity()) {
                entity.setDbEntity(dbEntity);
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
            }
        }
    });
    superEntityCombo.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // Change super-entity
            ObjEntity superEntity = (ObjEntity) superEntityCombo.getSelectedItem();
            String name = (superEntity == null || superEntity == NO_INHERITANCE) ? null : superEntity.getName();
            ObjEntity entity = mediator.getCurrentObjEntity();
            if (!Util.nullSafeEquals(name, entity.getSuperEntityName())) {
                List<ObjAttribute> duplicateAttributes = null;
                if (name != null) {
                    duplicateAttributes = getDuplicatedAttributes(superEntity);
                }
                if (duplicateAttributes != null && duplicateAttributes.size() > 0) {
                    DuplicatedAttributesDialog.showDialog(Application.getFrame(), duplicateAttributes, superEntity, entity);
                    if (DuplicatedAttributesDialog.getResult().equals(DuplicatedAttributesDialog.CANCEL_RESULT)) {
                        superEntityCombo.setSelectedItem(entity.getSuperEntity());
                        superClassName.setText(entity.getSuperClassName());
                        return;
                    }
                }
                entity.setSuperEntityName(name);
                // drop not valid dbAttributePath
                if (name == null) {
                    for (ObjAttribute objAttribute : entity.getAttributes()) {
                        if (objAttribute.getDbAttribute() == null) {
                            objAttribute.setDbAttributePath(null);
                        }
                    }
                }
                if (name == null) {
                    dbEntityCombo.setEnabled(true);
                } else {
                    dbEntityCombo.setEnabled(false);
                    dbEntityCombo.getModel().setSelectedItem(null);
                }
                // if a super-entity selected, disable table selection
                // and also update parent DbEntity selection...
                toggleEnabled(name == null, !serverOnly.isSelected());
                dbEntityCombo.getModel().setSelectedItem(entity.getDbEntity());
                superClassName.setText(entity.getSuperClassName());
                // fire both EntityEvent and EntityDisplayEvent;
                // the later is to update attribute and relationship display
                DataChannelDescriptor domain = (DataChannelDescriptor) mediator.getProject().getRootNode();
                DataMap map = mediator.getCurrentDataMap();
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
                mediator.fireObjEntityDisplayEvent(new EntityDisplayEvent(this, entity, map, domain));
            }
        }
    });
    tableLabel.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // Jump to DbEntity of the current ObjEntity
            DbEntity entity = mediator.getCurrentObjEntity().getDbEntity();
            if (entity != null) {
                DataChannelDescriptor dom = (DataChannelDescriptor) mediator.getProject().getRootNode();
                mediator.fireDbEntityDisplayEvent(new EntityDisplayEvent(this, entity, entity.getDataMap(), dom));
            }
        }
    });
    readOnly.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            ObjEntity entity = mediator.getCurrentObjEntity();
            if (entity != null) {
                entity.setReadOnly(readOnly.isSelected());
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
            }
        }
    });
    optimisticLocking.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            ObjEntity entity = mediator.getCurrentObjEntity();
            if (entity != null) {
                entity.setDeclaredLockType(optimisticLocking.isSelected() ? ObjEntity.LOCK_TYPE_OPTIMISTIC : ObjEntity.LOCK_TYPE_NONE);
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
            }
        }
    });
    serverOnly.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            ObjEntity entity = mediator.getCurrentObjEntity();
            if (entity != null) {
                entity.setServerOnly(serverOnly.isSelected());
                toggleEnabled(dbEntityCombo.isEnabled(), !serverOnly.isSelected());
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
            }
        }
    });
    isAbstract.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            ObjEntity entity = mediator.getCurrentObjEntity();
            if (entity != null) {
                entity.setAbstract(isAbstract.isSelected());
                mediator.fireObjEntityEvent(new EntityEvent(this, entity));
            }
        }
    });
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ItemEvent(java.awt.event.ItemEvent) EntityDisplayEvent(org.apache.cayenne.modeler.event.EntityDisplayEvent) ObjAttribute(org.apache.cayenne.map.ObjAttribute) ActionEvent(java.awt.event.ActionEvent) DataMap(org.apache.cayenne.map.DataMap) ObjEntity(org.apache.cayenne.map.ObjEntity) ActionListener(java.awt.event.ActionListener) DbEntity(org.apache.cayenne.map.DbEntity) EntityEvent(org.apache.cayenne.map.event.EntityEvent) List(java.util.List) LinkedList(java.util.LinkedList) ItemListener(java.awt.event.ItemListener)

Example 30 with DataMap

use of org.apache.cayenne.map.DataMap in project cayenne by apache.

the class ProcedureQueryView method getEntity.

/**
 * Returns an entity that maps to a procedure query result class.
 */
ObjEntity getEntity(ProcedureQueryDescriptor query) {
    String entityName = query.getResultEntityName();
    if (entityName == null) {
        return null;
    }
    DataMap map = mediator.getCurrentDataMap();
    if (map == null) {
        return null;
    }
    return map.getObjEntity(entityName);
}
Also used : DataMap(org.apache.cayenne.map.DataMap)

Aggregations

DataMap (org.apache.cayenne.map.DataMap)233 Test (org.junit.Test)88 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)48 DbEntity (org.apache.cayenne.map.DbEntity)48 ObjEntity (org.apache.cayenne.map.ObjEntity)45 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)22 URL (java.net.URL)21 URLResource (org.apache.cayenne.resource.URLResource)20 ArrayList (java.util.ArrayList)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)19 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)17 QueryDescriptor (org.apache.cayenne.map.QueryDescriptor)16 DataMapEvent (org.apache.cayenne.configuration.event.DataMapEvent)15 File (java.io.File)14 Procedure (org.apache.cayenne.map.Procedure)14 Embeddable (org.apache.cayenne.map.Embeddable)13 Injector (org.apache.cayenne.di.Injector)12 DbAttribute (org.apache.cayenne.map.DbAttribute)11 EntityResolver (org.apache.cayenne.map.EntityResolver)11 Entity (org.apache.cayenne.map.Entity)10