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();
}
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;
}
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.");
}
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);
}
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);
}
Aggregations