1 package org.simantics.interop.update.model;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 import org.simantics.db.Resource;
7 import org.simantics.db.Statement;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.interop.test.GraphChanges;
13 * Base class for update operations (adding and deleting objects)
15 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
18 public abstract class UpdateOp {
20 private GraphChanges changes;
22 private boolean selected = false;
23 private boolean manualSelection = false;
24 protected boolean applied = false;
25 protected boolean visible = true;
26 protected boolean enabled = true;
28 private Collection<UpdateOp> parentOps = new ArrayList<UpdateOp>();
29 private Collection<UpdateOp> subOps = new ArrayList<UpdateOp>();
32 public UpdateOp(GraphChanges changes) {
33 this.changes = changes;
36 public Collection<UpdateOp> getParentOps() {
40 public Collection<UpdateOp> getSubOps() {
44 public void addParentOp(UpdateOp op) {
45 assert (!op.equals(this));
49 public void addSubOp(UpdateOp op) {
50 assert (!op.equals(this));
54 public void removeParentOp(UpdateOp op) {
58 public void removeSubOp(UpdateOp op) {
62 public GraphChanges getChanges() {
66 public abstract boolean isAdd();
67 public abstract boolean isDelete();
69 public boolean isChange() {
70 return isAdd() || isDelete();
74 * Is parent operation applied before this.
77 public abstract boolean requiresParentOps();
79 * Is child operation applied before this.
82 public abstract boolean requiresSubOps();
85 * Is parent operation selected when this is selected
88 public boolean selectParentOps() {
89 return requiresParentOps();
93 * Is child operation selected when this is selected
96 public boolean selectSubOps() {
97 return requiresSubOps();
100 public boolean select(boolean select) {
105 boolean b = _select(select);
107 manualSelection = true;
111 private boolean _select(boolean select) {
112 if (select == selected)
119 if (selectParentOps()) {
120 for (UpdateOp op : parentOps)
125 manualSelection = false;
126 if (selectSubOps()) {
127 for (UpdateOp op : subOps)
133 manualSelection = false;
134 for (UpdateOp op : subOps) {
135 if (op.selectParentOps())
137 else if (!op.manualSelection)
140 for (UpdateOp op : parentOps)
141 if (op.selectSubOps())
147 public boolean selected() {
151 public boolean applied() {
155 public boolean isVisible() {
160 * Is change enabled. Disabled changes do not allow changing selected state.
163 public boolean enabled() {
167 public void setEnabled(boolean enabled) {
168 this.enabled = enabled;
172 public void apply(WriteGraph g) throws DatabaseException {
181 * Applies the changes into the database.
184 * @throws DatabaseException
186 protected abstract void _apply(WriteGraph g) throws DatabaseException;
189 * Returns resource that this operation is changing.
192 public abstract Resource getResource();
195 * Returns resource that this operation is changing.
198 public abstract Statement getStatement();
201 * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null.
204 public abstract Resource getCreatedResource();