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.WriteGraph;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.interop.test.GraphChanges;
12 * Base class for update operations (adding and deleting objects)
14 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
17 public abstract class UpdateOp {
19 private GraphChanges changes;
21 private boolean selected = false;
22 private boolean manualSelection = false;
23 protected boolean applied = false;
25 private Collection<UpdateOp> parentOps = new ArrayList<UpdateOp>();
26 private Collection<UpdateOp> subOps = new ArrayList<UpdateOp>();
29 public UpdateOp(GraphChanges changes) {
30 this.changes = changes;
33 public Collection<UpdateOp> getParentOps() {
37 public Collection<UpdateOp> getSubOps() {
41 public void addParentOp(UpdateOp op) {
42 assert (!op.equals(this));
46 public void addSubOp(UpdateOp op) {
47 assert (!op.equals(this));
51 public void removeParentOp(UpdateOp op) {
55 public void removeSubOp(UpdateOp op) {
59 public GraphChanges getChanges() {
63 public abstract boolean isAdd();
64 public abstract boolean isDelete();
67 * Is parent operation applied before this.
70 public abstract boolean requiresParentOps();
72 * Is child operation applied before this.
75 public abstract boolean requiresSubOps();
78 * Is parent operation selected when this is selected
81 public boolean selectParentOps() {
82 return requiresParentOps();
86 * Is child operation selected when this is selected
89 public boolean selectSubOps() {
90 return requiresSubOps();
93 public boolean select(boolean select) {
94 boolean b = _select(select);
96 manualSelection = true;
100 private boolean _select(boolean select) {
101 if (select == selected)
104 if (selectParentOps()) {
105 for (UpdateOp op : parentOps)
110 manualSelection = false;
111 if (selectSubOps()) {
112 for (UpdateOp op : subOps)
118 manualSelection = false;
119 for (UpdateOp op : subOps) {
120 if (op.selectParentOps())
122 else if (!op.manualSelection)
125 for (UpdateOp op : parentOps)
126 if (op.selectSubOps())
132 public boolean selected() {
136 public boolean applied() {
139 public void apply(WriteGraph g) throws DatabaseException {
148 * Applies the changes into the database.
151 * @throws DatabaseException
153 protected abstract void _apply(WriteGraph g) throws DatabaseException;
156 * Returns resource that this operation is changing.
159 public abstract Resource getResource();
162 * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null.
165 public abstract Resource getCreatedResource();