]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/changeset/DistrictChangeListener.java
39227a872777c75c3ee5a781bc7cc68051feb465
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / changeset / DistrictChangeListener.java
1 package org.simantics.district.network.changeset;
2
3 import java.util.Arrays;
4 import java.util.Map;
5
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;
23
24 public class DistrictChangeListener extends GenericChangeListener<DependencyChangesRequest, DependencyChanges> {
25
26     private static final Logger LOGGER = LoggerFactory.getLogger(DistrictChangeListener.class);
27     
28     @Override
29     public void onEvent(ReadGraph graph, MetadataI metadata, DependencyChanges event) throws DatabaseException {
30         for (Map.Entry<Resource, Change[]> modelEntry : event.get().entrySet()) {
31
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);
39                     }
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);
44                     }
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?
50                 }
51             }
52             
53             changesInspected(graph, model);
54         }
55     }
56     
57     private static boolean areWeInterested(ReadGraph graph, Resource component, Resource parent) throws DatabaseException {
58         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
59         Boolean trackChangesEnabled = false;
60         if (parent != null) {
61             if (graph.isInstanceOf(parent, DN.Diagram)) {
62                 trackChangesEnabled = DistrictNetworkUtil.trackChangesEnabled(graph, parent);
63             }
64         }
65         if (trackChangesEnabled) {
66             if (graph.isInstanceOf(component, DN.Edge) || graph.isInstanceOf(component, DN.Vertex)) {
67                 return true;
68             }
69         }
70         return false;
71     }
72
73     protected void elementAddition(ReadGraph graph, Resource element) throws DatabaseException {
74     }
75
76     protected void elementModification(ReadGraph graph, Resource element) throws DatabaseException {
77     }
78
79     protected void elementRemoved(ReadGraph graph, Resource element) throws DatabaseException {
80     }
81     
82     protected void changesInspected(ReadGraph graph, Resource model) throws DatabaseException {
83     }
84 }