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