--- /dev/null
+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;
+ }
+}