1 package org.simantics.interop.update.model;
3 import java.util.HashMap;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.Session;
9 import org.simantics.db.common.request.ResourceRead;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.request.Read;
12 import org.simantics.interop.test.GraphChanges;
15 public class UpdateTree {
17 private UpdateNode rootNode;
18 private Map<Resource,UpdateNode> nodes;
19 private GraphChanges changes;
20 private UpdateOperations updateOps;
23 public UpdateTree(ReadGraph g, GraphChanges changes, UpdateOperations updateOps) throws DatabaseException {
24 this.changes = changes;
25 this.nodes = new HashMap<Resource, UpdateNode>();
26 this.rootNode = createNode(g, UpdateStatus.EXIST, changes.getResource1());
27 nodes.put(changes.getResource1(), rootNode);
28 nodes.put(changes.getResource2(), rootNode);
29 this.updateOps = updateOps;
30 this.updateOps.populate(g);
34 public UpdateTree(Session session, GraphChanges changes, UpdateOperations updateOps) throws DatabaseException {
35 this.changes = changes;
36 this.nodes = new HashMap<Resource, UpdateNode>();
37 this.rootNode = session.syncRequest(new NodeRequest(UpdateStatus.EXIST, changes.getResource1()));
38 nodes.put(changes.getResource1(), rootNode);
39 nodes.put(changes.getResource2(), rootNode);
40 this.updateOps = updateOps;
41 this.updateOps.populate(session);
45 private class NodeRequest extends ResourceRead<UpdateNode> {
47 public NodeRequest(UpdateStatus status, Resource r) {
52 public UpdateNode perform(ReadGraph graph) throws DatabaseException {
53 return createNode(graph, status, resource);
57 public UpdateOperations getUpdateOps() {
61 public UpdateNode getRootNode() {
65 public GraphChanges getChanges() {
69 protected UpdateNode createNode(ReadGraph g, UpdateStatus status, Resource r) throws DatabaseException {
70 return new UpdateNode(g,status, r);
73 protected UpdateNode createNode(ReadGraph g, UpdateStatus status, UpdateOp op) throws DatabaseException{
74 return new UpdateNode(g,status, op);
77 private UpdateNode createNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
78 UpdateNode node = null;
79 if (r1 != null && r2 != null) {
80 UpdateOp op = updateOps.getUpdateOp(r1);
82 op = updateOps.getUpdateOp(r2);
84 node = createNode(g, UpdateStatus.EXIST, r1);
86 node = createNode(g, UpdateStatus.EXIST, op);
89 } else if (r1 != null) {
90 node = createNode(g,UpdateStatus.DELETED ,updateOps.getUpdateOp(r1));
92 } else if (r2 != null) {
93 node = createNode(g,UpdateStatus.NEW, updateOps.getUpdateOp(r2));
99 public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
100 if (nodes.containsKey(r1))
101 return nodes.get(r1);
102 if (nodes.containsKey(r2))
103 return nodes.get(r2);
105 UpdateNode node = createNode(g, r1, r2);
106 connectParent(g,node);
110 public UpdateNode getNode(Resource r) {
114 protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
115 UpdateNode parent = null;
117 Resource parentResource = node.getParentResource(g);
118 parent = nodes.get(parentResource);
119 if (parent == null) {
120 parent = getOrCreate(g, parentResource);
123 //parent.setStatus(Status.CONTAINS);
124 parent.addChild(node);
128 parent.addChild(node);
135 protected UpdateNode getOrCreate(ReadGraph g, Resource parentResource) throws DatabaseException {
136 UpdateNode parent = nodes.get(parentResource);
137 if (parent == null) {
138 if (changes.getComparable().containsLeft(parentResource)) {
139 parent = createNode(g, parentResource, changes.getComparable().getRight(parentResource));
140 } else if (changes.getComparable().containsRight(parentResource)) {
141 parent = createNode(g, changes.getComparable().getLeft(parentResource) ,parentResource);
145 //parent.setStatus(Status.CONTAINS
152 protected boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException {
156 protected void populate(ReadGraph g) throws DatabaseException{
158 for (UpdateOp op : updateOps.getOperations()) {
164 protected void populate(ReadGraph g, UpdateOp op) throws DatabaseException{
165 if (!handleCustom(g, op)) {
167 addNode(g, null,op.getResource());
168 } else if (op.isDelete()){
169 addNode(g, op.getResource(), null);
170 } else if (op.isChange()) {
171 Resource o = op.getResource();
172 Resource l = getChanges().getComparable().containsLeft(o) ? o :getChanges().getComparable().getLeft(o);
173 Resource r = getChanges().getComparable().containsRight(o) ? o :getChanges().getComparable().getRight(o);
179 protected void populate(Session session) throws DatabaseException{
181 while (i < updateOps.getOperations().size()) {
182 i = session.syncRequest(new PopulateRead(i));
186 protected class PopulateRead implements Read<Integer> {
188 public PopulateRead(int s) {
192 public Integer perform(ReadGraph graph) throws DatabaseException {
194 if (l > updateOps.getOperations().size())
195 l = updateOps.getOperations().size();
196 for (int i = s; i < l; i++) {
197 UpdateOp op = updateOps.getOperations().get(i);