]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network/src/org/simantics/district/network/changeset/DistrictChangeListener.java
Merge remote-tracking branch 'origin/release/1.37.0' into release/1.35.1
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / changeset / DistrictChangeListener.java
diff --git a/org.simantics.district.network/src/org/simantics/district/network/changeset/DistrictChangeListener.java b/org.simantics.district.network/src/org/simantics/district/network/changeset/DistrictChangeListener.java
new file mode 100644 (file)
index 0000000..39227a8
--- /dev/null
@@ -0,0 +1,84 @@
+package org.simantics.district.network.changeset;
+
+import java.util.Arrays;
+import java.util.Map;
+
+import org.simantics.db.MetadataI;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.changeset.GenericChangeListener;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.genericrelation.DependenciesRelation.DependencyChangesRequest;
+import org.simantics.db.layer0.genericrelation.DependencyChanges;
+import org.simantics.db.layer0.genericrelation.DependencyChanges.Change;
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentAddition;
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentModification;
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentRemoval;
+import org.simantics.db.layer0.genericrelation.DependencyChanges.LinkChange;
+import org.simantics.district.network.DistrictNetworkUtil;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.layer0.Layer0;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DistrictChangeListener extends GenericChangeListener<DependencyChangesRequest, DependencyChanges> {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(DistrictChangeListener.class);
+    
+    @Override
+    public void onEvent(ReadGraph graph, MetadataI metadata, DependencyChanges event) throws DatabaseException {
+        for (Map.Entry<Resource, Change[]> modelEntry : event.get().entrySet()) {
+
+            final Resource model = modelEntry.getKey();
+            final Change[] changes = modelEntry.getValue();
+            for (Change _entry : changes) {
+                if (_entry instanceof ComponentAddition) {
+                    ComponentAddition entry = (ComponentAddition) _entry;
+                    if (areWeInterested(graph, entry.component, entry.parent)) {
+                        elementAddition(graph, entry.component);
+                    }
+                } else if (_entry instanceof ComponentModification) {
+                    ComponentModification entry = (ComponentModification) _entry;
+                    if (areWeInterested(graph, entry.component, graph.getPossibleObject(entry.component, Layer0.getInstance(graph).PartOf))) {
+                        elementModification(graph, entry.component);
+                    }
+                } else if (_entry instanceof ComponentRemoval) {
+                    ComponentRemoval entry = (ComponentRemoval) _entry;
+                    elementRemoved(graph, entry.component);
+                } else if (_entry instanceof LinkChange) {
+                    // This can be ignored?
+                }
+            }
+            
+            changesInspected(graph, model);
+        }
+    }
+    
+    private static boolean areWeInterested(ReadGraph graph, Resource component, Resource parent) throws DatabaseException {
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        Boolean trackChangesEnabled = false;
+        if (parent != null) {
+            if (graph.isInstanceOf(parent, DN.Diagram)) {
+                trackChangesEnabled = DistrictNetworkUtil.trackChangesEnabled(graph, parent);
+            }
+        }
+        if (trackChangesEnabled) {
+            if (graph.isInstanceOf(component, DN.Edge) || graph.isInstanceOf(component, DN.Vertex)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    protected void elementAddition(ReadGraph graph, Resource element) throws DatabaseException {
+    }
+
+    protected void elementModification(ReadGraph graph, Resource element) throws DatabaseException {
+    }
+
+    protected void elementRemoved(ReadGraph graph, Resource element) throws DatabaseException {
+    }
+    
+    protected void changesInspected(ReadGraph graph, Resource model) throws DatabaseException {
+    }
+}