Search in sources :

Example 86 with ObjEntity

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);
        }
    }
}
Also used : EntityMergeSupport(org.apache.cayenne.dbsync.merge.context.EntityMergeSupport) DbEntitySyncUndoableEdit(org.apache.cayenne.modeler.undo.DbEntitySyncUndoableEdit) DbAttribute(org.apache.cayenne.map.DbAttribute) ProjectController(org.apache.cayenne.modeler.ProjectController) EntitySyncController(org.apache.cayenne.modeler.dialog.objentity.EntitySyncController) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) EntityEvent(org.apache.cayenne.map.event.EntityEvent)

Example 87 with ObjEntity

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));
            }
        }
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) RemoveRelationshipUndoableEdit(org.apache.cayenne.modeler.undo.RemoveRelationshipUndoableEdit) ProjectController(org.apache.cayenne.modeler.ProjectController) ConfirmRemoveDialog(org.apache.cayenne.modeler.dialog.ConfirmRemoveDialog)

Example 88 with ObjEntity

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;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) Entity(org.apache.cayenne.map.Entity) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) Iterator(java.util.Iterator)

Example 89 with ObjEntity

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));
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) RelationshipEvent(org.apache.cayenne.map.event.RelationshipEvent)

Example 90 with ObjEntity

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;
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) JTextComponent(javax.swing.text.JTextComponent)

Aggregations

ObjEntity (org.apache.cayenne.map.ObjEntity)294 Test (org.junit.Test)110 DbEntity (org.apache.cayenne.map.DbEntity)72 ObjAttribute (org.apache.cayenne.map.ObjAttribute)68 ObjRelationship (org.apache.cayenne.map.ObjRelationship)62 DataMap (org.apache.cayenne.map.DataMap)57 DbAttribute (org.apache.cayenne.map.DbAttribute)37 DbRelationship (org.apache.cayenne.map.DbRelationship)29 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)27 ObjectId (org.apache.cayenne.ObjectId)26 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)22 ArrayList (java.util.ArrayList)19 Embeddable (org.apache.cayenne.map.Embeddable)18 EntityResolver (org.apache.cayenne.map.EntityResolver)17 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)16 Expression (org.apache.cayenne.exp.Expression)15 Persistent (org.apache.cayenne.Persistent)12 EntityEvent (org.apache.cayenne.map.event.EntityEvent)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 Entity (org.apache.cayenne.map.Entity)11