]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Remover for tech type tables 53/4553/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 12 Oct 2020 07:54:37 +0000 (10:54 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Mon, 12 Oct 2020 09:02:20 +0000 (09:02 +0000)
gitlab #103

Change-Id: I839b5508e337c96b2f0d7842f77a7589acd25de3
(cherry picked from commit 779d2b145f6eb9bba150f16c8e00eb8c162189f0)

org.simantics.district.network.ui/adapters.xml
org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/TechTypeTableRemover.java [new file with mode: 0644]

index c9adfafbdad704b48713966b7730f489769305f5..d8c60eca4f98b3456bfa0a51ce3098eefb9eb2a3 100644 (file)
             class="org.simantics.district.network.ui.DNElementRemover">
             <this />
         </type>
+        <type uri="http://www.simantics.org/DistrictNetwork-1.0/TechType/TechTypeTable"
+            class="org.simantics.district.network.ui.techtype.TechTypeTableRemover">
+            <this />
+        </type>
     </target>
     
     <target interface="org.simantics.db.layer0.adapter.Template">
@@ -48,4 +52,4 @@
             <this />
         </resource>
     </target>
-</adapters>
\ No newline at end of file
+</adapters>
diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/TechTypeTableRemover.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/TechTypeTableRemover.java
new file mode 100644 (file)
index 0000000..bac175a
--- /dev/null
@@ -0,0 +1,130 @@
+package org.simantics.district.network.ui.techtype;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.ui.PlatformUI;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.district.network.techtype.requests.TechTypeTableData;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.adapters.ExistingInstancesRemover;
+import org.simantics.modeling.adapters.RemoveInstancesDialog;
+import org.simantics.modeling.adapters.RemoveInstancesDialog.Content;
+import org.simantics.utils.strings.AlphanumComparator;
+
+public class TechTypeTableRemover extends ExistingInstancesRemover {
+
+       public TechTypeTableRemover(Resource resource) {
+               super(resource, "Tech Type Table");
+       }
+       
+       @Override
+       public void remove(WriteGraph graph) throws DatabaseException {
+               DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+               
+               Resource componentType = graph.getPossibleObject(resource, DN.TechType_TechTypeTable_HasComponentType);
+               if (componentType == null) {
+                       justRemove(graph);
+                       return;
+               }
+               
+               super.remove(graph, resource, componentType, componentType);
+       }
+
+       @Override
+       protected Set<Resource> discoverInstances(WriteGraph graph, Resource componentType) throws DatabaseException {
+               Resource keyPredicate = graph.syncRequest(new PossibleTechTypeKeyPredicate(componentType), TransientCacheListener.instance());
+               if (keyPredicate == null)
+                       return Collections.emptySet();
+               Map<String, Map<String, String>> data = graph.syncRequest(new TechTypeTableData(resource), TransientCacheListener.instance());
+               
+               Set<Resource> used = new HashSet<>();
+               for (Resource comp : super.discoverInstances(graph, componentType)) {
+                       Object code = graph.getPossibleRelatedValue(comp, keyPredicate);
+                       if (code != null && code instanceof String && data.containsKey(code)) {
+                               used.add(comp);
+                       }
+               }
+               
+               return used;
+       }
+       
+       @Override
+       protected void confirmRemoval(WriteGraph graph, final Set<Resource> instances, final String typeDescription, final String typeName) throws DatabaseException {
+               Map<Resource, String> instanceAddresses = resolveInstanceAddresses(graph, instances);
+               final RemoveInstancesDialog.Content[] content = new RemoveInstancesDialog.Content[instanceAddresses.size()];
+               int i = 0;
+               for (Map.Entry<Resource, String> entry : instanceAddresses.entrySet()) {
+                       content[i] = new RemoveInstancesDialog.Content(entry.getValue());
+                       ++i;
+               }
+               Arrays.sort(content, new Comparator<RemoveInstancesDialog.Content>() {
+                       @Override
+                       public int compare(Content o1, Content o2) {
+                               return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.label, o2.label);
+                       }
+               });
+
+               final Session session = graph.getSession();
+
+               if (PlatformUI.isWorkbenchRunning()) {
+                       PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
+                               @Override
+                               public void run() {
+                                       RemoveInstancesDialog dialog = new RemoveInstancesDialog(
+                                                       PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
+                                                       "Remove " + typeDescription + "?",
+                                                       typeDescription + " for '" + typeName + "' is still in use in " + instances.size() + " instances. Are you sure you want to remove it?",
+                                                       content);
+                                       int result = dialog.open();
+                                       boolean doIt = result == IDialogConstants.OK_ID;
+                                       if (!doIt)
+                                               return;
+                                       session.asyncRequest(new WriteRequest() {
+                                               @Override
+                                               public void perform(WriteGraph graph) throws DatabaseException {
+                                                       justRemove(graph);
+                                               }
+                                       });
+                               }
+                       });
+               } else {
+                       // Just do it without confirmation when no user agent is available.
+                       justRemove(graph);
+               }
+       }
+}
+
+class PossibleTechTypeKeyPredicate extends ResourceRead<Resource> {
+       public PossibleTechTypeKeyPredicate(Resource componentType) {
+               super(componentType);
+       }
+
+       @Override
+       public Resource perform(ReadGraph graph) throws DatabaseException {
+               Layer0 L0 = Layer0.getInstance(graph);
+               DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+               
+               for (Resource r : graph.getObjects(resource, L0.DomainOf)) {
+                       Resource accessor = graph.getPossibleObject(r, L0.valueAccessor);
+                       if (accessor.equals(DN.TechType_Functions_techTypeCodeValueAccessor)) {
+                               return r;
+                       }
+               }
+               
+               return null;
+       }
+}