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