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