public void paste(Node parent,List children,int position,NodeModel model,int actionType){
Node p=(parent==null)?root:parent;
Project project=null;
ResourcePool resourcePool=null;
if (model.getDataFactory() instanceof Project)
project=(Project)model.getDataFactory();
else if(model.getDataFactory() instanceof ResourcePool)
resourcePool=(ResourcePool)model.getDataFactory();
int subprojectLevel=getChildrenSubprojectLevel(parent);
// ArrayList trees=new ArrayList();
// HierarchyUtils.extractParents(children,trees);
int childCount=p.getChildCount();
if (position>childCount){
NodeFactory nodeFactory=NodeFactory.getInstance();
Node node=nodeFactory.createVoidNode();
for (int i=childCount;i<position;i++){
setSubprojectLevel(node,subprojectLevel);
p.add(node);
}
}
int j=position;
for (Iterator i=/*trees*/children.iterator();i.hasNext();){
Node node=(Node)i.next();
if ((project!=null && node.getImpl() instanceof Task)||
(resourcePool!=null && node.getImpl() instanceof Resource)||
node.isVoid()){
//if (node.getImpl() instanceof Task) System.out.println("PASTE parent="+parent+":"+(parent==null?"X":parent.isInSubproject())+", node="+node+":"+node.isInSubproject());
setSubprojectLevel(node,subprojectLevel);
//if (node.getImpl() instanceof Task) System.out.println("PASTE node in sub="+node.isInSubproject());
if (position==-1) p.add(node);
else p.insert(node,j++);
}
}
Node[] descendants=addDescendants(/*trees*/children);
ArrayList<Dependency> dependencies=new ArrayList<Dependency>();
boolean doTransaction = model.getDocument() != null && descendants.length > 0;
int transactionId = 0;
if (doTransaction)
transactionId = model.getDocument().fireMultipleTransaction(0,true);
ArrayList<Node> insertedNodes=new ArrayList<Node>(descendants.length);
if (project!=null){
HashMap<Long,Resource> resourceMap=new HashMap<Long,Resource>();
for (Resource r: (Collection<Resource>)project.getResourcePool().getResourceList())
resourceMap.put(r.getUniqueId(),r);
HashMap<Long,Task> taskMap=null;
if (Environment.isKeepExternalLinks()){
taskMap=new HashMap<Long,Task>();
for (Task t: (Collection<Task>)project.getTasks()) //use model instead?
taskMap.put(t.getUniqueId(),t);
}
Project owningProject;
if (parent!=null && parent.getImpl() instanceof Task){
Task task=(Task)parent.getImpl();
if (task.isSubproject()) owningProject=((SubProj)task).getSubproject();
else owningProject=task.getOwningProject();
}else owningProject=(Project)model.getDataFactory();
for (int i=0;i<descendants.length;i++){
if (descendants[i].getImpl() instanceof Task){
Task task=(Task)descendants[i].getImpl();
Node parentSubproject=getParentSubproject((Node)descendants[i].getParent());
if (parentSubproject!=null) owningProject=((SubProj)parentSubproject.getImpl()).getSubproject();
if (!task.isExternal()) // fixes subproject bug with external links
task.setProjectId(owningProject.getUniqueId()); //useful?
owningProject.validateObject(task,model,this,null,false);
Set<Dependency> depsSet=new HashSet<Dependency>();
List pdeps=task.getDependencyList(true);
if (pdeps!=null&&pdeps.size()>0){
if (Environment.isKeepExternalLinks()){
for (Iterator k=pdeps.iterator();k.hasNext();){
Dependency d=(Dependency)k.next();
if (!(d.getPredecessor() instanceof Task)){
TaskLinkReference ref=(TaskLinkReference)d.getPredecessor();
Task t=taskMap.get(ref.getUniqueId());
if (t==null){
k.remove();
continue;
} else{
d.setPredecessor(t);
t.getSuccessorList().add(d);
//DependencyService.getInstance().updateSentinels(d);
//DependencyService.getInstance().connect(d, this);
}
}
depsSet.add(d);
}
}
}
List sdeps=task.getDependencyList(false);
if (sdeps!=null&&sdeps.size()>0){
if (Environment.isKeepExternalLinks()){
for (Iterator k=sdeps.iterator();k.hasNext();){
Dependency d=(Dependency)k.next();
if (!(d.getSuccessor() instanceof Task)){
TaskLinkReference ref=(TaskLinkReference)d.getSuccessor();
Task t=taskMap.get(ref.getUniqueId());
if (t==null){
k.remove();
continue;
} else{
d.setSuccessor(t);
t.getPredecessorList().add(d);
//DependencyService.getInstance().updateSentinels(d);
//DependencyService.getInstance().connect(d, this);
}
}
depsSet.add(d);
}
}
}
dependencies.addAll(depsSet);
//check assignments, if resource not present change it to unassigned
for (int s=0;s<Settings.numBaselines();s++){
TaskSnapshot snapshot=(TaskSnapshot)task.getSnapshot(new Integer(s));
if (snapshot==null) continue;
AssociationList snapshotAssignments=snapshot.getHasAssignments().getAssignments();
if (snapshotAssignments.size()>0){
// ArrayList<Assignment> assignmentsToLink=new ArrayList<Assignment>();
for (Iterator a=snapshotAssignments.listIterator();a.hasNext();){
Assignment assignment=(Assignment)a.next();
Resource resource=assignment.getResource();
if (resource==ResourceImpl.getUnassignedInstance()) continue;
Resource destResource=resourceMap.get(resource.getUniqueId());
if (destResource!=null){
if (Snapshottable.CURRENT.equals(s)){
if (destResource!=resource){ // use destination resource
resource=destResource;
assignment.getDetail().setResource(resource);
}
// assignmentsToLink.add(assignment);
resource.addAssignment(assignment);
NodeUndoInfo undo=new NodeUndoInfo(false);
((ResourcePool)assignment.getResource().getDocument()).getObjectEventManager().fireCreateEvent(this,assignment,undo);
}
}else{
assignment.getDetail().setResource(ResourceImpl.getUnassignedInstance());
}
}
// for (Assignment assignment: assignmentsToLink){
// AssignmentService.getInstance().remove(assignmentsToLink, this,false);
// AssignmentService.getInstance().connect(assignment, this);
// }
}
}
project.initializeId(task);
project.addPastedTask(task);
insertedNodes.add(descendants[i]);
}
}
}else if (resourcePool!=null){
for (int i=0;i<descendants.length;i++){
if (descendants[i].getImpl() instanceof Resource){
Resource resource=(Resource)descendants[i].getImpl();
model.getDataFactory().validateObject(resource,model,this,null,false);
resourcePool.initializeId(resource);
insertedNodes.add(descendants[i]);
}
}
}