package org.simantics.interop.update.model; import org.simantics.db.Resource; import org.simantics.db.Statement; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.interop.test.GraphChanges; import org.simantics.utils.datastructures.Pair; public class PropertyChange { protected GraphChanges changes; protected Pair pair; protected boolean applied = false; protected boolean selected = false; protected boolean visible = true; public PropertyChange(GraphChanges changes, Statement first, Statement second) { if (first == null && second == null) throw new IllegalArgumentException("At least one of the stamenents must be non null."); this.pair = new Pair(first, second); this.changes = changes; } public PropertyChange(GraphChanges changes, Pair change) { if (change == null || (change.first == null && change.second == null)) throw new IllegalArgumentException("At least one of the stamenents must be non null."); this.pair = change; this.changes = changes; } public Statement getFirst() { return pair.first; } public Statement getSecond() { return pair.second; } public GraphChanges getChanges() { return changes; } @Override public int hashCode() { return pair.hashCode(); } @Override public boolean equals(Object obj) { if (obj == null) return false; if (obj.getClass() != this.getClass()) return false; PropertyChange c = (PropertyChange)obj; return pair.equals(c.pair); } public void apply(WriteGraph graph) throws DatabaseException { if (applied) return; if (pair.second == null) { graph.deny(pair.first); return; } Resource s = changes.getComparable().getLeft(pair.second.getSubject()); //Resource s = pair.first.getSubject(); Resource pred = pair.second.getPredicate(); if (graph.hasValue(pair.second.getObject())) { Object value = graph.getValue(pair.second.getObject()); graph.claimLiteral(s, pred, value); } else { graph.deny(s,pred); } applied = true; } public boolean select(boolean select) { if (applied) return false; this.selected = select; return true; } public boolean selected() { return selected; } public boolean applied() { return applied; } public boolean isVisible() { return visible; } public void setVisible(boolean visible) { this.visible = visible; } @Override public String toString() { String s = "PropertyChange"; if (pair.first != null) s += " (" + (pair.first.getSubject()) + " , " + pair.first.getPredicate() + " , " + pair.first.getObject() + ")"; if (pair.second != null) s += " (" + (pair.second.getSubject()) + " , " + pair.second.getPredicate() + " , " + pair.second.getObject() + ")"; return s; } }