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 node = createNode(g, UpdateStatus.EXIST, r1);
57 } else if (r1 != null) {
58 node = createNode(g,UpdateStatus.DELETED ,updateOps.getUpdateOp(r1));
60 } else if (r2 != null) {
61 node = createNode(g,UpdateStatus.NEW, updateOps.getUpdateOp(r2));
67 public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
68 if (r1 != null && r2 != null) {
71 if (nodes.containsKey(r1))
73 if (nodes.containsKey(r2))
76 UpdateNode node = createNode(g, r1, r2);
77 connectParent(g,node);
81 public UpdateNode getNode(Resource r) {
85 protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
86 UpdateNode parent = null;
88 Resource parentResource = node.getParentResource(g);
89 parent = nodes.get(parentResource);
91 parent = getOrCreate(g, parentResource);
94 //parent.setStatus(Status.CONTAINS);
95 parent.addChild(node);
99 parent.addChild(node);
106 protected UpdateNode getOrCreate(ReadGraph g, Resource parentResource) throws DatabaseException {
107 UpdateNode parent = nodes.get(parentResource);
108 if (parent == null) {
109 if (changes.getComparable().containsLeft(parentResource)) {
110 parent = createNode(g, parentResource, changes.getComparable().getRight(parentResource));
111 } else if (changes.getComparable().containsRight(parentResource)) {
112 parent = createNode(g, changes.getComparable().getLeft(parentResource) ,parentResource);
116 //parent.setStatus(Status.CONTAINS
123 protected boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException {
127 private void populate(ReadGraph g) throws DatabaseException{
129 for (UpdateOp op : updateOps.getOperations()) {
130 if (!handleCustom(g, op)) {
132 addNode(g, null,op.getResource());
134 addNode(g, op.getResource(), null);