1 package org.simantics.district.network.ui.techtype;
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashSet;
10 import org.eclipse.jface.dialogs.IDialogConstants;
11 import org.eclipse.ui.PlatformUI;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.Session;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
17 import org.simantics.db.common.request.ResourceRead;
18 import org.simantics.db.common.request.WriteRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.district.network.ontology.DistrictNetworkResource;
21 import org.simantics.district.network.techtype.requests.TechTypeTableData;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.modeling.adapters.ExistingInstancesRemover;
24 import org.simantics.modeling.adapters.RemoveInstancesDialog;
25 import org.simantics.modeling.adapters.RemoveInstancesDialog.Content;
26 import org.simantics.utils.strings.AlphanumComparator;
28 public class TechTypeTableRemover extends ExistingInstancesRemover {
30 public TechTypeTableRemover(Resource resource) {
31 super(resource, "Tech Type Table");
35 public void remove(WriteGraph graph) throws DatabaseException {
36 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
38 Resource componentType = graph.getPossibleObject(resource, DN.TechType_TechTypeTable_HasComponentType);
39 if (componentType == null) {
44 super.remove(graph, resource, componentType, componentType);
48 protected Set<Resource> discoverInstances(WriteGraph graph, Resource componentType) throws DatabaseException {
49 Resource keyPredicate = graph.syncRequest(new PossibleTechTypeKeyPredicate(componentType), TransientCacheListener.instance());
50 if (keyPredicate == null)
51 return Collections.emptySet();
52 Map<String, Map<String, String>> data = graph.syncRequest(new TechTypeTableData(resource), TransientCacheListener.instance());
54 Set<Resource> used = new HashSet<>();
55 for (Resource comp : super.discoverInstances(graph, componentType)) {
56 Object code = graph.getPossibleRelatedValue(comp, keyPredicate);
57 if (code != null && code instanceof String && data.containsKey(code)) {
66 protected void confirmRemoval(WriteGraph graph, final Set<Resource> instances, final String typeDescription, final String typeName) throws DatabaseException {
67 Map<Resource, String> instanceAddresses = resolveInstanceAddresses(graph, instances);
68 final RemoveInstancesDialog.Content[] content = new RemoveInstancesDialog.Content[instanceAddresses.size()];
70 for (Map.Entry<Resource, String> entry : instanceAddresses.entrySet()) {
71 content[i] = new RemoveInstancesDialog.Content(entry.getValue());
74 Arrays.sort(content, new Comparator<RemoveInstancesDialog.Content>() {
76 public int compare(Content o1, Content o2) {
77 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.label, o2.label);
81 final Session session = graph.getSession();
83 if (PlatformUI.isWorkbenchRunning()) {
84 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
87 RemoveInstancesDialog dialog = new RemoveInstancesDialog(
88 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
89 "Remove " + typeDescription + "?",
90 typeDescription + " for '" + typeName + "' is still in use in " + instances.size() + " instances. Are you sure you want to remove it?",
92 int result = dialog.open();
93 boolean doIt = result == IDialogConstants.OK_ID;
96 session.asyncRequest(new WriteRequest() {
98 public void perform(WriteGraph graph) throws DatabaseException {
105 // Just do it without confirmation when no user agent is available.
111 class PossibleTechTypeKeyPredicate extends ResourceRead<Resource> {
112 public PossibleTechTypeKeyPredicate(Resource componentType) {
113 super(componentType);
117 public Resource perform(ReadGraph graph) throws DatabaseException {
118 Layer0 L0 = Layer0.getInstance(graph);
119 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
121 for (Resource r : graph.getObjects(resource, L0.DomainOf)) {
122 Resource accessor = graph.getPossibleObject(r, L0.valueAccessor);
123 if (accessor.equals(DN.TechType_Functions_techTypeCodeValueAccessor)) {