1 package org.simantics.db.layer0.genericrelation;
3 import java.io.IOException;
4 import java.util.ArrayList;
6 import java.util.TreeMap;
8 import org.simantics.databoard.Databoard;
9 import org.simantics.databoard.annotations.Arguments;
10 import org.simantics.databoard.annotations.Union;
11 import org.simantics.databoard.binding.Binding;
12 import org.simantics.databoard.binding.error.BindingConstructionException;
13 import org.simantics.databoard.serialization.SerializationException;
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;
24 public class DependencyChanges implements Metadata {
26 public final @Arguments({Resource.class, Change[].class}) TreeMap<Resource, Change[]> modelChanges;
27 public final boolean hasUnresolved;
29 public DependencyChanges(TreeMap<Resource, Change[]> modelChanges, boolean hasUnresolved) {
30 this.modelChanges = modelChanges;
31 this.hasUnresolved = hasUnresolved;
34 public DependencyChanges(Map<Resource, ArrayList<Change>> _modelChanges, boolean hasUnresolved) {
35 this(new TreeMap<Resource, Change[]>(), hasUnresolved);
36 for(Map.Entry<Resource, ArrayList<Change>> entry : _modelChanges.entrySet()) {
37 ArrayList<Change> value = entry.getValue();
38 modelChanges.put(entry.getKey(), value.toArray(new Change[value.size()]));
42 public static enum ChangeType {
43 LINK_CHANGE, COMPONENT_ADDITION, COMPONENT_REMOVAL,
44 COMPONENT_MODIFICATION
47 @Union({LinkChange.class,
48 ComponentAddition.class,
49 ComponentRemoval.class,
50 ComponentModification.class})
51 public static interface Change {
53 String toString(ReadGraph graph) throws DatabaseException;
56 public static class LinkChange implements Change {
57 public final Resource component;
58 public LinkChange(Resource component) {
59 this.component = component;
62 public ChangeType getType() {
63 return ChangeType.LINK_CHANGE;
66 public String toString(ReadGraph graph) throws DatabaseException {
67 return "LinkChange[" + NameUtils.getSafeName(graph, component, true) + "]";
71 public static class ComponentAddition implements Change {
72 public final Resource component;
73 public final Resource parent;
74 public ComponentAddition(Resource component, Resource parent) {
75 this.component = component;
79 public ChangeType getType() {
80 return ChangeType.COMPONENT_ADDITION;
83 public String toString() {
84 return "ComponentAddition[" + component + "]";
86 public boolean isValid(ReadGraph graph) throws DatabaseException {
87 return graph.hasStatement(component, Layer0.getInstance(graph).PartOf, parent);
90 public String toString(ReadGraph graph) throws DatabaseException {
91 return "ComponentAddition[" + NameUtils.getSafeName(graph, component, true) + "]";
95 public static class ComponentRemoval implements Change {
96 public final Resource component;
97 public final Resource parent;
98 public ComponentRemoval(Resource component, Resource parent) {
99 this.component = component;
100 this.parent = parent;
103 public ChangeType getType() {
104 return ChangeType.COMPONENT_REMOVAL;
106 public boolean isValid(ReadGraph graph) throws DatabaseException {
107 return !graph.hasStatement(component, Layer0.getInstance(graph).PartOf, parent);
110 public String toString(ReadGraph graph) throws DatabaseException {
111 return "ComponentRemoval[component=" + NameUtils.getSafeName(graph, component, true) + ", parent=" + NameUtils.getSafeName(graph, parent, true) + "]";
115 public static class ComponentModification implements Change {
116 public final Resource component;
117 public ComponentModification(Resource component) {
118 this.component = component;
121 public ChangeType getType() {
122 return ChangeType.COMPONENT_MODIFICATION;
125 public String toString(ReadGraph graph) throws DatabaseException {
126 return "ComponentModification[" + NameUtils.getSafeName(graph, component, true) + "]";
130 public Change[] get(Resource model) {
131 return modelChanges.get(model);
134 public Map<Resource, Change[]> get() {
139 public byte[] serialise(Session session) {
141 Databoard databoard = session.getService( Databoard.class );
142 Binding binding = databoard.getBinding( DependencyChanges.class );
143 Serializer serializer = databoard.getSerializer( binding );
144 return serializer.serialize(this);
145 } catch (SerializationException e) {
147 } catch (IOException e) {
148 // TODO Auto-generated catch block
150 } catch (SerializerConstructionException e) {
151 // TODO Auto-generated catch block
153 } catch (BindingConstructionException e) {
154 // TODO Auto-generated catch block
160 public static DependencyChanges deserialise(Session session, byte[] input) {
162 Databoard databoard = session.getService( Databoard.class );
163 Binding binding = databoard.getBinding( DependencyChanges.class );
164 Serializer serializer = databoard.getSerializer( binding );
165 return (DependencyChanges) serializer.deserialize(input);
166 } catch (SerializationException e) {
168 } catch (IOException e) {
170 } catch (SerializerConstructionException e) {
172 } catch (BindingConstructionException e) {
178 public boolean isEmpty() {
179 return modelChanges.isEmpty() && !hasUnresolved;