1 package org.simantics.district.network.ui;
3 import org.simantics.db.Resource;
4 import org.simantics.db.WriteGraph;
5 import org.simantics.db.common.utils.OrderedSetUtils;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.db.layer0.adapter.impl.EntityRemover;
8 import org.simantics.db.layer0.util.RemoverUtil;
9 import org.simantics.district.network.DistrictNetworkUtil;
10 import org.simantics.district.network.ontology.DistrictNetworkResource;
11 import org.simantics.layer0.Layer0;
12 import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;
13 import org.simantics.modeling.adapters.ElementRemover;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 public class DNElementRemover extends ElementRemover {
19 private static final Logger LOGGER = LoggerFactory.getLogger(DNElementRemover.class);
21 public DNElementRemover(Resource element) {
26 public void removeConnection(WriteGraph graph) throws DatabaseException {
27 throw new UnsupportedOperationException("Distrct network should not have STR.Connection resources! " + resource);
31 public void removeElement(WriteGraph graph) throws DatabaseException {
33 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
35 if (graph.isInstanceOf(resource, DN.Vertex)) {
36 graph.getObjects(resource, DN.HasEndVertex_Inverse).forEach(res -> {
39 } catch (DatabaseException e) {
40 LOGGER.error("Could not remove endvertex inverse for {}", resource, e);
43 graph.getObjects(resource, DN.HasStartVertex_Inverse).forEach(res -> {
46 } catch (DatabaseException e) {
47 LOGGER.error("Could not remove startvertex inverse for {}", resource, e);
51 doRemove(graph, resource);
54 private static void doRemove(WriteGraph graph, Resource resource) throws DatabaseException {
55 // 1. Disconnect element from diagrams
56 for (Resource diagram : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) {
57 OrderedSetUtils.remove(graph, diagram, resource);
60 removePossibleMappedComponents(graph, resource);
62 // 2. Delete element itself
63 EntityRemover.remove(graph, resource);
65 // 3. Recursively remove all related degenerate connections
66 // i.e. connections that only have one connector after remove the
67 // one connected to this removed element.
70 private static void removePossibleMappedComponents(WriteGraph graph, Resource resource) throws DatabaseException {
71 Boolean trackChangesEnabled = DistrictNetworkUtil.trackChangesEnabled(graph, graph.getPossibleObject(resource, Layer0.getInstance(graph).PartOf));
72 if (trackChangesEnabled) {
73 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
74 Resource mappedComponent = graph.getPossibleObject(resource, DN.MappedComponent);
75 if (mappedComponent != null) {
76 RemoverUtil.remove(graph, mappedComponent);