From 779d2b145f6eb9bba150f16c8e00eb8c162189f0 Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Mon, 12 Oct 2020 10:54:37 +0300 Subject: [PATCH] Remover for tech type tables gitlab #103 Change-Id: I839b5508e337c96b2f0d7842f77a7589acd25de3 --- .../adapters.xml | 6 +- .../ui/techtype/TechTypeTableRemover.java | 130 ++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/TechTypeTableRemover.java diff --git a/org.simantics.district.network.ui/adapters.xml b/org.simantics.district.network.ui/adapters.xml index c9adfafb..d8c60eca 100644 --- a/org.simantics.district.network.ui/adapters.xml +++ b/org.simantics.district.network.ui/adapters.xml @@ -33,6 +33,10 @@ class="org.simantics.district.network.ui.DNElementRemover"> + + + @@ -48,4 +52,4 @@ - \ No newline at end of file + 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 index 00000000..bac175a5 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/TechTypeTableRemover.java @@ -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 discoverInstances(WriteGraph graph, Resource componentType) throws DatabaseException { + Resource keyPredicate = graph.syncRequest(new PossibleTechTypeKeyPredicate(componentType), TransientCacheListener.instance()); + if (keyPredicate == null) + return Collections.emptySet(); + Map> data = graph.syncRequest(new TechTypeTableData(resource), TransientCacheListener.instance()); + + Set 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 instances, final String typeDescription, final String typeName) throws DatabaseException { + Map instanceAddresses = resolveInstanceAddresses(graph, instances); + final RemoveInstancesDialog.Content[] content = new RemoveInstancesDialog.Content[instanceAddresses.size()]; + int i = 0; + for (Map.Entry entry : instanceAddresses.entrySet()) { + content[i] = new RemoveInstancesDialog.Content(entry.getValue()); + ++i; + } + Arrays.sort(content, new Comparator() { + @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 { + 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; + } +} -- 2.47.1