]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java
a971da4f41234690724961a9fc1137e13192ffe3
[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
11
12 public class UpdateTree {
13         
14         private UpdateNode rootNode;
15         private Map<Resource,UpdateNode> nodes;
16         private GraphChanges changes;
17         private UpdateOperations updateOps;
18         
19         
20         public UpdateTree(ReadGraph g, GraphChanges changes, UpdateOperations updateOps) throws DatabaseException {
21                 this.changes = changes;
22                 this.nodes = new HashMap<Resource, UpdateNode>();
23                 this.rootNode = createNode(g, UpdateStatus.EXIST, changes.getResource1());
24                 nodes.put(changes.getResource1(), rootNode);
25                 nodes.put(changes.getResource2(), rootNode);
26                 this.updateOps = updateOps;
27                 this.updateOps.populate(g);
28                 populate(g);
29         }
30         
31         public UpdateOperations getUpdateOps() {
32                 return updateOps;
33         }
34         
35         public UpdateNode getRootNode() {
36                 return rootNode;
37         }
38         
39         public GraphChanges getChanges() {
40                 return changes;
41         }
42         
43         protected UpdateNode createNode(ReadGraph g, UpdateStatus status, Resource r) throws DatabaseException {
44                 return new UpdateNode(g,status, r);
45         }
46         
47         protected UpdateNode createNode(ReadGraph g, UpdateStatus status, UpdateOp op) throws DatabaseException{
48                 return new UpdateNode(g,status, op);
49         }
50         
51         private UpdateNode createNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
52                 UpdateNode node = null;
53                 if (r1 != null && r2 != null) {
54                     UpdateOp op = updateOps.getUpdateOp(r1);
55                     if (op == null)
56                         op = updateOps.getUpdateOp(r2);
57             if (op == null)                     
58                 node = createNode(g, UpdateStatus.EXIST, r1);
59             else
60                 node = createNode(g, UpdateStatus.EXIST, op);
61                         nodes.put(r1, node);
62                         nodes.put(r2, node);
63                 } else if (r1 != null) {
64                         node = createNode(g,UpdateStatus.DELETED ,updateOps.getUpdateOp(r1));
65                         nodes.put(r1, node);
66                 } else if (r2 != null) {
67                         node = createNode(g,UpdateStatus.NEW, updateOps.getUpdateOp(r2));
68                         nodes.put(r2, node);
69                 }
70                 return node;
71         }
72         
73         public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
74                 if (nodes.containsKey(r1))
75                         return nodes.get(r1);
76                 if (nodes.containsKey(r2))
77                         return nodes.get(r2);
78                 
79                 UpdateNode node = createNode(g, r1, r2);
80                 connectParent(g,node);
81                 return node;
82         }
83         
84         public UpdateNode getNode(Resource r) {
85                 return nodes.get(r);
86         }
87         
88         protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
89                 UpdateNode parent = null;
90                 while (true) {
91                         Resource parentResource = node.getParentResource(g);
92                         parent = nodes.get(parentResource);
93                         if (parent == null) {
94                                 parent = getOrCreate(g, parentResource);
95                                 if (parent == null)
96                                         return false;
97                                 //parent.setStatus(Status.CONTAINS);
98                                 parent.addChild(node);
99                                 node = parent;
100                                 parent = null;
101                         } else {
102                                 parent.addChild(node);
103                                 return true;
104                         }
105                         
106                 }
107         }
108         
109         protected UpdateNode getOrCreate(ReadGraph g, Resource parentResource) throws DatabaseException {
110                 UpdateNode parent = nodes.get(parentResource);
111                 if (parent == null) {
112                         if (changes.getComparable().containsLeft(parentResource)) {
113                                 parent = createNode(g, parentResource, changes.getComparable().getRight(parentResource));
114                         } else if (changes.getComparable().containsRight(parentResource)) {
115                                 parent = createNode(g, changes.getComparable().getLeft(parentResource) ,parentResource);
116                         } else {
117                                 return null;
118                         }
119                         //parent.setStatus(Status.CONTAINS
120                 } 
121                 return parent;
122         }
123         
124         
125         
126         protected boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException {
127                 return false;
128         }
129         
130         private void populate(ReadGraph g) throws DatabaseException{
131
132                 for (UpdateOp op : updateOps.getOperations()) {
133                         if (!handleCustom(g, op)) {
134                                 if (op.isAdd()) {
135                                         addNode(g, null,op.getResource());
136                                 } else if (op.isDelete()){
137                                         addNode(g, op.getResource(), null);
138                                 } else if (op.isChange()) {
139                                         Resource o = op.getResource();
140                                     Resource l = getChanges().getComparable().containsLeft(o) ? o :getChanges().getComparable().getLeft(o);
141                                     Resource r = getChanges().getComparable().containsRight(o) ? o :getChanges().getComparable().getRight(o);
142                                     addNode(g, l, r);
143                                 }
144                         }
145                 }
146                 
147         }
148
149 }