public void paste(
Object where,
Object content,
DataChannelDescriptor domain,
DataMap map) {
final ProjectController mediator = getProjectController();
/**
* Add a little intelligence - if a tree leaf is selected, we can paste to a
* parent datamap
*/
if (isTreeLeaf(where) && isTreeLeaf(content)) {
where = mediator.getCurrentDataMap();
}
if ((where instanceof DataChannelDescriptor || where instanceof DataNodeDescriptor)
&& content instanceof DataMap) {
// paste DataMap to DataDomain or DataNode
DataMap dataMap = ((DataMap) content);
dataMap
.setName(getFreeName(new DataMapNameChecker(domain), dataMap
.getName()));
/**
* Update all names in the new DataMap, so that they would not conflict with
* names from other datamaps of this domain
*/
// add some intelligence - if we rename an entity, we should rename all links
// to it as well
Map<String, String> renamedDbEntities = new HashMap<String, String>();
Map<String, String> renamedObjEntities = new HashMap<String, String>();
Map<String, String> renamedEmbeddables = new HashMap<String, String>();
for (DbEntity dbEntity : dataMap.getDbEntities()) {
String oldName = dbEntity.getName();
dbEntity.setName(getFreeName(new DbEntityNameChecker(domain), dbEntity
.getName()));
if (!oldName.equals(dbEntity.getName())) {
renamedDbEntities.put(oldName, dbEntity.getName());
}
}
for (ObjEntity objEntity : dataMap.getObjEntities()) {
String oldName = objEntity.getName();
objEntity.setName(getFreeName(new ObjEntityNameChecker(domain), objEntity
.getName()));
if (!oldName.equals(objEntity.getName())) {
renamedObjEntities.put(oldName, objEntity.getName());
}
}
for (Embeddable embeddable : dataMap.getEmbeddables()) {
String oldName = embeddable.getClassName();
embeddable.setClassName(getFreeName(
new EmbeddableNameChecker(domain),
embeddable.getClassName()));
if (!oldName.equals(embeddable.getClassName())) {
renamedEmbeddables.put(oldName, embeddable.getClassName());
}
}
for (Procedure procedure : dataMap.getProcedures()) {
procedure.setName(getFreeName(new ProcedureNameChecker(domain), procedure
.getName()));
}
for (Query query : dataMap.getQueries()) {
((AbstractQuery) query).setName(getFreeName(
new QueryNameChecker(domain),
query.getName()));
}
// if an entity was renamed, we rename all links to it too
for (DbEntity dbEntity : dataMap.getDbEntities()) {
for (DbRelationship rel : dbEntity.getRelationships()) {
if (renamedDbEntities.containsKey(rel.getTargetEntityName())) {
rel.setTargetEntityName(renamedDbEntities.get(rel
.getTargetEntityName()));
}
}
}
for (ObjEntity objEntity : dataMap.getObjEntities()) {
if (renamedDbEntities.containsKey(objEntity.getDbEntityName())) {
objEntity.setDbEntityName(renamedDbEntities.get(objEntity
.getDbEntityName()));
}
if (renamedObjEntities.containsKey(objEntity.getSuperEntityName())) {
objEntity.setSuperEntityName(renamedDbEntities.get(objEntity
.getSuperEntityName()));
}
for (ObjRelationship rel : objEntity.getRelationships()) {
if (renamedObjEntities.containsKey(rel.getTargetEntityName())) {
rel.setTargetEntityName(renamedObjEntities.get(rel
.getTargetEntityName()));
}
}
}
mediator.addDataMap(this, dataMap);
}
else if (where instanceof DataMap) {
// paste DbEntity to DataMap
final DataMap dataMap = ((DataMap) where);
if (content instanceof DbEntity) {
DbEntity dbEntity = (DbEntity) content;
dbEntity.setName(getFreeName(new DbEntityNameChecker(domain), dbEntity
.getName()));
dataMap.addDbEntity(dbEntity);
CreateDbEntityAction.fireDbEntityEvent(this, mediator, dbEntity);
}
else if (content instanceof ObjEntity) {
// paste ObjEntity to DataMap
ObjEntity objEntity = (ObjEntity) content;
objEntity.setName(getFreeName(new ObjEntityNameChecker(domain), objEntity
.getName()));
dataMap.addObjEntity(objEntity);
CreateObjEntityAction.fireObjEntityEvent(
this,
mediator,
dataMap,
objEntity);
}
else if (content instanceof Embeddable) {
// paste Embeddable to DataMap
Embeddable embeddable = (Embeddable) content;
embeddable.setClassName(getFreeName(
new EmbeddableNameChecker(domain),
embeddable.getClassName()));
dataMap.addEmbeddable(embeddable);
CreateEmbeddableAction.fireEmbeddableEvent(
this,
mediator,
dataMap,
embeddable);
}
else if (content instanceof Query) {
// paste Query to DataMap
AbstractQuery query = (AbstractQuery) content;
/**
* Change Query root do current datamap's
*/
Object root = query.getRoot();
Object newRoot = root;
if (root instanceof ObjEntity) {
newRoot = dataMap.getObjEntity(((ObjEntity) root).getName());
}
else if (root instanceof DbEntity) {
newRoot = dataMap.getDbEntity(((DbEntity) root).getName());
}
else if (root instanceof Procedure) {
newRoot = dataMap.getProcedure(((Procedure) root).getName());
}
if (newRoot == null) {
JOptionPane
.showMessageDialog(
Application.getFrame(),
"Query root cannot be resolved. Pasting has not been performed.",
"Warning",
JOptionPane.WARNING_MESSAGE);
return;
}
query.setName(getFreeName(new QueryNameChecker(domain), query.getName()));
query.setDataMap(dataMap);
dataMap.addQuery(query);
QueryType.fireQueryEvent(this, mediator, dataMap, query);
}
else if (content instanceof Procedure) {
// paste Procedure to DataMap
Procedure procedure = (Procedure) content;
procedure.setName(getFreeName(new ProcedureNameChecker(domain), procedure
.getName()));
dataMap.addProcedure(procedure);
CreateProcedureAction.fireProcedureEvent(
this,
mediator,
dataMap,
procedure);
}
}
else if (where instanceof DbEntity) {
final DbEntity dbEntity = (DbEntity) where;
// attrs and rels must be unique in entity namespace
FreeNameChecker checker = new FreeNameChecker() {
public boolean isNameFree(String name) {
return dbEntity.getAttribute(name) == null
&& dbEntity.getRelationship(name) == null;
}
};
if (content instanceof DbAttribute) {
DbAttribute attr = (DbAttribute) content;
attr.setName(getFreeName(checker, attr.getName()));
dbEntity.addAttribute(attr);
CreateAttributeAction.fireDbAttributeEvent(this, mediator, mediator
.getCurrentDataMap(), dbEntity, attr);
}
else if (content instanceof DbRelationship) {
DbRelationship rel = (DbRelationship) content;
rel.setName(getFreeName(checker, rel.getName()));
dbEntity.addRelationship(rel);
CreateRelationshipAction.fireDbRelationshipEvent(
this,
mediator,
dbEntity,
rel);
}
}
else if (where instanceof ObjEntity) {
final ObjEntity objEntity = (ObjEntity) where;
// attrs and rels must be unique in entity namespace
FreeNameChecker checker = new FreeNameChecker() {
public boolean isNameFree(String name) {
return objEntity.getAttribute(name) == null
&& objEntity.getRelationship(name) == null;
}
};
if (content instanceof ObjAttribute) {
ObjAttribute attr = (ObjAttribute) content;
attr.setName(getFreeName(checker, attr.getName()));
objEntity.addAttribute(attr);
CreateAttributeAction.fireObjAttributeEvent(this, mediator, mediator
.getCurrentDataMap(), objEntity, attr);
}
else if (content instanceof ObjRelationship) {
ObjRelationship rel = (ObjRelationship) content;
rel.setName(getFreeName(checker, rel.getName()));