Search in sources :

Example 81 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class ImportDataMapAction method importDataMap.

protected void importDataMap() {
    File dataMapFile = selectDataMap(Application.getFrame());
    if (dataMapFile == null) {
        return;
    }
    DataMap newMap;
    try {
        URL url = dataMapFile.toURI().toURL();
        DataMapLoader loader = application.getInjector().getInstance(DataMapLoader.class);
        newMap = loader.load(new URLResource(url));
        ConfigurationNode root = getProjectController().getProject().getRootNode();
        newMap.setName(NameBuilder.builder(newMap, root).baseName(newMap.getName()).name());
        Resource baseResource = ((DataChannelDescriptor) root).getConfigurationSource();
        if (baseResource != null) {
            Resource dataMapResource = baseResource.getRelativeResource(nameMapper.configurationLocation(newMap));
            newMap.setConfigurationSource(dataMapResource);
        }
        getProjectController().addDataMap(this, newMap);
    } catch (Exception ex) {
        logObj.info("Error importing DataMap.", ex);
        JOptionPane.showMessageDialog(Application.getFrame(), "Error reading DataMap: " + ex.getMessage(), "Can't Open DataMap", JOptionPane.OK_OPTION);
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) File(java.io.File) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap)

Example 82 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class ImportEOModelAction method addDataMap.

/**
 * Adds DataMap into the project.
 */
protected void addDataMap(DataMap map, DataMap currentMap) {
    ProjectController mediator = getProjectController();
    if (currentMap != null) {
        // merge with existing map... have to memorize map state before and after
        // to do the right events
        Collection<ObjEntity> originalOE = new ArrayList<>(currentMap.getObjEntities());
        Collection<DbEntity> originalDE = new ArrayList<>(currentMap.getDbEntities());
        Collection<QueryDescriptor> originalQueries = new ArrayList<>(currentMap.getQueryDescriptors());
        currentMap.mergeWithDataMap(map);
        map = currentMap;
        // postprocess changes
        Collection<ObjEntity> newOE = new ArrayList<>(currentMap.getObjEntities());
        Collection<DbEntity> newDE = new ArrayList<>(currentMap.getDbEntities());
        Collection<QueryDescriptor> newQueries = new ArrayList<>(currentMap.getQueryDescriptors());
        EntityEvent entityEvent = new EntityEvent(Application.getFrame(), null);
        QueryEvent queryEvent = new QueryEvent(Application.getFrame(), null);
        // 1. ObjEntities
        Collection<ObjEntity> addedOE = new ArrayList<>(newOE);
        addedOE.removeAll(originalOE);
        for (ObjEntity e : addedOE) {
            entityEvent.setEntity(e);
            entityEvent.setId(MapEvent.ADD);
            mediator.fireObjEntityEvent(entityEvent);
        }
        Collection<ObjEntity> removedOE = new ArrayList<>(originalOE);
        removedOE.removeAll(newOE);
        for (ObjEntity e : removedOE) {
            entityEvent.setEntity(e);
            entityEvent.setId(MapEvent.REMOVE);
            mediator.fireObjEntityEvent(entityEvent);
        }
        // 2. DbEntities
        Collection<DbEntity> addedDE = new ArrayList<>(newDE);
        addedDE.removeAll(originalDE);
        for (DbEntity e : addedDE) {
            entityEvent.setEntity(e);
            entityEvent.setId(MapEvent.ADD);
            mediator.fireDbEntityEvent(entityEvent);
        }
        Collection<DbEntity> removedDE = new ArrayList<>(originalDE);
        removedDE.removeAll(newDE);
        for (DbEntity e : removedDE) {
            entityEvent.setEntity(e);
            entityEvent.setId(MapEvent.REMOVE);
            mediator.fireDbEntityEvent(entityEvent);
        }
        // 3. queries
        Collection<QueryDescriptor> addedQueries = new ArrayList<>(newQueries);
        addedQueries.removeAll(originalQueries);
        for (QueryDescriptor q : addedQueries) {
            queryEvent.setQuery(q);
            queryEvent.setId(MapEvent.ADD);
            mediator.fireQueryEvent(queryEvent);
        }
        Collection<QueryDescriptor> removedQueries = new ArrayList<>(originalQueries);
        removedQueries.removeAll(newQueries);
        for (QueryDescriptor q : removedQueries) {
            queryEvent.setQuery(q);
            queryEvent.setId(MapEvent.REMOVE);
            mediator.fireQueryEvent(queryEvent);
        }
        mediator.fireDataMapDisplayEvent(new DataMapDisplayEvent(Application.getFrame(), map, (DataChannelDescriptor) mediator.getProject().getRootNode(), mediator.getCurrentDataNode()));
    } else {
        // fix DataMap name, as there maybe a map with the same name already
        ConfigurationNode root = mediator.getProject().getRootNode();
        map.setName(NameBuilder.builder(map, root).baseName(map.getName()).name());
        // side effect of this operation is that if a node was created, this DataMap
        // will be linked with it...
        mediator.addDataMap(Application.getFrame(), map);
    }
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ArrayList(java.util.ArrayList) QueryEvent(org.apache.cayenne.configuration.event.QueryEvent) DataMapDisplayEvent(org.apache.cayenne.modeler.event.DataMapDisplayEvent) ProjectController(org.apache.cayenne.modeler.ProjectController) QueryDescriptor(org.apache.cayenne.map.QueryDescriptor) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) EntityEvent(org.apache.cayenne.map.event.EntityEvent)

Example 83 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class NewProjectAction method performAction.

public void performAction(ActionEvent e) {
    CayenneModelerController controller = Application.getInstance().getFrameController();
    // Save and close (if needed) currently open project.
    if (getCurrentProject() != null && !closeProject(true)) {
        return;
    }
    DataChannelDescriptor dataChannelDescriptor = new DataChannelDescriptor();
    dataChannelDescriptor.setName(NameBuilder.builder(dataChannelDescriptor).name());
    Project project = new Project(new ConfigurationTree<DataChannelDescriptor>(dataChannelDescriptor));
    controller.projectOpenedAction(project);
    // select default domain
    getProjectController().fireDomainDisplayEvent(new DomainDisplayEvent(this, dataChannelDescriptor));
}
Also used : Project(org.apache.cayenne.project.Project) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DomainDisplayEvent(org.apache.cayenne.modeler.event.DomainDisplayEvent) CayenneModelerController(org.apache.cayenne.modeler.CayenneModelerController)

Example 84 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class PasteAction method paste.

/**
 * Pastes single object
 */
public void paste(Object where, Object content, DataChannelDescriptor dataChannelDescriptor, DataMap map) {
    final ProjectController mediator = getProjectController();
    /**
     * Add a little intelligence - if a tree leaf is selected, we can paste to a
     * parent datamap
     */
    if (isTreeLeaf(where) && isTreeLeaf(content)) {
        where = mediator.getCurrentDataMap();
    }
    if ((where instanceof DataChannelDescriptor || where instanceof DataNodeDescriptor) && content instanceof DataMap) {
        // paste DataMap to DataDomain or DataNode
        DataMap dataMap = ((DataMap) content);
        dataMap.setName(NameBuilder.builder(dataMap, dataChannelDescriptor).baseName(dataMap.getName()).dupesPattern(COPY_PATTERN).name());
        /**
         * Update all names in the new DataMap, so that they would not conflict with
         * names from other datamaps of this domain
         */
        // add some intelligence - if we rename an entity, we should rename all links
        // to it as well
        Map<String, String> renamedDbEntities = new HashMap<>();
        Map<String, String> renamedObjEntities = new HashMap<>();
        Map<String, String> renamedEmbeddables = new HashMap<>();
        for (DbEntity dbEntity : dataMap.getDbEntities()) {
            String oldName = dbEntity.getName();
            dbEntity.setName(NameBuilder.builder(dbEntity, dataMap).baseName(dbEntity.getName()).dupesPattern(COPY_PATTERN).name());
            if (!oldName.equals(dbEntity.getName())) {
                renamedDbEntities.put(oldName, dbEntity.getName());
            }
        }
        for (ObjEntity objEntity : dataMap.getObjEntities()) {
            String oldName = objEntity.getName();
            objEntity.setName(NameBuilder.builder(objEntity, dataMap).baseName(objEntity.getName()).dupesPattern(COPY_PATTERN).name());
            if (!oldName.equals(objEntity.getName())) {
                renamedObjEntities.put(oldName, objEntity.getName());
            }
        }
        for (Embeddable embeddable : dataMap.getEmbeddables()) {
            String oldName = embeddable.getClassName();
            embeddable.setClassName(NameBuilder.builder(embeddable, dataMap).baseName(embeddable.getClassName()).dupesPattern(COPY_PATTERN).name());
            if (!oldName.equals(embeddable.getClassName())) {
                renamedEmbeddables.put(oldName, embeddable.getClassName());
            }
        }
        for (Procedure procedure : dataMap.getProcedures()) {
            procedure.setName(NameBuilder.builder(procedure, dataMap).baseName(procedure.getName()).dupesPattern(COPY_PATTERN).name());
        }
        for (QueryDescriptor query : dataMap.getQueryDescriptors()) {
            query.setName(NameBuilder.builder(query, dataMap).baseName(query.getName()).dupesPattern(COPY_PATTERN).name());
        }
        // if an entity was renamed, we rename all links to it too
        for (DbEntity dbEntity : dataMap.getDbEntities()) {
            for (DbRelationship rel : dbEntity.getRelationships()) {
                if (renamedDbEntities.containsKey(rel.getTargetEntityName())) {
                    rel.setTargetEntityName(renamedDbEntities.get(rel.getTargetEntityName()));
                }
            }
        }
        for (ObjEntity objEntity : dataMap.getObjEntities()) {
            if (renamedDbEntities.containsKey(objEntity.getDbEntityName())) {
                objEntity.setDbEntityName(renamedDbEntities.get(objEntity.getDbEntityName()));
            }
            if (renamedObjEntities.containsKey(objEntity.getSuperEntityName())) {
                objEntity.setSuperEntityName(renamedDbEntities.get(objEntity.getSuperEntityName()));
            }
            for (ObjRelationship rel : objEntity.getRelationships()) {
                if (renamedObjEntities.containsKey(rel.getTargetEntityName())) {
                    rel.setTargetEntityName(renamedObjEntities.get(rel.getTargetEntityName()));
                }
            }
        }
        mediator.addDataMap(this, dataMap);
    } else if (where instanceof DataMap) {
        // paste DbEntity to DataMap
        final DataMap dataMap = ((DataMap) where);
        // clear data map parent cache
        clearDataMapCache(dataMap);
        if (content instanceof DbEntity) {
            DbEntity dbEntity = (DbEntity) content;
            dbEntity.setName(NameBuilder.builder(dbEntity, dataMap).baseName(dbEntity.getName()).dupesPattern(COPY_PATTERN).name());
            dataMap.addDbEntity(dbEntity);
            CreateDbEntityAction.fireDbEntityEvent(this, mediator, dbEntity);
        } else if (content instanceof ObjEntity) {
            // paste ObjEntity to DataMap
            ObjEntity objEntity = (ObjEntity) content;
            objEntity.setName(NameBuilder.builder(objEntity, dataMap).baseName(objEntity.getName()).dupesPattern(COPY_PATTERN).name());
            dataMap.addObjEntity(objEntity);
            CreateObjEntityAction.fireObjEntityEvent(this, mediator, dataMap, objEntity);
        } else if (content instanceof Embeddable) {
            // paste Embeddable to DataMap
            Embeddable embeddable = (Embeddable) content;
            embeddable.setClassName(NameBuilder.builder(embeddable, dataMap).baseName(embeddable.getClassName()).dupesPattern(COPY_PATTERN).name());
            dataMap.addEmbeddable(embeddable);
            CreateEmbeddableAction.fireEmbeddableEvent(this, mediator, dataMap, embeddable);
        } else if (content instanceof QueryDescriptor) {
            QueryDescriptor query = (QueryDescriptor) content;
            query.setName(NameBuilder.builder(query, dataMap).dupesPattern(COPY_PATTERN).baseName(query.getName()).name());
            query.setDataMap(dataMap);
            dataMap.addQueryDescriptor(query);
            QueryType.fireQueryEvent(this, mediator, dataMap, query);
        } else if (content instanceof Procedure) {
            // paste Procedure to DataMap
            Procedure procedure = (Procedure) content;
            procedure.setName(NameBuilder.builder(procedure, dataMap).dupesPattern(COPY_PATTERN).baseName(procedure.getName()).name());
            dataMap.addProcedure(procedure);
            CreateProcedureAction.fireProcedureEvent(this, mediator, dataMap, procedure);
        }
    } else if (where instanceof DbEntity) {
        final DbEntity dbEntity = (DbEntity) where;
        if (content instanceof DbAttribute) {
            DbAttribute attr = (DbAttribute) content;
            attr.setName(NameBuilder.builder(attr, dbEntity).dupesPattern(COPY_PATTERN).baseName(attr.getName()).name());
            dbEntity.addAttribute(attr);
            CreateAttributeAction.fireDbAttributeEvent(this, mediator, mediator.getCurrentDataMap(), dbEntity, attr);
        } else if (content instanceof DbRelationship) {
            DbRelationship rel = (DbRelationship) content;
            rel.setName(NameBuilder.builder(rel, dbEntity).baseName(rel.getName()).dupesPattern(COPY_PATTERN).name());
            dbEntity.addRelationship(rel);
            CreateRelationshipAction.fireDbRelationshipEvent(this, mediator, dbEntity, rel);
        }
    } else if (where instanceof ObjEntity) {
        ObjEntity objEntity = (ObjEntity) where;
        if (content instanceof ObjAttribute) {
            ObjAttribute attr = (ObjAttribute) content;
            attr.setName(NameBuilder.builder(attr, objEntity).baseName(attr.getName()).dupesPattern(COPY_PATTERN).name());
            objEntity.addAttribute(attr);
            CreateAttributeAction.fireObjAttributeEvent(this, mediator, mediator.getCurrentDataMap(), objEntity, attr);
        } else if (content instanceof ObjRelationship) {
            ObjRelationship rel = (ObjRelationship) content;
            rel.setName(NameBuilder.builder(rel, objEntity).baseName(rel.getName()).dupesPattern(COPY_PATTERN).name());
            objEntity.addRelationship(rel);
            CreateRelationshipAction.fireObjRelationshipEvent(this, mediator, objEntity, rel);
        } else if (content instanceof ObjCallbackMethod) {
            ObjCallbackMethod method = (ObjCallbackMethod) content;
            method.setName(NameBuilder.builderForCallbackMethod(objEntity).baseName(method.getName()).dupesPattern(COPY_PATTERN).name());
            objEntity.getCallbackMap().getCallbackDescriptor(mediator.getCurrentCallbackType().getType()).addCallbackMethod(method.getName());
            CallbackMethodEvent ce = new CallbackMethodEvent(this, null, method.getName(), MapEvent.ADD);
            getProjectController().fireCallbackMethodEvent(ce);
        }
    } else if (where instanceof Embeddable) {
        final Embeddable embeddable = (Embeddable) where;
        if (content instanceof EmbeddableAttribute) {
            EmbeddableAttribute attr = (EmbeddableAttribute) content;
            attr.setName(NameBuilder.builder(attr, embeddable).baseName(attr.getName()).dupesPattern(COPY_PATTERN).name());
            embeddable.addAttribute(attr);
            CreateAttributeAction.fireEmbeddableAttributeEvent(this, mediator, embeddable, attr);
        }
    } else if (where instanceof Procedure) {
        // paste param to procedure
        final Procedure procedure = (Procedure) where;
        if (content instanceof ProcedureParameter) {
            ProcedureParameter param = (ProcedureParameter) content;
            param.setName(NameBuilder.builder(param, procedure).baseName(param.getName()).dupesPattern(COPY_PATTERN).name());
            procedure.addCallParameter(param);
            CreateProcedureParameterAction.fireProcedureParameterEvent(this, mediator, procedure, param);
        }
    }
}
Also used : ProcedureParameter(org.apache.cayenne.map.ProcedureParameter) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) HashMap(java.util.HashMap) DbAttribute(org.apache.cayenne.map.DbAttribute) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) ProjectController(org.apache.cayenne.modeler.ProjectController) DataMap(org.apache.cayenne.map.DataMap) Embeddable(org.apache.cayenne.map.Embeddable) QueryDescriptor(org.apache.cayenne.map.QueryDescriptor) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) CallbackMethodEvent(org.apache.cayenne.modeler.event.CallbackMethodEvent) DbRelationship(org.apache.cayenne.map.DbRelationship) Procedure(org.apache.cayenne.map.Procedure) EmbeddableAttribute(org.apache.cayenne.map.EmbeddableAttribute) ObjCallbackMethod(org.apache.cayenne.modeler.editor.ObjCallbackMethod)

Example 85 with DataChannelDescriptor

use of org.apache.cayenne.configuration.DataChannelDescriptor in project cayenne by apache.

the class RemoveAction method removeDataMap.

public void removeDataMap(DataMap map) {
    ProjectController mediator = getProjectController();
    DataChannelDescriptor domain = (DataChannelDescriptor) mediator.getProject().getRootNode();
    DataMapEvent e = new DataMapEvent(Application.getFrame(), map, MapEvent.REMOVE);
    e.setDomain((DataChannelDescriptor) mediator.getProject().getRootNode());
    domain.getDataMaps().remove(map);
    if (map.getConfigurationSource() != null) {
        URL mapURL = map.getConfigurationSource().getURL();
        Collection<URL> unusedResources = getCurrentProject().getUnusedResources();
        unusedResources.add(mapURL);
    }
    for (DataNodeDescriptor node : domain.getNodeDescriptors()) {
        if (node.getDataMapNames().contains(map.getName())) {
            removeDataMapFromDataNode(node, map);
        }
    }
    mediator.fireDataMapEvent(e);
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) ProjectController(org.apache.cayenne.modeler.ProjectController) DataMapEvent(org.apache.cayenne.configuration.event.DataMapEvent) URL(java.net.URL)

Aggregations

DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)100 DataMap (org.apache.cayenne.map.DataMap)50 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)28 Test (org.junit.Test)20 ObjEntity (org.apache.cayenne.map.ObjEntity)18 DbEntity (org.apache.cayenne.map.DbEntity)12 ProjectController (org.apache.cayenne.modeler.ProjectController)12 URL (java.net.URL)11 Embeddable (org.apache.cayenne.map.Embeddable)11 ArrayList (java.util.ArrayList)10 URLResource (org.apache.cayenne.resource.URLResource)10 ObjAttribute (org.apache.cayenne.map.ObjAttribute)8 QueryDescriptor (org.apache.cayenne.map.QueryDescriptor)8 EntityDisplayEvent (org.apache.cayenne.modeler.event.EntityDisplayEvent)8 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)7 DataNodeEvent (org.apache.cayenne.configuration.event.DataNodeEvent)6 Injector (org.apache.cayenne.di.Injector)6 Procedure (org.apache.cayenne.map.Procedure)6 ProcedureDisplayEvent (org.apache.cayenne.modeler.event.ProcedureDisplayEvent)6 Resource (org.apache.cayenne.resource.Resource)6