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;
10 import org.simantics.interop.update.model.UpdateNode.Status;
13 public class UpdateTree {
15 private UpdateNode rootNode;
16 private Map<Resource,UpdateNode> nodes;
17 private GraphChanges changes;
18 private UpdateOperations updateOps;
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);
32 public UpdateOperations getUpdateOps() {
36 public UpdateNode getRootNode() {
40 public GraphChanges getChanges() {
44 protected UpdateNode createNode(ReadGraph g, Status status, Resource r) throws DatabaseException {
45 return new UpdateNode(g,status, r);
48 protected UpdateNode createNode(ReadGraph g, Status status, UpdateOp op) throws DatabaseException{
49 return new UpdateNode(g,status, op);
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);
58 } else if (r1 != null) {
59 node = createNode(g,Status.DELETED ,updateOps.getUpdateOp(r1));
61 } else if (r2 != null) {
62 node = createNode(g,Status.NEW, updateOps.getUpdateOp(r2));
68 public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
69 if (r1 != null && r2 != null) {
72 if (nodes.containsKey(r1))
74 if (nodes.containsKey(r2))
77 UpdateNode node = createNode(g, r1, r2);
78 connectParent(g,node);
82 protected UpdateNode getNode(Resource r) {
86 protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
87 UpdateNode parent = null;
89 Resource parentResource = node.getParentResource(g);
90 parent = nodes.get(parentResource);
92 parent = getOrCreate(g, parentResource);
95 //parent.setStatus(Status.CONTAINS);
96 parent.addChild(node);
100 parent.addChild(node);
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);
117 //parent.setStatus(Status.CONTAINS
124 protected boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException {
128 private void populate(ReadGraph g) throws DatabaseException{
130 for (UpdateOp op : updateOps.getOperations()) {
131 if (!handleCustom(g, op)) {
133 addNode(g, null,op.getResource());
135 addNode(g, op.getResource(), null);