X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Ftechtype%2Frequests%2FEnableTechTypeItem.java;fp=org.simantics.district.network%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Ftechtype%2Frequests%2FEnableTechTypeItem.java;h=c202325ee506829e04c29e51addef4bd50f0472c;hb=29af77f249a4842bfd3f9280755121c9c98b32a1;hp=0000000000000000000000000000000000000000;hpb=62f9a86961adc4fd44782e3c2f79852b1269810d;p=simantics%2Fdistrict.git 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 index 00000000..c202325e --- /dev/null +++ b/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/EnableTechTypeItem.java @@ -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 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)); + } + } + } + +}