]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependencyChanges.java
Replace e.printStackTrace() with Logger in DependencyChanges
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / genericrelation / DependencyChanges.java
1 package org.simantics.db.layer0.genericrelation;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Map;
7 import java.util.TreeMap;
8
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;
25
26 public class DependencyChanges implements Metadata {
27
28     private static final Logger LOGGER = LoggerFactory.getLogger(DependencyChanges.class);
29
30     public final @Arguments({Resource.class, Change[].class}) TreeMap<Resource, Change[]> modelChanges;
31     public final boolean hasUnresolved;
32     
33     public DependencyChanges(TreeMap<Resource, Change[]> modelChanges, boolean hasUnresolved) {
34         this.modelChanges = modelChanges;
35         this.hasUnresolved = hasUnresolved;
36     }        
37     
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()]));
43         }
44     }
45     
46     public static enum ChangeType {
47         LINK_CHANGE, COMPONENT_ADDITION, COMPONENT_REMOVAL,
48         COMPONENT_MODIFICATION
49     }
50         
51     @Union({LinkChange.class,
52         ComponentAddition.class, 
53         ComponentRemoval.class, 
54         ComponentModification.class})
55     public static interface Change {
56         ChangeType getType();
57         String toString(ReadGraph graph) throws DatabaseException;
58     }
59     
60     public static class LinkChange implements Change {
61         public final Resource component;
62         public LinkChange(Resource component) {
63             this.component = component;
64         }
65         @Override
66         public ChangeType getType() {
67             return ChangeType.LINK_CHANGE;
68         }
69         @Override
70         public String toString(ReadGraph graph) throws DatabaseException {
71                 return "LinkChange[" + NameUtils.getSafeName(graph, component, true) + "]";
72         }
73     }
74
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;
80             this.parent = parent;
81         }
82         @Override
83         public ChangeType getType() {
84             return ChangeType.COMPONENT_ADDITION;
85         }
86         @Override
87         public String toString() {
88                 return "ComponentAddition[" + component + "]";
89         }
90         public boolean isValid(ReadGraph graph) throws DatabaseException {
91                 return graph.hasStatement(component, Layer0.getInstance(graph).PartOf, parent);
92         }
93         @Override
94         public String toString(ReadGraph graph) throws DatabaseException {
95                 return "ComponentAddition[" + NameUtils.getSafeName(graph, component, true) + "]";
96         }
97     }
98     
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;
105         }
106         @Override
107         public ChangeType getType() {
108             return ChangeType.COMPONENT_REMOVAL;
109         }
110         public boolean isValid(ReadGraph graph) throws DatabaseException {
111                 return !graph.hasStatement(component, Layer0.getInstance(graph).PartOf, parent);
112         }
113         @Override
114         public String toString(ReadGraph graph) throws DatabaseException {
115                 return "ComponentRemoval[component=" + NameUtils.getSafeName(graph, component, true) + ", parent=" + NameUtils.getSafeName(graph, parent, true) + "]";
116         }
117     }
118     
119     public static class ComponentModification implements Change {
120         public final Resource component;
121         public ComponentModification(Resource component) {
122             this.component = component;
123         }
124         @Override
125         public ChangeType getType() {
126             return ChangeType.COMPONENT_MODIFICATION;
127         }
128         @Override
129         public String toString(ReadGraph graph) throws DatabaseException {
130                 return "ComponentModification[" + NameUtils.getSafeName(graph, component, true) + "]";
131         }
132     }
133         
134     public Change[] get(Resource model) {
135         return modelChanges.get(model);
136     }
137     
138     public Map<Resource, Change[]> get() {
139         return modelChanges;
140     }
141             
142     @Override
143     public byte[] serialise(Session session) {
144         try {
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);
151         }
152         return new byte[0];
153     }
154     
155     public static DependencyChanges deserialise(Session session, byte[] input) {
156         try {
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);
163         }
164         return null;
165     }
166     
167     public boolean isEmpty() {
168         return modelChanges.isEmpty() && !hasUnresolved;
169     }
170     
171 }