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());
}
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());
}
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());
}
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));
}
}
});
}
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);
}
Aggregations