]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java
Merge branch 'master' of
[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 <<<<<<< HEAD
41         protected abstract UpdateNode createNode(Status status, Resource r);
42         protected abstract UpdateNode createNode(Status status, UpdateOp op);
43         
44         private UpdateNode createNode(Resource r1, Resource r2) {
45                 UpdateNode node = null;
46                 if (r1 != null && r2 != null) {
47                         node =  createNode(Status.EXIST, r1);
48                         nodes.put(r1, node);
49                         nodes.put(r2, node);
50                 } else if (r1 != null) {
51                         node = createNode(Status.DELETED ,updateOps.getUpdateOp(r1));
52                         nodes.put(r1, node);
53                 } else if (r2 != null) {
54                         node = createNode(Status.NEW, updateOps.getUpdateOp(r2));
55                         nodes.put(r2, node);
56                 }
57                 return node;
58         }
59         
60         public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
61                 if (r1 != null && r2 != null) {
62                         return null;
63                 }
64                 if (nodes.containsKey(r1))
65                         return nodes.get(r1);
66                 if (nodes.containsKey(r2))
67                         return nodes.get(r2);
68                 
69                 UpdateNode node = createNode(r1, r2);
70                 connectParent(g,node);
71                 return node;
72                 
73         }
74         
75         protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
76 =======
77         protected abstract UpdateNode createNode(Status staus, Resource r);
78         protected abstract UpdateNode createNode(Status staus, UpdateOp op);
79         
80         private UpdateNode createNode(Resource r1, Resource r2) {
81                 UpdateNode node = null;
82                 if (r1 != null && r2 != null) {
83                         node =  createNode(Status.EXIST, r1);
84                         nodes.put(r1, node);
85                         nodes.put(r2, node);
86                 } else if (r1 != null) {
87                         node = createNode(Status.DELETED ,updateOps.getUpdateOp(r1));
88                         nodes.put(r1, node);
89                 } else if (r2 != null) {
90                         node = createNode(Status.NEW, updateOps.getUpdateOp(r2));
91                         nodes.put(r2, node);
92                 }
93                 return node;
94         }
95         
96         public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
97                 if (r1 != null && r2 != null) {
98                         return null;
99                 }
100                 if (nodes.containsKey(r1))
101                         return nodes.get(r1);
102                 if (nodes.containsKey(r2))
103                         return nodes.get(r2);
104                 
105                 UpdateNode node = createNode(r1, r2);
106                 connectParent(g,node);
107                 return node;
108                 
109         }
110         
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;
114                 while (true) {
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);
122                                 } else {
123                                         return false;
124                                 }
125                                 //parent.setStatus(Status.CONTAINS);
126                                 parent.addChild(node);
127                                 node = parent;
128                                 parent = null;
129                         } else {
130                                 parent.addChild(node);
131                                 return true;
132                         }
133                         
134                 }
135         }
136         
137         protected abstract boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException;
138         
139         private void populate(ReadGraph g) throws DatabaseException{
140
141                 for (UpdateOp op : updateOps.getOperations()) {
142                         if (!handleCustom(g, op)) {
143                                 if (op.isAdd()) {
144                                         addNode(g, null,op.getResource());
145                                 } else {
146                                         addNode(g, op.getResource(), null);
147                                 }
148                         }
149                 }
150                 
151         }
152
153 }