use of org.apache.cayenne.configuration.ConfigurationNode in project cayenne by apache.
the class ProjectTest method testRootNode.
@Test
public void testRootNode() {
ConfigurationNode object = new ConfigurationNode() {
public <T> T acceptVisitor(ConfigurationNodeVisitor<T> visitor) {
return null;
}
};
Project project = new Project(new ConfigurationTree<ConfigurationNode>(object));
assertSame(object, project.getRootNode());
assertFalse(project.isModified());
project.setModified(true);
assertTrue(project.isModified());
}
use of org.apache.cayenne.configuration.ConfigurationNode 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);
}
}
use of org.apache.cayenne.configuration.ConfigurationNode 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);
}
}
use of org.apache.cayenne.configuration.ConfigurationNode in project cayenne by apache.
the class ProjectController method getCurrentObject.
/**
* Returns currently selected object, null if there are none, List if there
* are several
*/
public Object getCurrentObject() {
if (getCurrentObjEntity() != null) {
return getCurrentObjEntity();
} else if (getCurrentDbEntity() != null) {
return getCurrentDbEntity();
} else if (getCurrentEmbeddable() != null) {
return getCurrentEmbeddable();
} else if (getCurrentQuery() != null) {
return getCurrentQuery();
} else if (getCurrentProcedure() != null) {
return getCurrentProcedure();
} else if (getCurrentDataMap() != null) {
return getCurrentDataMap();
} else if (getCurrentDataNode() != null) {
return getCurrentDataNode();
} else if (getCurrentDataChanel() != null) {
return getCurrentDataChanel();
} else if (getCurrentPaths() != null) {
// multiple objects
ConfigurationNode[] paths = getCurrentPaths();
ConfigurationNodeParentGetter parentGetter = getApplication().getInjector().getInstance(ConfigurationNodeParentGetter.class);
Object parent = parentGetter.getParent(paths[0]);
List<ConfigurationNode> result = new ArrayList<>();
result.addAll(Arrays.asList(paths));
/*
* Here we sort the list of objects to minimize the risk that
* objects will be pasted incorrectly. For instance, ObjEntity
* should go before Query, to increase chances that Query's root
* would be set.
*/
Collections.sort(result, parent instanceof DataMap ? Comparators.getDataMapChildrenComparator() : Comparators.getDataDomainChildrenComparator());
return result;
}
return null;
}
use of org.apache.cayenne.configuration.ConfigurationNode in project cayenne by apache.
the class EntityTreeModel method sortedChildren.
private ConfigurationNode[] sortedChildren(Object node) {
Entity entity = entityForNonLeafNode(node);
// may happen in incomplete relationships
if (entity == null) {
return new ConfigurationNode[0];
}
ConfigurationNode[] sortedForNode = sortedChildren.get(node);
if (sortedForNode == null) {
Collection<? extends Attribute> attributes = entity.getAttributes();
Collection<? extends Relationship> relationships = entity.getRelationships();
List<ConfigurationNode> nodes = new ArrayList<>();
// combine two collections in an array
for (Attribute attr : attributes) {
if (filter == null || filter.attributeMatch(node, attr)) {
nodes.add((ConfigurationNode) attr);
}
}
for (Relationship rel : relationships) {
if (filter == null || filter.relationshipMatch(node, rel)) {
nodes.add((ConfigurationNode) rel);
}
}
sortedForNode = nodes.toArray(new ConfigurationNode[0]);
Arrays.sort(sortedForNode, Comparators.getEntityChildrenComparator());
sortedChildren.put(node, sortedForNode);
}
return sortedForNode;
}
Aggregations