]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java
b8e53f16699b2354fe3c5ac82f5994fcf06eec8b
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateTree.java
1 package  org.simantics.interop.update.model;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
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;
11
12
13 public abstract class UpdateTree {
14         
15         private UpdateNode rootNode;
16         private Map<Resource,UpdateNode> nodes;
17         private GraphChanges changes;
18         private UpdateOperations updateOps;
19         
20         
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);
29                 populate(g);
30         }
31         
32         public UpdateOperations getUpdateOps() {
33                 return updateOps;
34         }
35         
36         public UpdateNode getRootNode() {
37                 return rootNode;
38         }
39         
40         protected abstract UpdateNode createNode(Status status, Resource r);
41         protected abstract UpdateNode createNode(Status status, UpdateOp op);
42         
43         private UpdateNode createNode(Resource r1, Resource r2) {
44                 UpdateNode node = null;
45                 if (r1 != null && r2 != null) {
46                         node =  createNode(Status.EXIST, r1);
47                         nodes.put(r1, node);
48                         nodes.put(r2, node);
49                 } else if (r1 != null) {
50                         node = createNode(Status.DELETED ,updateOps.getUpdateOp(r1));
51                         nodes.put(r1, node);
52                 } else if (r2 != null) {
53                         node = createNode(Status.NEW, updateOps.getUpdateOp(r2));
54                         nodes.put(r2, node);
55                 }
56                 return node;
57         }
58         
59         public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
60                 if (r1 != null && r2 != null) {
61                         return null;
62                 }
63                 if (nodes.containsKey(r1))
64                         return nodes.get(r1);
65                 if (nodes.containsKey(r2))
66                         return nodes.get(r2);
67                 
68                 UpdateNode node = createNode(r1, r2);
69                 connectParent(g,node);
70                 return node;
71         }
72         
73         private boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
74                 UpdateNode parent = null;
75                 while (true) {
76                         Resource parentResource = node.getParentResource(g);
77                         parent = nodes.get(parentResource);
78                         if (parent == null) {
79                                 if (changes.getComparable().containsLeft(parentResource)) {
80                                         parent = createNode(parentResource, changes.getComparable().getRight(parentResource));
81                                 } else if (changes.getComparable().containsRight(parentResource)) {
82                                         parent = createNode(changes.getComparable().getLeft(parentResource) ,parentResource);
83                                 } else {
84                                         return false;
85                                 }
86                                 //parent.setStatus(Status.CONTAINS);
87                                 parent.addChild(node);
88                                 node = parent;
89                                 parent = null;
90                         } else {
91                                 parent.addChild(node);
92                                 return true;
93                         }
94                         
95                 }
96         }
97         
98         protected abstract boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException;
99         
100         private void populate(ReadGraph g) throws DatabaseException{
101
102                 for (UpdateOp op : updateOps.getOperations()) {
103                         if (!handleCustom(g, op)) {
104                                 if (op.isAdd()) {
105                                         addNode(g, null,op.getResource());
106                                 } else {
107                                         addNode(g, op.getResource(), null);
108                                 }
109                         }
110                 }
111                 
112         }
113
114 }