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.exception.DatabaseException;
9 import org.simantics.interop.test.GraphChanges;
12 public class UpdateTree {
14 private UpdateNode rootNode;
15 private Map<Resource,UpdateNode> nodes;
16 private GraphChanges changes;
17 private UpdateOperations updateOps;
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);
31 public UpdateOperations getUpdateOps() {
35 public UpdateNode getRootNode() {
39 public GraphChanges getChanges() {
43 protected UpdateNode createNode(ReadGraph g, UpdateStatus status, Resource r) throws DatabaseException {
44 return new UpdateNode(g,status, r);
47 protected UpdateNode createNode(ReadGraph g, UpdateStatus status, UpdateOp op) throws DatabaseException{
48 return new UpdateNode(g,status, op);
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);
56 op = updateOps.getUpdateOp(r2);
58 node = createNode(g, UpdateStatus.EXIST, r1);
60 node = createNode(g, UpdateStatus.EXIST, op);
63 } else if (r1 != null) {
64 node = createNode(g,UpdateStatus.DELETED ,updateOps.getUpdateOp(r1));
66 } else if (r2 != null) {
67 node = createNode(g,UpdateStatus.NEW, updateOps.getUpdateOp(r2));
73 public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
74 if (nodes.containsKey(r1))
76 if (nodes.containsKey(r2))
79 UpdateNode node = createNode(g, r1, r2);
80 connectParent(g,node);
84 public UpdateNode getNode(Resource r) {
88 protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
89 UpdateNode parent = null;
91 Resource parentResource = node.getParentResource(g);
92 parent = nodes.get(parentResource);
94 parent = getOrCreate(g, parentResource);
97 //parent.setStatus(Status.CONTAINS);
98 parent.addChild(node);
102 parent.addChild(node);
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);
119 //parent.setStatus(Status.CONTAINS
126 protected boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException {
130 private void populate(ReadGraph g) throws DatabaseException{
132 for (UpdateOp op : updateOps.getOperations()) {
133 if (!handleCustom(g, op)) {
135 addNode(g, null,op.getResource());
136 } else if (op.isDelete()){
137 addNode(g, op.getResource(), null);
138 } else if (op.isChange()) {
139 Resource l = getChanges().getComparable().containsLeft(op.getResource()) ? op.getResource() :getChanges().getComparable().getLeft(op.getResource());
140 Resource r = getChanges().getComparable().containsRight(op.getResource()) ? op.getResource() :getChanges().getComparable().getRight(op.getResource());