]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java
81a342a7ef079429d9d3c7afdcb76fb1171644d1
[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 Resource leftSubject;
13         protected Resource rightSubject;
14         protected Pair<Statement, Statement> pair;
15         protected boolean applied = false;
16         protected boolean selected = false;
17         protected boolean visible = true;
18         
19         
20         public PropertyChange(GraphChanges changes, Resource left, Statement first, Resource right, Statement second) {
21                 if (first == null && second == null)
22                         throw new IllegalArgumentException("At least one of the stamenents must be non null.");
23                 if (left == null || right == null)
24                         throw new IllegalArgumentException("Subject resources cannot be null.");
25                 this.pair = new Pair<Statement, Statement>(first, second);
26                 this.changes = changes;
27                 this.leftSubject = left;
28                 this.rightSubject = right;
29         }
30         
31         public PropertyChange(GraphChanges changes, Resource left, Resource right, Pair<Statement, Statement> change) {
32                 if (change == null || (change.first == null && change.second == null))
33                         throw new IllegalArgumentException("At least one of the stamenents must be non null.");
34                 if (left == null || right == null)
35                         throw new IllegalArgumentException("Subject resources cannot be null.");
36                 this.pair = change;
37                 this.changes = changes;
38                 this.leftSubject = left;
39                 this.rightSubject = right;
40         }
41         
42         
43         public Resource getFirstSubject() {
44                 return leftSubject;
45         }
46         
47         public Statement getFirst() {
48                 return pair.first;
49         }
50         
51         public Resource getSecondSubject() {
52                 return rightSubject;
53         }
54         
55         public Statement getSecond() {
56                 return pair.second;
57         }
58         
59         public Resource getPredicate() {
60             if (pair.first != null)
61                 return pair.first.getPredicate();
62             return pair.second.getPredicate();
63         }
64         
65         public GraphChanges getChanges() {
66                 return changes;
67         }
68         
69         @Override
70         public int hashCode() {
71                 return pair.hashCode();
72         }
73         
74         @Override
75         public boolean equals(Object obj) {
76                 if (obj == null)
77                         return false;
78                 if (obj.getClass() != this.getClass())
79                         return false;
80                 PropertyChange c = (PropertyChange)obj;
81                 return pair.equals(c.pair);
82         }
83         
84         public void apply(WriteGraph graph) throws DatabaseException {
85                 if (applied)
86                         return;
87                 if (pair.second == null) {
88                         graph.deny(leftSubject, pair.first.getPredicate(),pair.first.getObject());
89                         return;
90                 } 
91                 Resource s = leftSubject;
92                 //Resource s = changes.getComparable().getLeft(rightSubject);
93                 //Resource s = pair.first.getSubject();
94                 Resource pred = pair.second.getPredicate();
95                 if (graph.hasValue(pair.second.getObject())) {
96                         Object value = graph.getValue(pair.second.getObject());
97                         graph.deny(s, pred);
98                         graph.claimLiteral(s, pred, value);
99                 } else {
100                         graph.deny(s,pred);
101                 }
102                 applied = true;
103                 
104         }
105         
106         public boolean select(boolean select) {
107                 if (applied)
108                         return false;
109                 this.selected = select;
110                 return true;
111         }
112         
113         
114         public boolean selected() {
115                 return selected;
116         }
117         
118         public boolean applied() {
119                 return applied;
120         }
121         
122         public boolean isVisible() {
123                 return visible;
124         }
125         
126         public void setVisible(boolean visible) {
127                 this.visible = visible;
128         }
129         
130         @Override
131         public String toString() {
132                 String s = "PropertyChange";
133                 if (pair.first != null)
134                         s += " (" + (leftSubject) + " , " + pair.first.getPredicate() + " , " + pair.first.getObject() + ")";
135                 if (pair.second != null)
136                         s += " (" + (rightSubject) + " , " + pair.second.getPredicate() + " , " + pair.second.getObject() + ")";
137                 return s;
138         }
139         
140 }