]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOperations.java
Merge branch 'master' of
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateOperations.java
1 package org.simantics.interop.update.model;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Stack;
8
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.Statement;
12 import org.simantics.db.WriteGraph;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.interop.test.GraphChanges;
15
16 /**
17  * 
18  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
19  *
20  */
21 public abstract  class UpdateOperations {
22         
23         private List<UpdateOp> operations = new ArrayList<UpdateOp>();
24         private Map<Resource, UpdateOp> opMap = new HashMap<Resource, UpdateOp>();
25         private GraphChanges changes;
26         
27         public UpdateOperations(GraphChanges changes) {
28                 this.changes = changes; 
29         }
30         
31         public UpdateOp getUpdateOp(Resource r) {
32                 return opMap.get(r);
33         }
34         
35         public void applyAll(WriteGraph g) throws DatabaseException {
36                 for (UpdateOp op : operations) {
37                         apply(g, op);
38                 }
39         }
40
41         public void applySelected(WriteGraph g) throws DatabaseException {
42                 for (UpdateOp op : operations) {
43                         if (op.selected())
44                                 apply(g, op);
45                 }
46         }
47         
48         public List<UpdateOp> getOperations() {
49                 return operations;
50         }
51         
52         public GraphChanges getChanges() {
53                 return changes;
54         }
55
56         private void apply(WriteGraph g, UpdateOp op) throws DatabaseException {
57                 Stack<UpdateOp> stack = new Stack<UpdateOp>();
58                 _apply(g, stack, op);
59         }
60         
61         private void _apply(WriteGraph g, Stack<UpdateOp> stack, UpdateOp op) throws DatabaseException {
62                 if (op.applied())
63                         return;
64                 if (stack.contains(op))
65                         return;
66                 stack.push(op);
67                 if (op.requiresParentOps()) {
68                         for (UpdateOp pop : op.getParentOps())
69                                 if (!pop.applied())
70                                         _apply(g, stack, pop);
71                 } 
72                 if (op.requiresSubOps()) {
73                         for (UpdateOp sop : op.getSubOps())
74                                 if (!sop.applied())
75                                         _apply(g, stack, sop);
76                 }
77                 stack.pop();
78                 op.apply(g);
79         }
80
81         protected List<UpdateOp> getOps() {
82                 List<UpdateOp> list = new ArrayList<UpdateOp>(operations.size());
83                 list.addAll(operations);
84                 return list;
85         }
86         
87         protected void addOp(Resource r, UpdateOp op) {
88                 opMap.put(r, op);
89                 operations.add(op);
90         }
91         
92         protected void replaceOp(Resource r, UpdateOp op) {
93                 UpdateOp oldOp = opMap.remove(r);
94                 if (oldOp != null) {
95                         operations.remove(op);
96                 }
97                 opMap.put(r, op);
98                 operations.add(op);
99         }
100         
101         protected UpdateOp getOP(Resource r) {
102                 return opMap.get(r);
103         }
104
105         public abstract void populate(ReadGraph g) throws DatabaseException;
106         
107         protected boolean compares(Resource r1, Resource r2) {
108                 if (r1.equals(r2))
109                         return true;
110                 if (changes.getComparable().contains(r1, r2))
111                         return true;
112                 return false;
113         }
114
115 }