1 package org.simantics.district.network.changeset;
3 import java.util.Arrays;
6 import org.simantics.db.MetadataI;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.changeset.GenericChangeListener;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.genericrelation.DependenciesRelation.DependencyChangesRequest;
12 import org.simantics.db.layer0.genericrelation.DependencyChanges;
13 import org.simantics.db.layer0.genericrelation.DependencyChanges.Change;
14 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentAddition;
15 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentModification;
16 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentRemoval;
17 import org.simantics.db.layer0.genericrelation.DependencyChanges.LinkChange;
18 import org.simantics.district.network.DistrictNetworkUtil;
19 import org.simantics.district.network.ontology.DistrictNetworkResource;
20 import org.simantics.layer0.Layer0;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class DistrictChangeListener extends GenericChangeListener<DependencyChangesRequest, DependencyChanges> {
26 private static final Logger LOGGER = LoggerFactory.getLogger(DistrictChangeListener.class);
29 public void onEvent(ReadGraph graph, MetadataI metadata, DependencyChanges event) throws DatabaseException {
30 for (Map.Entry<Resource, Change[]> modelEntry : event.get().entrySet()) {
32 final Resource model = modelEntry.getKey();
33 final Change[] changes = modelEntry.getValue();
34 for (Change _entry : changes) {
35 if (_entry instanceof ComponentAddition) {
36 ComponentAddition entry = (ComponentAddition) _entry;
37 if (areWeInterested(graph, entry.component, entry.parent)) {
38 elementAddition(graph, entry.component);
40 } else if (_entry instanceof ComponentModification) {
41 ComponentModification entry = (ComponentModification) _entry;
42 if (areWeInterested(graph, entry.component, graph.getPossibleObject(entry.component, Layer0.getInstance(graph).PartOf))) {
43 elementModification(graph, entry.component);
45 } else if (_entry instanceof ComponentRemoval) {
46 ComponentRemoval entry = (ComponentRemoval) _entry;
47 elementRemoved(graph, entry.component);
48 } else if (_entry instanceof LinkChange) {
49 // This can be ignored?
53 changesInspected(graph, model);
57 private static boolean areWeInterested(ReadGraph graph, Resource component, Resource parent) throws DatabaseException {
58 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
59 Boolean trackChangesEnabled = false;
61 if (graph.isInstanceOf(parent, DN.Diagram)) {
62 trackChangesEnabled = DistrictNetworkUtil.trackChangesEnabled(graph, parent);
65 if (trackChangesEnabled) {
66 if (graph.isInstanceOf(component, DN.Edge) || graph.isInstanceOf(component, DN.Vertex)) {
73 protected void elementAddition(ReadGraph graph, Resource element) throws DatabaseException {
76 protected void elementModification(ReadGraph graph, Resource element) throws DatabaseException {
79 protected void elementRemoved(ReadGraph graph, Resource element) throws DatabaseException {
82 protected void changesInspected(ReadGraph graph, Resource model) throws DatabaseException {