use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class DbEntitySyncAction method syncDbEntity.
protected void syncDbEntity() {
final ProjectController mediator = getProjectController();
final DbEntity dbEntity = mediator.getCurrentDbEntity();
if (dbEntity != null) {
final Collection<ObjEntity> entities = dbEntity.getDataMap().getMappedEntities(dbEntity);
if (entities.isEmpty()) {
return;
}
final EntityMergeSupport merger = new EntitySyncController(Application.getInstance().getFrameController(), dbEntity).createMerger();
if (merger == null) {
return;
}
merger.setNameGenerator(new PreserveRelationshipNameGenerator());
final 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);
boolean hasChanges = false;
for (final ObjEntity entity : entities) {
final DbEntitySyncUndoableEdit.EntitySyncUndoableListener listener = undoableEdit.new EntitySyncUndoableListener(entity);
merger.addEntityMergeListener(listener);
final Collection<DbAttribute> meaningfulFKs = merger.getMeaningfulFKs(entity);
// we should not be trying to introspect the merger
if (merger.isRemovingMeaningfulFKs() && !meaningfulFKs.isEmpty()) {
undoableEdit.addEdit(undoableEdit.new MeaningfulFKsUndoableEdit(entity, meaningfulFKs));
hasChanges = true;
}
if (merger.synchronizeWithDbEntity(entity)) {
mediator.fireObjEntityEvent(new EntityEvent(this, entity, MapEvent.CHANGE));
hasChanges = true;
}
merger.removeEntityMergeListener(listener);
}
if (hasChanges) {
application.getUndoManager().addEdit(undoableEdit);
}
}
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class RemoveRelationshipAction method performAction.
@Override
public void performAction(ActionEvent e, boolean allowAsking) {
ConfirmRemoveDialog dialog = getConfirmDeleteDialog(allowAsking);
ProjectController mediator = getProjectController();
ObjRelationship[] rels = getProjectController().getCurrentObjRelationships();
if (rels != null && rels.length > 0) {
if ((rels.length == 1 && dialog.shouldDelete("ObjRelationship", rels[0].getName())) || (rels.length > 1 && dialog.shouldDelete("selected ObjRelationships"))) {
ObjEntity entity = mediator.getCurrentObjEntity();
removeObjRelationships(entity, rels);
Application.getInstance().getUndoManager().addEdit(new RemoveRelationshipUndoableEdit(entity, rels));
}
} else {
DbRelationship[] dbRels = getProjectController().getCurrentDbRelationships();
if (dbRels != null && dbRels.length > 0) {
if ((dbRels.length == 1 && dialog.shouldDelete("DbRelationship", dbRels[0].getName())) || (dbRels.length > 1 && dialog.shouldDelete("selected DbRelationships"))) {
DbEntity entity = mediator.getCurrentDbEntity();
removeDbRelationships(entity, dbRels);
Application.getInstance().getUndoManager().addEdit(new RemoveRelationshipUndoableEdit(entity, dbRels));
}
}
}
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class DbAttributePathComboBoxEditor method createTreeModelForComboBox.
@Override
protected EntityTreeModel createTreeModelForComboBox(int attributeIndexInTable) {
ObjAttribute attribute = model.getAttribute(attributeIndexInTable).getValue();
Entity firstEntity = null;
if (attribute.getDbAttribute() == null) {
if (attribute.getParent() instanceof ObjEntity) {
DbEntity dbEnt = ((ObjEntity) attribute.getParent()).getDbEntity();
if (dbEnt != null) {
Collection<DbAttribute> attributes = dbEnt.getAttributes();
Collection<DbRelationship> rel = dbEnt.getRelationships();
if (!attributes.isEmpty()) {
Iterator<DbAttribute> iterator = attributes.iterator();
firstEntity = iterator.next().getEntity();
} else if (!rel.isEmpty()) {
Iterator<DbRelationship> iterator = rel.iterator();
firstEntity = iterator.next().getSourceEntity();
}
}
}
} else {
firstEntity = getFirstEntity(attribute);
}
if (firstEntity != null) {
EntityTreeModel treeModel = new EntityTreeModel(firstEntity);
treeModel.setFilter(new EntityTreeAttributeRelationshipFilter());
return treeModel;
}
return null;
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class RelationshipUndoableEdit method fireObjRelationshipEvent.
private void fireObjRelationshipEvent(Relationship relToFire, Relationship currRel) {
ObjEntity objEntity = ((ObjRelationship) currRel).getSourceEntity();
objEntity.removeRelationship(currRel.getName());
objEntity.addRelationship(relToFire);
projectController.fireObjRelationshipEvent(new RelationshipEvent(this, relToFire, relToFire.getSourceEntity(), MapEvent.ADD));
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class DbRelationshipPathComboBoxEditor method enterPressed.
@Override
protected void enterPressed(JTable table) {
String dbRelationshipPath = ((JTextComponent) (comboBoxPathChooser).getEditor().getEditorComponent()).getText();
changeObjEntity(dbRelationshipPath);
Object currentNode = getCurrentNode(dbRelationshipPath);
String[] pathStrings = dbRelationshipPath.split(Pattern.quote("."));
String lastStringInPath = pathStrings[pathStrings.length - 1];
if (lastStringInPath.equals(ModelerUtil.getObjectName(currentNode)) && currentNode instanceof DbRelationship) {
if (enterPressedCount == 1) {
// it is second time enter pressed.. so we will save input data
enterPressedCount = 0;
if (table.getCellEditor() != null) {
table.getCellEditor().stopCellEditing();
if (dbRelationshipPath.equals(savePath)) {
return;
}
// we need object target to save it in model
DbEntity lastEntity = ((DbRelationship) currentNode).getTargetEntity();
if (lastEntity != null) {
Collection<ObjEntity> objEntities = ((DbRelationship) currentNode).getTargetEntity().getDataMap().getMappedEntities(lastEntity);
ObjEntity objectTarget = objEntities.isEmpty() ? null : objEntities.iterator().next();
model.getRelationship(row).setTargetEntityName(objectTarget);
model.setUpdatedValueAt(dbRelationshipPath, row, REL_TARGET_PATH_COLUMN);
model.getRelationship(row).setDbRelationshipPath(dbRelationshipPath);
}
model.getRelationship(row).setMapKey(null);
}
table.repaint();
}
enterPressedCount = 1;
}
}
Aggregations