Search in sources :

Example 51 with DbEntity

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

the class MergerFactoryIT method testAddTableToDb.

@Test
public void testAddTableToDb() throws Exception {
    dropTableIfPresent("NEW_TABLE");
    assertTokensAndExecute(0, 0);
    DbEntity dbEntity = new DbEntity("NEW_TABLE");
    DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
    column1.setMandatory(true);
    column1.setPrimaryKey(true);
    dbEntity.addAttribute(column1);
    DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
    column2.setMaxLength(10);
    column2.setMandatory(false);
    dbEntity.addAttribute(column2);
    map.addDbEntity(dbEntity);
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
    ObjEntity objEntity = new ObjEntity("NewTable");
    objEntity.setDbEntity(dbEntity);
    ObjAttribute oatr1 = new ObjAttribute("name");
    oatr1.setDbAttributePath(column2.getName());
    oatr1.setType("java.lang.String");
    objEntity.addAttribute(oatr1);
    map.addObjEntity(objEntity);
    for (int i = 0; i < 5; i++) {
        CayenneDataObject dao = (CayenneDataObject) context.newObject(objEntity.getName());
        dao.writeProperty(oatr1.getName(), "test " + i);
    }
    context.commitChanges();
    // clear up
    map.removeObjEntity(objEntity.getName(), true);
    map.removeDbEntity(dbEntity.getName(), true);
    resolver.refreshMappingCache();
    assertNull(map.getObjEntity(objEntity.getName()));
    assertNull(map.getDbEntity(dbEntity.getName()));
    assertFalse(map.getDbEntities().contains(dbEntity));
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) CayenneDataObject(org.apache.cayenne.CayenneDataObject) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 52 with DbEntity

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

the class MergerFactoryIT method testMultipleTokensToDb.

@Test
public void testMultipleTokensToDb() throws Exception {
    DbEntity dbEntity = map.getDbEntity("PAINTING");
    assertNotNull(dbEntity);
    DbAttribute column1 = new DbAttribute("NEWCOL3", Types.VARCHAR, dbEntity);
    column1.setMandatory(false);
    column1.setMaxLength(10);
    dbEntity.addAttribute(column1);
    DbAttribute column2 = new DbAttribute("NEWCOL4", Types.VARCHAR, dbEntity);
    column2.setMandatory(false);
    column2.setMaxLength(10);
    dbEntity.addAttribute(column2);
    assertTokensAndExecute(2, 0);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // change size
    column1.setMaxLength(20);
    column2.setMaxLength(30);
    // merge to db
    assertTokensAndExecute(2, 0);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // clean up
    dbEntity.removeAttribute(column1.getName());
    dbEntity.removeAttribute(column2.getName());
    assertTokensAndExecute(2, 0);
    assertTokensAndExecute(0, 0);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 53 with DbEntity

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

the class MergerFactoryIT method testAddForeignKeyWithTable.

@Test
public void testAddForeignKeyWithTable() throws Exception {
    dropTableIfPresent("NEW_TABLE");
    assertTokensAndExecute(0, 0);
    DbEntity dbEntity = new DbEntity("NEW_TABLE");
    attr(dbEntity, "ID", Types.INTEGER, true, true);
    attr(dbEntity, "NAME", Types.VARCHAR, false, false).setMaxLength(10);
    attr(dbEntity, "ARTIST_ID", Types.BIGINT, false, false);
    map.addDbEntity(dbEntity);
    DbEntity artistDbEntity = map.getDbEntity("ARTIST");
    assertNotNull(artistDbEntity);
    // relation from new_table to artist
    DbRelationship r1 = new DbRelationship("toArtistR1");
    r1.setSourceEntity(dbEntity);
    r1.setTargetEntityName(artistDbEntity);
    r1.setToMany(false);
    r1.addJoin(new DbJoin(r1, "ARTIST_ID", "ARTIST_ID"));
    dbEntity.addRelationship(r1);
    // relation from artist to new_table
    DbRelationship r2 = new DbRelationship("toNewTableR2");
    r2.setSourceEntity(artistDbEntity);
    r2.setTargetEntityName(dbEntity);
    r2.setToMany(true);
    r2.addJoin(new DbJoin(r2, "ARTIST_ID", "ARTIST_ID"));
    artistDbEntity.addRelationship(r2);
    assertTokensAndExecute(2, 0);
    assertTokensAndExecute(0, 0);
    // remove relationships
    dbEntity.removeRelationship(r1.getName());
    artistDbEntity.removeRelationship(r2.getName());
    resolver.refreshMappingCache();
    /*
         * Db -Rel 'toArtistR1' - NEW_TABLE 1 -> 1 ARTIST"
r2 =     * Db -Rel 'toNewTableR2' - ARTIST 1 -> * NEW_TABLE" -- Not generated any more
         * */
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
    // clear up
    // map.removeObjEntity(objEntity.getName(), true);
    map.removeDbEntity(dbEntity.getName(), true);
    resolver.refreshMappingCache();
    // assertNull(map.getObjEntity(objEntity.getName()));
    assertNull(map.getDbEntity(dbEntity.getName()));
    assertFalse(map.getDbEntities().contains(dbEntity));
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) Test(org.junit.Test)

Example 54 with DbEntity

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

use of org.apache.cayenne.map.DbEntity 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)

Aggregations

DbEntity (org.apache.cayenne.map.DbEntity)273 DbAttribute (org.apache.cayenne.map.DbAttribute)106 Test (org.junit.Test)106 ObjEntity (org.apache.cayenne.map.ObjEntity)64 DbRelationship (org.apache.cayenne.map.DbRelationship)55 DataMap (org.apache.cayenne.map.DataMap)47 ObjAttribute (org.apache.cayenne.map.ObjAttribute)26 ArrayList (java.util.ArrayList)25 DbJoin (org.apache.cayenne.map.DbJoin)24 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)20 ObjRelationship (org.apache.cayenne.map.ObjRelationship)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)16 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)16 Entity (org.apache.cayenne.map.Entity)16 List (java.util.List)15 DbAdapter (org.apache.cayenne.dba.DbAdapter)15 EntityEvent (org.apache.cayenne.map.event.EntityEvent)14 HashMap (java.util.HashMap)12 SelectQuery (org.apache.cayenne.query.SelectQuery)12 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)11