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(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 protected UpdateNode createNode(Status status, Resource r) {
41 return new UpdateNode(status, r);
44 protected UpdateNode createNode(Status status, UpdateOp op) {
45 return new UpdateNode(status, op);
48 private UpdateNode createNode(Resource r1, Resource r2) {
49 UpdateNode node = null;
50 if (r1 != null && r2 != null) {
51 node = createNode(Status.EXIST, r1);
54 } else if (r1 != null) {
55 node = createNode(Status.DELETED ,updateOps.getUpdateOp(r1));
57 } else if (r2 != null) {
58 node = createNode(Status.NEW, updateOps.getUpdateOp(r2));
64 public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
65 if (r1 != null && r2 != null) {
68 if (nodes.containsKey(r1))
70 if (nodes.containsKey(r2))
73 UpdateNode node = createNode(r1, r2);
74 connectParent(g,node);
78 protected UpdateNode getNode(Resource r) {
82 protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
83 UpdateNode parent = null;
85 Resource parentResource = node.getParentResource(g);
86 parent = nodes.get(parentResource);
88 parent = getOrCreate(g, parentResource);
91 //parent.setStatus(Status.CONTAINS);
92 parent.addChild(node);
96 parent.addChild(node);
103 protected UpdateNode getOrCreate(ReadGraph g, Resource parentResource) throws DatabaseException {
104 UpdateNode parent = nodes.get(parentResource);
105 if (parent == null) {
106 if (changes.getComparable().containsLeft(parentResource)) {
107 parent = createNode(parentResource, changes.getComparable().getRight(parentResource));
108 } else if (changes.getComparable().containsRight(parentResource)) {
109 parent = createNode(changes.getComparable().getLeft(parentResource) ,parentResource);
113 //parent.setStatus(Status.CONTAINS
120 protected boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException {
124 private void populate(ReadGraph g) throws DatabaseException{
126 for (UpdateOp op : updateOps.getOperations()) {
127 if (!handleCustom(g, op)) {
129 addNode(g, null,op.getResource());
131 addNode(g, op.getResource(), null);