]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/SimpleObjectUpdateOp.java
9f2f41af1f26569488a1d27e2875185f2c42240a
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / SimpleObjectUpdateOp.java
1 package org.simantics.interop.update.model;
2
3 import java.util.Collection;
4
5 import org.simantics.db.Resource;
6 import org.simantics.db.Statement;
7 import org.simantics.db.WriteGraph;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.interop.test.GraphChanges;
10 import org.simantics.layer0.Layer0;
11
12 /**
13  * Simple update operation, where 
14  *   addition is based on copying L0.InstanceOf and L0.HasProperty relations.
15  *   deletion is deny.
16  * 
17  * @author luukkainen
18  *
19  */
20 public class SimpleObjectUpdateOp extends AddDeleteUpdateOp {
21         
22         private Statement r;
23         protected Resource copyObj;
24         
25         public SimpleObjectUpdateOp(Statement r, boolean add, GraphChanges changes) {
26                 super(changes);
27                 this.r = r;
28                 this.add = add;
29         }
30         
31         @Override
32         protected void _apply(WriteGraph g) throws DatabaseException {
33                 if (add) {
34                         Resource parent = null;
35                         if (getChanges().getComparable().containsRight(r.getSubject()))
36                                 parent = getChanges().getComparable().getLeft(r.getSubject());
37                         else {
38                                 Collection<UpdateOp> parentOps = getParentOps();
39                                 if (parentOps.size() != 1)
40                                         throw new RuntimeException("Parent not found.");
41                                 parent = parentOps.iterator().next().getCreatedResource();
42                         }
43                         if (parent == null)
44                                 throw new RuntimeException("Parent not found.");
45                         
46                         Layer0 L0 = Layer0.getInstance(g);
47                         
48                         Resource sourceObj = r.getObject();
49                         
50                         copyObj = g.newResource();
51                         for (Resource t : g.getObjects(sourceObj, L0.InstanceOf)) {
52                                 g.claim(copyObj, L0.InstanceOf, t);
53                         }
54                         copyProperties(g, sourceObj, copyObj);
55                         g.claim(parent, r.getPredicate(), copyObj);
56                                 
57                 } else {
58                         
59                         Resource sourceObj= r.getObject();
60                         g.deny(sourceObj);
61                         
62                 }
63
64         }
65         
66         @Override
67         public Statement getStatement() {
68                 return r;
69         }
70         
71         @Override
72         public Resource getResource() {
73                 return r.getObject();
74         }
75         
76         @Override
77         public Resource getCreatedResource() {
78                 return copyObj;
79         }
80         
81
82 }