From: Marko Luukkainen Date: Tue, 17 Dec 2019 15:52:55 +0000 (+0200) Subject: Simple update operation X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F18%2F3718%2F1;p=simantics%2Finterop.git Simple update operation Operation creates new objects by copyingL0.InstanceOf and L0.HasProperty relations. gitlab #20 Change-Id: I4a1bacd17a826c6cffea375dd311428e0a24ec89 --- diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/SimpleObjectUpdateOp.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/SimpleObjectUpdateOp.java new file mode 100644 index 0000000..9f2f41a --- /dev/null +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/SimpleObjectUpdateOp.java @@ -0,0 +1,82 @@ +package org.simantics.interop.update.model; + +import java.util.Collection; + +import org.simantics.db.Resource; +import org.simantics.db.Statement; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.interop.test.GraphChanges; +import org.simantics.layer0.Layer0; + +/** + * Simple update operation, where + * addition is based on copying L0.InstanceOf and L0.HasProperty relations. + * deletion is deny. + * + * @author luukkainen + * + */ +public class SimpleObjectUpdateOp extends AddDeleteUpdateOp { + + private Statement r; + protected Resource copyObj; + + public SimpleObjectUpdateOp(Statement r, boolean add, GraphChanges changes) { + super(changes); + this.r = r; + this.add = add; + } + + @Override + protected void _apply(WriteGraph g) throws DatabaseException { + if (add) { + Resource parent = null; + if (getChanges().getComparable().containsRight(r.getSubject())) + parent = getChanges().getComparable().getLeft(r.getSubject()); + else { + Collection parentOps = getParentOps(); + if (parentOps.size() != 1) + throw new RuntimeException("Parent not found."); + parent = parentOps.iterator().next().getCreatedResource(); + } + if (parent == null) + throw new RuntimeException("Parent not found."); + + Layer0 L0 = Layer0.getInstance(g); + + Resource sourceObj = r.getObject(); + + copyObj = g.newResource(); + for (Resource t : g.getObjects(sourceObj, L0.InstanceOf)) { + g.claim(copyObj, L0.InstanceOf, t); + } + copyProperties(g, sourceObj, copyObj); + g.claim(parent, r.getPredicate(), copyObj); + + } else { + + Resource sourceObj= r.getObject(); + g.deny(sourceObj); + + } + + } + + @Override + public Statement getStatement() { + return r; + } + + @Override + public Resource getResource() { + return r.getObject(); + } + + @Override + public Resource getCreatedResource() { + return copyObj; + } + + +}