Search in sources :

Example 1 with SaverDelegate

use of org.apache.cayenne.project.extension.SaverDelegate 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)

Aggregations

IOException (java.io.IOException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 ConfigurationNode (org.apache.cayenne.configuration.ConfigurationNode)1 ProjectExtension (org.apache.cayenne.project.extension.ProjectExtension)1 SaverDelegate (org.apache.cayenne.project.extension.SaverDelegate)1 Resource (org.apache.cayenne.resource.Resource)1 URLResource (org.apache.cayenne.resource.URLResource)1