]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/techtype/requests/EnableTechTypeItem.java
Add enable/disable feature for tech type tables
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / techtype / requests / EnableTechTypeItem.java
1 package org.simantics.district.network.techtype.requests;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.Datatypes;
9 import org.simantics.databoard.binding.Binding;
10 import org.simantics.databoard.binding.impl.ArrayListBinding;
11 import org.simantics.db.Resource;
12 import org.simantics.db.WriteGraph;
13 import org.simantics.db.common.request.WriteRequest;
14 import org.simantics.db.common.utils.CommonDBUtils;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.util.Layer0Utils;
17 import org.simantics.db.service.UndoRedoSupport;
18 import org.simantics.district.network.ontology.DistrictNetworkResource;
19
20 /**
21  * Change the enabled/disabled state of a single tech type table record
22  */
23 public class EnableTechTypeItem extends WriteRequest {
24         
25         Resource table;
26         int itemIndex;
27         boolean enable;
28
29         public EnableTechTypeItem(Resource table, int itemIndex, boolean enable) {
30                 super();
31                 this.table = table;
32                 this.itemIndex = itemIndex;
33                 this.enable = enable;
34         }
35
36         @Override
37         public void perform(WriteGraph graph) throws DatabaseException {
38                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
39                 
40                 Binding binding = new ArrayListBinding(Datatypes.INTEGER_ARRAY, Bindings.INTEGER);
41                 List<Integer> enabled = graph.getPossibleRelatedValue2(table, DN.TechType_TechTypeTable_HasEnabledItems, binding);
42                 if (enabled == null)
43                         enabled = Collections.emptyList();
44                 
45                 if (enable) {
46                         if (!enabled.contains(itemIndex)) {
47                                 enabled = new ArrayList<>(enabled);
48                                 enabled.add((Integer) itemIndex);
49                                 graph.getSession().markUndoPoint();
50                                 graph.claimLiteral(table, DN.TechType_TechTypeTable_HasEnabledItems, enabled, binding);
51                                 Layer0Utils.addCommentMetadata(graph, "Enable tech type table item " + (itemIndex + 1));
52                         }
53                 } else {
54                         if (enabled.contains(itemIndex)) {
55                                 enabled = new ArrayList<>(enabled);
56                                 enabled.remove((Integer) itemIndex);  // note - without the cast, itemIndex is list index, not element value
57                                 graph.getSession().markUndoPoint();
58                                 graph.claimLiteral(table, DN.TechType_TechTypeTable_HasEnabledItems, enabled, binding);
59                                 Layer0Utils.addCommentMetadata(graph, "Disable tech type table item " + (itemIndex + 1));
60                         }
61                 }
62         }
63
64 }