1 package org.simantics.interop.update.model;
3 import java.util.HashMap;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.interop.test.GraphChanges;
10 import org.simantics.interop.update.model.UpdateNode.Status;
13 public abstract class UpdateTree {
15 private UpdateNode rootNode;
16 private Map<Resource,UpdateNode> nodes;
17 private GraphChanges changes;
18 private UpdateOperations updateOps;
21 public UpdateTree(ReadGraph g, GraphChanges changes, UpdateOperations updateOps) throws DatabaseException {
22 this.changes = changes;
23 this.nodes = new HashMap<Resource, UpdateNode>();
24 this.rootNode = createNode(Status.EXIST, changes.getResource1());
25 nodes.put(changes.getResource1(), rootNode);
26 nodes.put(changes.getResource2(), rootNode);
27 this.updateOps = updateOps;
28 this.updateOps.populate(g);
32 public UpdateOperations getUpdateOps() {
36 public UpdateNode getRootNode() {
41 protected abstract UpdateNode createNode(Status status, Resource r);
42 protected abstract UpdateNode createNode(Status status, UpdateOp op);
44 private UpdateNode createNode(Resource r1, Resource r2) {
45 UpdateNode node = null;
46 if (r1 != null && r2 != null) {
47 node = createNode(Status.EXIST, r1);
50 } else if (r1 != null) {
51 node = createNode(Status.DELETED ,updateOps.getUpdateOp(r1));
53 } else if (r2 != null) {
54 node = createNode(Status.NEW, updateOps.getUpdateOp(r2));
60 public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
61 if (r1 != null && r2 != null) {
64 if (nodes.containsKey(r1))
66 if (nodes.containsKey(r2))
69 UpdateNode node = createNode(r1, r2);
70 connectParent(g,node);
75 protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
77 protected abstract UpdateNode createNode(Status staus, Resource r);
78 protected abstract UpdateNode createNode(Status staus, UpdateOp op);
80 private UpdateNode createNode(Resource r1, Resource r2) {
81 UpdateNode node = null;
82 if (r1 != null && r2 != null) {
83 node = createNode(Status.EXIST, r1);
86 } else if (r1 != null) {
87 node = createNode(Status.DELETED ,updateOps.getUpdateOp(r1));
89 } else if (r2 != null) {
90 node = createNode(Status.NEW, updateOps.getUpdateOp(r2));
96 public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
97 if (r1 != null && r2 != null) {
100 if (nodes.containsKey(r1))
101 return nodes.get(r1);
102 if (nodes.containsKey(r2))
103 return nodes.get(r2);
105 UpdateNode node = createNode(r1, r2);
106 connectParent(g,node);
111 private boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
112 >>>>>>> branch 'master' of ssh://luukkainen@www.simantics.org:29418/simantics/interop
113 UpdateNode parent = null;
115 Resource parentResource = node.getParentResource(g);
116 parent = nodes.get(parentResource);
117 if (parent == null) {
118 if (changes.getComparable().containsLeft(parentResource)) {
119 parent = createNode(parentResource, changes.getComparable().getRight(parentResource));
120 } else if (changes.getComparable().containsRight(parentResource)) {
121 parent = createNode(changes.getComparable().getLeft(parentResource) ,parentResource);
125 //parent.setStatus(Status.CONTAINS);
126 parent.addChild(node);
130 parent.addChild(node);
137 protected abstract boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException;
139 private void populate(ReadGraph g) throws DatabaseException{
141 for (UpdateOp op : updateOps.getOperations()) {
142 if (!handleCustom(g, op)) {
144 addNode(g, null,op.getResource());
146 addNode(g, op.getResource(), null);