]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOperations.java
Allow binding statement changes to update operations
[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> resourceMap = new HashMap<Resource, UpdateOp>();
25         private Map<Statement, UpdateOp> statementMap = new HashMap<Statement, UpdateOp>();
26         private GraphChanges changes;
27         
28         public UpdateOperations(GraphChanges changes) {
29                 this.changes = changes; 
30         }
31         
32         public UpdateOp getUpdateOp(Resource r) {
33                 return resourceMap.get(r);
34         }
35         
36         public UpdateOp getUpdateOp(Statement s) {
37                 return statementMap.get(s);
38         }
39         
40         public void applyAll(WriteGraph g) throws DatabaseException {
41                 for (UpdateOp op : operations) {
42                         apply(g, op);
43                 }
44         }
45
46         public void applySelected(WriteGraph g) throws DatabaseException {
47                 for (UpdateOp op : operations) {
48                         if (op.selected())
49                                 apply(g, op);
50                 }
51         }
52         
53         public List<UpdateOp> getOperations() {
54                 return operations;
55         }
56         
57         public GraphChanges getChanges() {
58                 return changes;
59         }
60
61         private void apply(WriteGraph g, UpdateOp op) throws DatabaseException {
62                 Stack<UpdateOp> stack = new Stack<UpdateOp>();
63                 _apply(g, stack, op);
64         }
65         
66         private void _apply(WriteGraph g, Stack<UpdateOp> stack, UpdateOp op) throws DatabaseException {
67                 if (op.applied())
68                         return;
69                 if (stack.contains(op)) {
70                         op.apply(g);
71                         return;
72                 }
73                 stack.push(op);
74                 if (op.requiresParentOps()) {
75                         for (UpdateOp pop : op.getParentOps())
76                                 if (!pop.applied())
77                                         _apply(g, stack, pop);
78                 } 
79                 if (op.requiresSubOps()) {
80                         for (UpdateOp sop : op.getSubOps())
81                                 if (!sop.applied())
82                                         _apply(g, stack, sop);
83                 }
84                 stack.pop();
85                 op.apply(g);
86         }
87
88         protected List<UpdateOp> getOps() {
89                 List<UpdateOp> list = new ArrayList<UpdateOp>(operations.size());
90                 list.addAll(operations);
91                 return list;
92         }
93         
94         protected void addOp(Resource r, UpdateOp op) {
95                 resourceMap.put(r, op);
96                 operations.add(op);
97         }
98         
99         protected void addOp(Statement s, UpdateOp op) {
100                 statementMap.put(s, op);
101                 operations.add(op);
102         }
103         
104         protected void replaceOp(Resource r, UpdateOp op) {
105                 UpdateOp oldOp = resourceMap.remove(r);
106                 if (oldOp != null) {
107                         operations.remove(oldOp);
108                 }
109                 resourceMap.put(r, op);
110                 operations.add(op);
111         }
112         
113         protected UpdateOp getOP(Resource r) {
114                 return resourceMap.get(r);
115         }
116         
117         protected UpdateOp getOP(Statement r) {
118                 return statementMap.get(r);
119         }
120
121         public abstract void populate(ReadGraph g) throws DatabaseException;
122         
123         protected boolean compares(Resource r1, Resource r2) {
124                 if (r1.equals(r2))
125                         return true;
126                 if (changes.getComparable().contains(r1, r2))
127                         return true;
128                 return false;
129         }
130
131 }