use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class DropColumnToModelIT method testRemoveFKColumnWithoutRelationshipInDb.
@Test
public void testRemoveFKColumnWithoutRelationshipInDb() throws Exception {
dropTableIfPresent("NEW_TABLE");
dropTableIfPresent("NEW_TABLE2");
assertTokensAndExecute(0, 0);
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);
DbAttribute e2col3 = new DbAttribute("NAME", Types.VARCHAR, dbEntity2);
e2col3.setMaxLength(10);
dbEntity2.addAttribute(e2col3);
map.addDbEntity(dbEntity2);
assertTokensAndExecute(2, 0);
assertTokensAndExecute(0, 0);
// force drop fk column in db
execute(mergerFactory().createDropColumnToDb(dbEntity2, e2col2));
// create db relationships, but do not sync them to db
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());
// create ObjEntities
ObjEntity objEntity1 = new ObjEntity("NewTable");
objEntity1.setDbEntity(dbEntity1);
ObjAttribute oatr1 = new ObjAttribute("name");
oatr1.setDbAttributePath(e1col2.getName());
oatr1.setType("java.lang.String");
objEntity1.addAttribute(oatr1);
map.addObjEntity(objEntity1);
ObjEntity objEntity2 = new ObjEntity("NewTable2");
objEntity2.setDbEntity(dbEntity2);
ObjAttribute o2a1 = new ObjAttribute("name");
o2a1.setDbAttributePath(e2col3.getName());
o2a1.setType("java.lang.String");
objEntity2.addAttribute(o2a1);
map.addObjEntity(objEntity2);
// create ObjRelationships
assertEquals(0, objEntity1.getRelationships().size());
assertEquals(0, objEntity2.getRelationships().size());
ObjRelationship objRel1To2 = new ObjRelationship("objRel1To2");
objRel1To2.addDbRelationship(rel1To2);
objRel1To2.setSourceEntity(objEntity1);
objRel1To2.setTargetEntityName(objEntity2);
objEntity1.addRelationship(objRel1To2);
ObjRelationship objRel2To1 = new ObjRelationship("objRel2To1");
objRel2To1.addDbRelationship(rel2To1);
objRel2To1.setSourceEntity(objEntity2);
objRel2To1.setTargetEntityName(objEntity1);
objEntity2.addRelationship(objRel2To1);
assertEquals(1, objEntity1.getRelationships().size());
assertEquals(1, objEntity2.getRelationships().size());
assertSame(objRel1To2, objRel2To1.getReverseRelationship());
assertSame(objRel2To1, objRel1To2.getReverseRelationship());
// try do use the merger to remove the column and relationship in the
// model
List<MergerToken> tokens = createMergeTokens();
assertTokens(tokens, 2, 0);
// TODO: reversing the following two tokens should also reverse the
// order
MergerToken token0 = tokens.get(0).createReverse(mergerFactory());
MergerToken token1 = tokens.get(1).createReverse(mergerFactory());
if (!(token0 instanceof DropRelationshipToModel && token1 instanceof DropColumnToModel || token1 instanceof DropRelationshipToModel && token0 instanceof DropColumnToModel)) {
fail();
}
// do not execute DropRelationshipToModel, only DropColumnToModel.
if (token1 instanceof DropColumnToModel) {
execute(token1);
} else {
execute(token0);
}
// check after merging
assertNull(dbEntity2.getAttribute(e2col2.getName()));
assertEquals(0, dbEntity1.getRelationships().size());
assertEquals(0, dbEntity2.getRelationships().size());
assertEquals(0, objEntity1.getRelationships().size());
assertEquals(0, objEntity2.getRelationships().size());
// clear up
dbEntity1.removeRelationship(rel1To2.getName());
dbEntity2.removeRelationship(rel2To1.getName());
map.removeObjEntity(objEntity1.getName(), true);
map.removeDbEntity(dbEntity1.getName(), true);
map.removeObjEntity(objEntity2.getName(), true);
map.removeDbEntity(dbEntity2.getName(), true);
resolver.refreshMappingCache();
assertNull(map.getObjEntity(objEntity1.getName()));
assertNull(map.getDbEntity(dbEntity1.getName()));
assertNull(map.getObjEntity(objEntity2.getName()));
assertNull(map.getDbEntity(dbEntity2.getName()));
assertFalse(map.getDbEntities().contains(dbEntity1));
assertFalse(map.getDbEntities().contains(dbEntity2));
assertTokensAndExecute(2, 0);
assertTokensAndExecute(0, 0);
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class DiffProcessor method arcDeleted.
@Override
public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) {
ObjectId id = (ObjectId) nodeId;
String relationshipName = arcId.toString();
ObjEntity entity = entityResolver.getObjEntity(id.getEntityName());
ObjRelationship relationship = entity.getRelationship(relationshipName);
MutableObjectChange c = changeSet.getOrCreate(id, ObjectChangeType.UPDATE);
ObjectId tid = (ObjectId) targetNodeId;
if (relationship.isToMany()) {
c.toManyRelationshipDisconnected(relationshipName, tid);
} else {
c.toOneRelationshipDisconnected(relationshipName, tid);
}
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class EntityMergeSupport method createObjRelationship.
private boolean createObjRelationship(ObjEntity entity, DbRelationship dr, String targetEntityName) {
ObjRelationship or = new ObjRelationship();
or.setName(NameBuilder.builder(or, entity).baseName(nameGenerator.relationshipName(dr)).name());
or.addDbRelationship(dr);
Map<String, ObjEntity> objEntities = entity.getDataMap().getSubclassesForObjEntity(entity);
boolean hasFlattingAttributes = false;
boolean needGeneratedEntity = true;
if (objEntities.containsKey(targetEntityName)) {
needGeneratedEntity = false;
}
for (ObjEntity subObjEntity : objEntities.values()) {
for (ObjAttribute objAttribute : subObjEntity.getAttributes()) {
String path = objAttribute.getDbAttributePath();
if (path != null) {
if (path.startsWith(or.getDbRelationshipPath())) {
hasFlattingAttributes = true;
break;
}
}
}
}
if (!hasFlattingAttributes) {
if (needGeneratedEntity) {
or.setTargetEntityName(targetEntityName);
or.setSourceEntity(entity);
}
entity.addRelationship(or);
fireRelationshipAdded(or);
}
return needGeneratedEntity;
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class DefaultDbImportAction method relationshipsSanity.
private void relationshipsSanity(DataMap executed) {
for (ObjEntity objEntity : executed.getObjEntities()) {
List<ObjRelationship> rels = new LinkedList<>(objEntity.getRelationships());
for (ObjRelationship rel : rels) {
if (rel.getSourceEntity() == null || rel.getTargetEntity() == null) {
logger.error("Incorrect obj relationship source or target entity is null: " + rel);
objEntity.removeRelationship(rel.getName());
}
}
}
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class ObjEntityRelationshipPanel method rebuildTable.
protected void rebuildTable(ObjEntity entity) {
final ObjRelationshipTableModel model = new ObjRelationshipTableModel(entity, mediator, this);
model.addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
if (table.getSelectedRow() >= 0) {
ObjRelationship rel = model.getRelationship(table.getSelectedRow());
enabledResolve = rel.getSourceEntity().getDbEntity() != null;
resolveMenu.setEnabled(enabledResolve);
}
}
});
table.setModel(model);
table.setRowHeight(25);
table.setRowMargin(3);
TableColumn col = table.getColumnModel().getColumn(ObjRelationshipTableModel.REL_TARGET_PATH);
col.setCellEditor(new DbRelationshipPathComboBoxEditor());
col.setCellRenderer(new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
setToolTipText("To choose relationship press enter two times.To choose next relationship press dot.");
return this;
}
});
col = table.getColumnModel().getColumn(ObjRelationshipTableModel.REL_DELETE_RULE);
JComboBox deleteRulesCombo = Application.getWidgetFactory().createComboBox(DELETE_RULES, false);
deleteRulesCombo.setFocusable(false);
deleteRulesCombo.setEditable(true);
((JComponent) deleteRulesCombo.getEditor().getEditorComponent()).setBorder(null);
deleteRulesCombo.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
// Default to the first value
deleteRulesCombo.setSelectedIndex(0);
col.setCellEditor(Application.getWidgetFactory().createCellEditor(deleteRulesCombo));
tablePreferences.bind(table, null, null, null, ObjRelationshipTableModel.REL_NAME, true);
}
Aggregations