X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.interop.update%2Fsrc%2Forg%2Fsimantics%2Finterop%2Fupdate%2Fmodel%2FUpdateTree.java;h=bf7e0688546beb2e053529ee30e54b41350b738d;hb=88fcf43b9eb2e217b50bf67cee58edaef4637a59;hp=a971da4f41234690724961a9fc1137e13192ffe3;hpb=311d93d874e43c880c0890ff91249914aaeb7ae1;p=simantics%2Finterop.git diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java index a971da4..bf7e068 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java @@ -3,9 +3,14 @@ package org.simantics.interop.update.model; import java.util.HashMap; import java.util.Map; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.common.request.ResourceRead; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; import org.simantics.interop.test.GraphChanges; @@ -26,6 +31,42 @@ public class UpdateTree { this.updateOps = updateOps; this.updateOps.populate(g); populate(g); + this.rootNode.sort(); + } + + public UpdateTree(Session session, GraphChanges changes, UpdateOperations updateOps) throws DatabaseException { + this(session, changes, updateOps, new NullProgressMonitor()); + } + + public UpdateTree(Session session, GraphChanges changes, UpdateOperations updateOps, IProgressMonitor monitor) throws DatabaseException { + this.changes = changes; + this.nodes = new HashMap(); + this.rootNode = session.syncRequest(new NodeRequest(UpdateStatus.EXIST, changes.getResource1())); + nodes.put(changes.getResource1(), rootNode); + nodes.put(changes.getResource2(), rootNode); + this.updateOps = updateOps; + this.updateOps.populate(session, monitor); + populate(session, monitor); + if (monitor.isCanceled()) { + this.changes = null; + this.nodes.clear(); + this.updateOps = null; + this.rootNode = null; + } else { + this.rootNode.sort(); + } + } + + private class NodeRequest extends ResourceRead { + UpdateStatus status; + public NodeRequest(UpdateStatus status, Resource r) { + super(r); + this.status = status; + } + @Override + public UpdateNode perform(ReadGraph graph) throws DatabaseException { + return createNode(graph, status, resource); + } } public UpdateOperations getUpdateOps() { @@ -127,23 +168,54 @@ public class UpdateTree { return false; } - private void populate(ReadGraph g) throws DatabaseException{ + protected void populate(ReadGraph g) throws DatabaseException{ for (UpdateOp op : updateOps.getOperations()) { - if (!handleCustom(g, op)) { - if (op.isAdd()) { - addNode(g, null,op.getResource()); - } else if (op.isDelete()){ - addNode(g, op.getResource(), null); - } else if (op.isChange()) { - Resource o = op.getResource(); - Resource l = getChanges().getComparable().containsLeft(o) ? o :getChanges().getComparable().getLeft(o); - Resource r = getChanges().getComparable().containsRight(o) ? o :getChanges().getComparable().getRight(o); - addNode(g, l, r); - } + populate(g, op); + } + + } + + protected void populate(ReadGraph g, UpdateOp op) throws DatabaseException{ + if (!handleCustom(g, op)) { + if (op.isAdd()) { + addNode(g, null,op.getResource()); + } else if (op.isDelete()){ + addNode(g, op.getResource(), null); + } else if (op.isChange()) { + Resource o = op.getResource(); + Resource l = getChanges().getComparable().containsLeft(o) ? o :getChanges().getComparable().getLeft(o); + Resource r = getChanges().getComparable().containsRight(o) ? o :getChanges().getComparable().getRight(o); + addNode(g, l, r); } } + } + + protected void populate(Session session, IProgressMonitor monitor) throws DatabaseException{ + int i = 0; + while (i < updateOps.getOperations().size()) { + if (monitor.isCanceled()) + return; + i = session.syncRequest(new PopulateRead(i)); + } + } + + protected class PopulateRead implements Read { + int s; + public PopulateRead(int s) { + this.s = s; + } + public Integer perform(ReadGraph graph) throws DatabaseException { + int l = s + 100; + if (l > updateOps.getOperations().size()) + l = updateOps.getOperations().size(); + for (int i = s; i < l; i++) { + UpdateOp op = updateOps.getOperations().get(i); + populate(graph, op); + } + return l; + } } }