]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNElementRemover.java
06b31031f2efc794eca78c498aaaf5e0a1287e29
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / DNElementRemover.java
1 package org.simantics.district.network.ui;
2
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;
16
17 public class DNElementRemover extends ElementRemover {
18
19     private static final Logger LOGGER = LoggerFactory.getLogger(DNElementRemover.class);
20
21     public DNElementRemover(Resource element) {
22         super(element);
23     }
24     
25     @Override
26     public void removeConnection(WriteGraph graph) throws DatabaseException {
27         throw new UnsupportedOperationException("Distrct network should not have STR.Connection resources! " + resource);
28     }
29     
30     @Override
31     public void removeElement(WriteGraph graph) throws DatabaseException {
32         
33         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
34         
35         if (graph.isInstanceOf(resource, DN.Vertex)) {
36             graph.getObjects(resource, DN.HasEndVertex_Inverse).forEach(res -> {
37                 try {
38                     doRemove(graph, res);
39                 } catch (DatabaseException e) {
40                     LOGGER.error("Could not remove endvertex inverse for {}", resource, e);
41                 }
42             });
43             graph.getObjects(resource, DN.HasStartVertex_Inverse).forEach(res -> {
44                 try {
45                     doRemove(graph, res);
46                 } catch (DatabaseException e) {
47                     LOGGER.error("Could not remove startvertex inverse for {}", resource, e);
48                 }
49             });
50         }
51         doRemove(graph, resource);
52     }
53     
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);
58         }
59
60         removePossibleMappedComponents(graph, resource);
61         
62         // 2. Delete element itself
63         EntityRemover.remove(graph, resource);
64
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.
68     }
69
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);
77             }
78         }
79     }
80
81 }