1 package org.simantics.db.layer0.genericrelation;
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
7 import java.util.TreeMap;
9 import org.simantics.databoard.Databoard;
10 import org.simantics.databoard.annotations.Arguments;
11 import org.simantics.databoard.annotations.Union;
12 import org.simantics.databoard.binding.Binding;
13 import org.simantics.databoard.binding.error.BindingConstructionException;
14 import org.simantics.databoard.serialization.Serializer;
15 import org.simantics.databoard.serialization.SerializerConstructionException;
16 import org.simantics.db.Metadata;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.layer0.Layer0;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class DependencyChanges implements Metadata {
28 private static final Logger LOGGER = LoggerFactory.getLogger(DependencyChanges.class);
30 public final @Arguments({Resource.class, Change[].class}) TreeMap<Resource, Change[]> modelChanges;
31 public final boolean hasUnresolved;
33 public DependencyChanges(TreeMap<Resource, Change[]> modelChanges, boolean hasUnresolved) {
34 this.modelChanges = modelChanges;
35 this.hasUnresolved = hasUnresolved;
38 public DependencyChanges(Map<Resource, ArrayList<Change>> _modelChanges, boolean hasUnresolved) {
39 this(new TreeMap<Resource, Change[]>(), hasUnresolved);
40 for(Map.Entry<Resource, ArrayList<Change>> entry : _modelChanges.entrySet()) {
41 ArrayList<Change> value = entry.getValue();
42 modelChanges.put(entry.getKey(), value.toArray(new Change[value.size()]));
46 public static enum ChangeType {
47 LINK_CHANGE, COMPONENT_ADDITION, COMPONENT_REMOVAL,
48 COMPONENT_MODIFICATION
51 @Union({LinkChange.class,
52 ComponentAddition.class,
53 ComponentRemoval.class,
54 ComponentModification.class})
55 public static interface Change {
57 String toString(ReadGraph graph) throws DatabaseException;
60 public static class LinkChange implements Change {
61 public final Resource component;
62 public LinkChange(Resource component) {
63 this.component = component;
66 public ChangeType getType() {
67 return ChangeType.LINK_CHANGE;
70 public String toString(ReadGraph graph) throws DatabaseException {
71 return "LinkChange[" + NameUtils.getSafeName(graph, component, true) + "]";
75 public static class ComponentAddition implements Change {
76 public final Resource component;
77 public final Resource parent;
78 public ComponentAddition(Resource component, Resource parent) {
79 this.component = component;
83 public ChangeType getType() {
84 return ChangeType.COMPONENT_ADDITION;
87 public String toString() {
88 return "ComponentAddition[" + component + "]";
90 public boolean isValid(ReadGraph graph) throws DatabaseException {
91 return graph.hasStatement(component, Layer0.getInstance(graph).PartOf, parent);
94 public String toString(ReadGraph graph) throws DatabaseException {
95 return "ComponentAddition[" + NameUtils.getSafeName(graph, component, true) + "]";
99 public static class ComponentRemoval implements Change {
100 public final Resource component;
101 public final Resource parent;
102 public ComponentRemoval(Resource component, Resource parent) {
103 this.component = component;
104 this.parent = parent;
107 public ChangeType getType() {
108 return ChangeType.COMPONENT_REMOVAL;
110 public boolean isValid(ReadGraph graph) throws DatabaseException {
111 return !graph.hasStatement(component, Layer0.getInstance(graph).PartOf, parent);
114 public String toString(ReadGraph graph) throws DatabaseException {
115 return "ComponentRemoval[component=" + NameUtils.getSafeName(graph, component, true) + ", parent=" + NameUtils.getSafeName(graph, parent, true) + "]";
119 public static class ComponentModification implements Change {
120 public final Resource component;
121 public ComponentModification(Resource component) {
122 this.component = component;
125 public ChangeType getType() {
126 return ChangeType.COMPONENT_MODIFICATION;
129 public String toString(ReadGraph graph) throws DatabaseException {
130 return "ComponentModification[" + NameUtils.getSafeName(graph, component, true) + "]";
134 public Change[] get(Resource model) {
135 return modelChanges.get(model);
138 public Map<Resource, Change[]> get() {
143 public byte[] serialise(Session session) {
145 Databoard databoard = session.getService( Databoard.class );
146 Binding binding = databoard.getBinding( DependencyChanges.class );
147 Serializer serializer = databoard.getSerializer( binding );
148 return serializer.serialize(this);
149 } catch (IOException | SerializerConstructionException | BindingConstructionException e) {
150 LOGGER.error("Could not serialise {} {}", getClass().getSimpleName(), this, e);
155 public static DependencyChanges deserialise(Session session, byte[] input) {
157 Databoard databoard = session.getService( Databoard.class );
158 Binding binding = databoard.getBinding( DependencyChanges.class );
159 Serializer serializer = databoard.getSerializer( binding );
160 return (DependencyChanges) serializer.deserialize(input);
161 } catch (IOException | SerializerConstructionException | BindingConstructionException e) {
162 LOGGER.error("Could not deserialise {}", Arrays.toString(input), e);
167 public boolean isEmpty() {
168 return modelChanges.isEmpty() && !hasUnresolved;