/**
* 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 originalOE = new ArrayList(currentMap.getObjEntities());
Collection originalDE = new ArrayList(currentMap.getDbEntities());
Collection originalQueries = new ArrayList(currentMap.getQueries());
currentMap.mergeWithDataMap(map);
map = currentMap;
// postprocess changes
Collection newOE = new ArrayList(currentMap.getObjEntities());
Collection newDE = new ArrayList(currentMap.getDbEntities());
Collection newQueries = new ArrayList(currentMap.getQueries());
EntityEvent entityEvent = new EntityEvent(Application.getFrame(), null);
QueryEvent queryEvent = new QueryEvent(Application.getFrame(), null);
Collection addedOE = CollectionUtils.subtract(newOE, originalOE);
Iterator it = addedOE.iterator();
while (it.hasNext()) {
Entity e = (Entity) it.next();
entityEvent.setEntity(e);
entityEvent.setId(MapEvent.ADD);
mediator.fireObjEntityEvent(entityEvent);
}
Collection removedOE = CollectionUtils.subtract(originalOE, newOE);
it = removedOE.iterator();
while (it.hasNext()) {
Entity e = (Entity) it.next();
entityEvent.setEntity(e);
entityEvent.setId(MapEvent.REMOVE);
mediator.fireObjEntityEvent(entityEvent);
}
Collection addedDE = CollectionUtils.subtract(newDE, originalDE);
it = addedDE.iterator();
while (it.hasNext()) {
Entity e = (Entity) it.next();
entityEvent.setEntity(e);
entityEvent.setId(MapEvent.ADD);
mediator.fireDbEntityEvent(entityEvent);
}
Collection removedDE = CollectionUtils.subtract(originalDE, newDE);
it = removedDE.iterator();
while (it.hasNext()) {
Entity e = (Entity) it.next();
entityEvent.setEntity(e);
entityEvent.setId(MapEvent.REMOVE);
mediator.fireDbEntityEvent(entityEvent);
}
// queries
Collection addedQueries = CollectionUtils.subtract(newQueries, originalQueries);
it = addedQueries.iterator();
while (it.hasNext()) {
Query q = (Query) it.next();
queryEvent.setQuery(q);
queryEvent.setId(MapEvent.ADD);
mediator.fireQueryEvent(queryEvent);
}
Collection removedQueries = CollectionUtils.subtract(originalQueries, newQueries);
it = removedQueries.iterator();
while (it.hasNext()) {
Query q = (Query) it.next();
queryEvent.setQuery(q);
queryEvent.setId(MapEvent.REMOVE);
mediator.fireQueryEvent(queryEvent);
}
mediator.fireDataMapDisplayEvent(new DataMapDisplayEvent(Application
.getFrame(), map, mediator.getCurrentDataDomain(), mediator
.getCurrentDataNode()));
}
else {
// fix DataMap name, as there maybe a map with the same name already
DataDomain domain = mediator.getCurrentDataDomain();
map.setName(NamedObjectFactory.createName(DataMap.class, domain, map
.getName()));
// side effect of this operation is that if a node was created, this DataMap
// will be linked with it...
mediator.addDataMap(Application.getFrame(), map);
}
}