Search in sources :

Example 1 with ConfigurationNode

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

the class FileProjectSaver method save.

void save(Project project, Resource baseResource, boolean deleteOldResources) {
    Collection<ConfigurationNode> nodes = project.getRootNode().acceptVisitor(saveableNodesGetter);
    Collection<SaveUnit> units = new ArrayList<>(nodes.size());
    for (ConfigurationNode node : nodes) {
        String targetLocation = nameMapper.configurationLocation(node);
        Resource targetResource = baseResource.getRelativeResource(targetLocation);
        units.add(createSaveUnit(node, targetResource, null));
        for (ProjectExtension extension : extensions) {
            ConfigurationNodeVisitor<String> namingDelegate = extension.createNamingDelegate();
            SaverDelegate unitSaverDelegate = extension.createSaverDelegate();
            String fileName = node.acceptVisitor(namingDelegate);
            if (fileName != null) {
                // not null means that this should go to a separate file
                targetResource = baseResource.getRelativeResource(fileName);
                units.add(createSaveUnit(node, targetResource, unitSaverDelegate));
            }
        }
    }
    checkAccess(units);
    try {
        saveToTempFiles(units);
        saveCommit(units);
    } finally {
        clearTempFiles(units);
    }
    try {
        if (deleteOldResources) {
            clearRenamedFiles(units);
            Collection<URL> unusedResources = project.getUnusedResources();
            for (SaveUnit unit : units) {
                unusedResources.remove(unit.sourceConfiguration.getURL());
            }
            deleteUnusedFiles(unusedResources);
        }
    } catch (IOException ex) {
        throw new CayenneRuntimeException(ex);
    }
    // I guess we should reset projects state regardless of the value of
    // 'deleteOldResources'
    project.getUnusedResources().clear();
}
Also used : SaverDelegate(org.apache.cayenne.project.extension.SaverDelegate) ArrayList(java.util.ArrayList) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) IOException(java.io.IOException) URL(java.net.URL) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) ProjectExtension(org.apache.cayenne.project.extension.ProjectExtension)

Example 2 with ConfigurationNode

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

the class PropertyHandler method processElement.

@Override
protected boolean processElement(String namespaceURI, String localName, Attributes attributes) throws SAXException {
    switch(localName) {
        case PROPERTY_TAG:
            ConfigurationNode parentObject = getParentObject();
            String name = attributes.getValue("name");
            if (parentObject != null) {
                ObjectInfo info = metaData.get(parentObject, ObjectInfo.class);
                if (info == null) {
                    info = new ObjectInfo();
                    metaData.add(parentObject, info);
                }
                String oldValue = info.put(name, attributes.getValue("value"));
                if (oldValue != null) {
                    logger.warn("Duplicated property {} for object {}", name, parentObject);
                }
            }
            return true;
    }
    return false;
}
Also used : ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode)

Example 3 with ConfigurationNode

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

the class DefaultDbImportAction method saveLoaded.

/**
 * Save imported data.
 * This can create DataMap and/or Project files.
 */
protected void saveLoaded(DataMap dataMap, DbImportConfiguration config) throws MalformedURLException {
    ConfigurationTree<ConfigurationNode> projectRoot;
    if (config.getCayenneProject() == null) {
        // Old version of cdbimport, no Cayenne project, need to save only DataMap
        projectRoot = new ConfigurationTree<>(dataMap);
    } else {
        // Cayenne project is present
        DataChannelDescriptor dataChannelDescriptor;
        if (config.getCayenneProject().exists()) {
            // Cayenne project file exists, need to read it and push DataMap inside
            URLResource configurationResource = new URLResource(config.getCayenneProject().toURI().toURL());
            ConfigurationTree<DataChannelDescriptor> configurationTree = dataChannelDescriptorLoader.load(configurationResource);
            if (!configurationTree.getLoadFailures().isEmpty()) {
                throw new CayenneRuntimeException("Unable to load cayenne project %s, %s", config.getCayenneProject(), configurationTree.getLoadFailures().iterator().next().getDescription());
            }
            dataChannelDescriptor = configurationTree.getRootNode();
            // remove old copy of DataMap if it's there
            DataMap oldDataMap = dataChannelDescriptor.getDataMap(dataMap.getName());
            if (oldDataMap != null) {
                dataChannelDescriptor.getDataMaps().remove(oldDataMap);
            }
        } else {
            // No project file yet, can simply create empty project with resulting DataMap
            dataChannelDescriptor = new DataChannelDescriptor();
            dataChannelDescriptor.setName(getProjectNameFromFileName(config.getCayenneProject().getName()));
            dataChannelDescriptor.setConfigurationSource(new URLResource(config.getCayenneProject().toURI().toURL()));
            logger.info("Project file does not exist. New project will be saved into '" + config.getCayenneProject().getAbsolutePath());
        }
        dataChannelDescriptor.getDataMaps().add(dataMap);
        projectRoot = new ConfigurationTree<>(dataChannelDescriptor);
    }
    Project project = new Project(projectRoot);
    projectSaver.save(project);
    logger.info("");
    logger.info("All changes saved.");
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) Project(org.apache.cayenne.project.Project) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataMap(org.apache.cayenne.map.DataMap)

Example 4 with ConfigurationNode

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

the class ProjectTreeView method initTreeSelectionListener.

private void initTreeSelectionListener() {
    treeSelectionListener = new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            TreePath[] paths = getSelectionPaths();
            if (paths != null) {
                if (paths.length > 1) {
                    ConfigurationNode projectParentPath = null;
                    ConfigurationNode[] projectPaths = new ConfigurationNode[paths.length];
                    boolean commonParentPath = true;
                    for (int i = 0; i < paths.length; i++) {
                        projectPaths[i] = createProjectPath(paths[i]);
                        TreePath parentPath = paths[i].getParentPath();
                        if (i > 0 && parentPath != null && !parentPath.equals(paths[i - 1].getParentPath())) {
                            commonParentPath = false;
                        }
                    }
                    if (commonParentPath) {
                        TreePath parentPath = paths[0].getParentPath();
                        projectParentPath = createProjectPath(parentPath);
                    }
                    mediator.fireMultipleObjectsDisplayEvent(new MultipleObjectsDisplayEvent(this, projectPaths, projectParentPath));
                } else if (paths.length == 1) {
                    processSelection(paths[0]);
                }
            }
        }

        /**
         * Converts TreePath to Object
         */
        private ConfigurationNode createProjectPath(TreePath treePath) {
            Object[] path = treePath.getPath();
            DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) path[path.length - 1];
            return (ConfigurationNode) treeNode.getUserObject();
        }
    };
    addTreeSelectionListener(treeSelectionListener);
}
Also used : MultipleObjectsDisplayEvent(org.apache.cayenne.modeler.event.MultipleObjectsDisplayEvent) TreePath(javax.swing.tree.TreePath) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) TreeSelectionListener(javax.swing.event.TreeSelectionListener) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Example 5 with ConfigurationNode

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

the class ProjectTreeView method currentObjectsChanged.

public void currentObjectsChanged(MultipleObjectsDisplayEvent e, Application application) {
    if (e.getSource() == this || e.getParentNode() == null) {
        return;
    }
    ConfigurationNode[] nodes = e.getNodes();
    TreePath[] treePaths = new TreePath[nodes.length];
    for (int i = 0; i < nodes.length; i++) {
        DefaultMutableTreeNode treeNode = getProjectModel().getNodeForObjectPath(new Object[] { e.getParentNode(), nodes[i] });
        if (treeNode != null) {
            treePaths[i] = new TreePath(treeNode.getPath());
        } else if (e.getParentNode() == nodes[i]) {
            treeNode = getProjectModel().getNodeForObjectPath(new Object[] { e.getParentNode() });
            treePaths[i] = new TreePath(treeNode.getPath());
        }
    }
    if (!isVisible(treePaths[0])) {
        makeVisible(treePaths[0]);
        Rectangle bounds = getPathBounds(treePaths[0]);
        if (bounds != null) {
            bounds.height = getVisibleRect().height;
            scrollRectToVisible(bounds);
        }
    }
    setSelectionPaths(treePaths);
}
Also used : TreePath(javax.swing.tree.TreePath) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) Rectangle(java.awt.Rectangle)

Aggregations

ConfigurationNode (org.apache.cayenne.configuration.ConfigurationNode)16 ArrayList (java.util.ArrayList)5 DataMap (org.apache.cayenne.map.DataMap)5 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)4 MultipleObjectsDisplayEvent (org.apache.cayenne.modeler.event.MultipleObjectsDisplayEvent)3 URLResource (org.apache.cayenne.resource.URLResource)3 File (java.io.File)2 URL (java.net.URL)2 EventObject (java.util.EventObject)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 TreePath (javax.swing.tree.TreePath)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 ProjectController (org.apache.cayenne.modeler.ProjectController)2 Resource (org.apache.cayenne.resource.Resource)2 Rectangle (java.awt.Rectangle)1 IOException (java.io.IOException)1 BackingStoreException (java.util.prefs.BackingStoreException)1 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)1 TreeSelectionListener (javax.swing.event.TreeSelectionListener)1 CompoundEdit (javax.swing.undo.CompoundEdit)1