]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java
UpdateList object for property changes
[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 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(g, 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         public GraphChanges getChanges() {
41                 return changes;
42         }
43         
44         protected UpdateNode createNode(ReadGraph g, Status status, Resource r) throws DatabaseException {
45                 return new UpdateNode(g,status, r);
46         }
47         
48         protected UpdateNode createNode(ReadGraph g, Status status, UpdateOp op) throws DatabaseException{
49                 return new UpdateNode(g,status, op);
50         }
51         
52         private UpdateNode createNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
53                 UpdateNode node = null;
54                 if (r1 != null && r2 != null) {
55                         node =  createNode(g, Status.EXIST, r1);
56                         nodes.put(r1, node);
57                         nodes.put(r2, node);
58                 } else if (r1 != null) {
59                         node = createNode(g,Status.DELETED ,updateOps.getUpdateOp(r1));
60                         nodes.put(r1, node);
61                 } else if (r2 != null) {
62                         node = createNode(g,Status.NEW, updateOps.getUpdateOp(r2));
63                         nodes.put(r2, node);
64                 }
65                 return node;
66         }
67         
68         public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
69                 if (r1 != null && r2 != null) {
70                         return null;
71                 }
72                 if (nodes.containsKey(r1))
73                         return nodes.get(r1);
74                 if (nodes.containsKey(r2))
75                         return nodes.get(r2);
76                 
77                 UpdateNode node = createNode(g, r1, r2);
78                 connectParent(g,node);
79                 return node;
80         }
81         
82         protected UpdateNode getNode(Resource r) {
83                 return nodes.get(r);
84         }
85         
86         protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
87                 UpdateNode parent = null;
88                 while (true) {
89                         Resource parentResource = node.getParentResource(g);
90                         parent = nodes.get(parentResource);
91                         if (parent == null) {
92                                 parent = getOrCreate(g, parentResource);
93                                 if (parent == null)
94                                         return false;
95                                 //parent.setStatus(Status.CONTAINS);
96                                 parent.addChild(node);
97                                 node = parent;
98                                 parent = null;
99                         } else {
100                                 parent.addChild(node);
101                                 return true;
102                         }
103                         
104                 }
105         }
106         
107         protected UpdateNode getOrCreate(ReadGraph g, Resource parentResource) throws DatabaseException {
108                 UpdateNode parent = nodes.get(parentResource);
109                 if (parent == null) {
110                         if (changes.getComparable().containsLeft(parentResource)) {
111                                 parent = createNode(g, parentResource, changes.getComparable().getRight(parentResource));
112                         } else if (changes.getComparable().containsRight(parentResource)) {
113                                 parent = createNode(g, changes.getComparable().getLeft(parentResource) ,parentResource);
114                         } else {
115                                 return null;
116                         }
117                         //parent.setStatus(Status.CONTAINS
118                 } 
119                 return parent;
120         }
121         
122         
123         
124         protected boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException {
125                 return false;
126         }
127         
128         private void populate(ReadGraph g) throws DatabaseException{
129
130                 for (UpdateOp op : updateOps.getOperations()) {
131                         if (!handleCustom(g, op)) {
132                                 if (op.isAdd()) {
133                                         addNode(g, null,op.getResource());
134                                 } else {
135                                         addNode(g, op.getResource(), null);
136                                 }
137                         }
138                 }
139                 
140         }
141
142 }