]> gerrit.simantics Code Review - simantics/district.git/blobdiff - 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
diff --git a/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/EnableTechTypeItem.java b/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/EnableTechTypeItem.java
new file mode 100644 (file)
index 0000000..c202325
--- /dev/null
@@ -0,0 +1,64 @@
+package org.simantics.district.network.techtype.requests;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.Datatypes;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.impl.ArrayListBinding;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.utils.CommonDBUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.db.service.UndoRedoSupport;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+
+/**
+ * Change the enabled/disabled state of a single tech type table record
+ */
+public class EnableTechTypeItem extends WriteRequest {
+       
+       Resource table;
+       int itemIndex;
+       boolean enable;
+
+       public EnableTechTypeItem(Resource table, int itemIndex, boolean enable) {
+               super();
+               this.table = table;
+               this.itemIndex = itemIndex;
+               this.enable = enable;
+       }
+
+       @Override
+       public void perform(WriteGraph graph) throws DatabaseException {
+               DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+               
+               Binding binding = new ArrayListBinding(Datatypes.INTEGER_ARRAY, Bindings.INTEGER);
+               List<Integer> enabled = graph.getPossibleRelatedValue2(table, DN.TechType_TechTypeTable_HasEnabledItems, binding);
+               if (enabled == null)
+                       enabled = Collections.emptyList();
+               
+               if (enable) {
+                       if (!enabled.contains(itemIndex)) {
+                               enabled = new ArrayList<>(enabled);
+                               enabled.add((Integer) itemIndex);
+                               graph.getSession().markUndoPoint();
+                               graph.claimLiteral(table, DN.TechType_TechTypeTable_HasEnabledItems, enabled, binding);
+                               Layer0Utils.addCommentMetadata(graph, "Enable tech type table item " + (itemIndex + 1));
+                       }
+               } else {
+                       if (enabled.contains(itemIndex)) {
+                               enabled = new ArrayList<>(enabled);
+                               enabled.remove((Integer) itemIndex);  // note - without the cast, itemIndex is list index, not element value
+                               graph.getSession().markUndoPoint();
+                               graph.claimLiteral(table, DN.TechType_TechTypeTable_HasEnabledItems, enabled, binding);
+                               Layer0Utils.addCommentMetadata(graph, "Disable tech type table item " + (itemIndex + 1));
+                       }
+               }
+       }
+
+}