]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java
PropertyChange class (instead of Pair)
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / PropertyChange.java
1 package org.simantics.interop.update.model;
2
3 import org.simantics.db.Resource;
4 import org.simantics.db.Statement;
5 import org.simantics.db.WriteGraph;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.interop.test.GraphChanges;
8 import org.simantics.utils.datastructures.Pair;
9
10 public class PropertyChange {
11         protected GraphChanges changes;
12         protected Pair<Statement, Statement> pair;
13         protected boolean applied = false;
14         protected boolean selected = false;
15         
16         
17         public PropertyChange(GraphChanges changes, Statement first, Statement second) {
18                 this.pair = new Pair<Statement, Statement>(first, second);
19                 this.changes = changes;
20         }
21         
22         public PropertyChange(GraphChanges changes, Pair<Statement, Statement> change) {
23                 this.pair = change;
24                 this.changes = changes;
25         }
26         
27         public Statement getFirst() {
28                 return pair.first;
29         }
30         
31         public Statement getSecond() {
32                 return pair.second;
33         }
34         
35         public GraphChanges getChanges() {
36                 return changes;
37         }
38         
39         @Override
40         public int hashCode() {
41                 return pair.hashCode();
42         }
43         
44         @Override
45         public boolean equals(Object obj) {
46                 if (obj == null)
47                         return false;
48                 if (obj.getClass() != this.getClass())
49                         return false;
50                 PropertyChange c = (PropertyChange)obj;
51                 return pair.equals(c.pair);
52         }
53         
54         public void apply(WriteGraph graph) throws DatabaseException {
55                 if (applied)
56                         return;
57                 if (pair.second == null) {
58                         graph.deny(pair.first);
59                         return;
60                 } 
61                 Resource s = changes.getComparable().getLeft(pair.second.getSubject());
62                 //Resource s = pair.first.getSubject();
63                 Resource pred = pair.second.getPredicate();
64                 if (graph.hasValue(pair.second.getObject())) {
65                         Object value = graph.getValue(pair.second.getObject());
66                         graph.claimLiteral(s, pred, value);
67                 } else {
68                         graph.deny(s,pred);
69                 }
70                 applied = true;
71                 
72         }
73         
74         public boolean select(boolean select) {
75                 if (applied)
76                         return false;
77                 this.selected = select;
78                 return true;
79         }
80         
81         
82         public boolean selected() {
83                 return selected;
84         }
85         
86         public boolean applied() {
87                 return applied;
88         }
89         
90         
91 }