]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/SimpleObjectUpdateOp.java
Copy all generic type definitions with SimpleObjectUpdate
[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                         copyTypes(g, sourceObj, copyObj);
52                         copyProperties(g, sourceObj, copyObj);
53                         g.claim(parent, r.getPredicate(), copyObj);
54                                 
55                 } else {
56                         
57                         Resource sourceObj= r.getObject();
58                         g.deny(sourceObj);
59                         
60                 }
61
62         }
63         
64         @Override
65         public Statement getStatement() {
66                 return r;
67         }
68         
69         @Override
70         public Resource getResource() {
71                 return r.getObject();
72         }
73         
74         @Override
75         public Resource getCreatedResource() {
76                 return copyObj;
77         }
78         
79         @Override
80     public String toString() {
81         return super.toString() + " " + r.getSubject() + " " + r.getPredicate() + " " + r.getObject();
82     }
83         
84
85 }