]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/TechTypeTableRemover.java
Remover for tech type tables
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / techtype / TechTypeTableRemover.java
1 package org.simantics.district.network.ui.techtype;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashSet;
7 import java.util.Map;
8 import java.util.Set;
9
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;
27
28 public class TechTypeTableRemover extends ExistingInstancesRemover {
29
30         public TechTypeTableRemover(Resource resource) {
31                 super(resource, "Tech Type Table");
32         }
33         
34         @Override
35         public void remove(WriteGraph graph) throws DatabaseException {
36                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
37                 
38                 Resource componentType = graph.getPossibleObject(resource, DN.TechType_TechTypeTable_HasComponentType);
39                 if (componentType == null) {
40                         justRemove(graph);
41                         return;
42                 }
43                 
44                 super.remove(graph, resource, componentType, componentType);
45         }
46
47         @Override
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());
53                 
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)) {
58                                 used.add(comp);
59                         }
60                 }
61                 
62                 return used;
63         }
64         
65         @Override
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()];
69                 int i = 0;
70                 for (Map.Entry<Resource, String> entry : instanceAddresses.entrySet()) {
71                         content[i] = new RemoveInstancesDialog.Content(entry.getValue());
72                         ++i;
73                 }
74                 Arrays.sort(content, new Comparator<RemoveInstancesDialog.Content>() {
75                         @Override
76                         public int compare(Content o1, Content o2) {
77                                 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.label, o2.label);
78                         }
79                 });
80
81                 final Session session = graph.getSession();
82
83                 if (PlatformUI.isWorkbenchRunning()) {
84                         PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
85                                 @Override
86                                 public void run() {
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?",
91                                                         content);
92                                         int result = dialog.open();
93                                         boolean doIt = result == IDialogConstants.OK_ID;
94                                         if (!doIt)
95                                                 return;
96                                         session.asyncRequest(new WriteRequest() {
97                                                 @Override
98                                                 public void perform(WriteGraph graph) throws DatabaseException {
99                                                         justRemove(graph);
100                                                 }
101                                         });
102                                 }
103                         });
104                 } else {
105                         // Just do it without confirmation when no user agent is available.
106                         justRemove(graph);
107                 }
108         }
109 }
110
111 class PossibleTechTypeKeyPredicate extends ResourceRead<Resource> {
112         public PossibleTechTypeKeyPredicate(Resource componentType) {
113                 super(componentType);
114         }
115
116         @Override
117         public Resource perform(ReadGraph graph) throws DatabaseException {
118                 Layer0 L0 = Layer0.getInstance(graph);
119                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
120                 
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)) {
124                                 return r;
125                         }
126                 }
127                 
128                 return null;
129         }
130 }