]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Simple update operation 18/3718/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 17 Dec 2019 15:52:55 +0000 (17:52 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 17 Dec 2019 15:52:55 +0000 (17:52 +0200)
Operation creates new objects by copyingL0.InstanceOf and L0.HasProperty
relations.

gitlab #20

Change-Id: I4a1bacd17a826c6cffea375dd311428e0a24ec89

org.simantics.interop.update/src/org/simantics/interop/update/model/SimpleObjectUpdateOp.java [new file with mode: 0644]

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 (file)
index 0000000..9f2f41a
--- /dev/null
@@ -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<UpdateOp> 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;
+       }
+       
+
+}