001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.control.user.item.server.command;
018
019import com.echothree.control.user.item.common.edit.ItemEditFactory;
020import com.echothree.control.user.item.common.edit.ItemWeightEdit;
021import com.echothree.control.user.item.common.result.EditItemWeightResult;
022import com.echothree.control.user.item.common.result.ItemResultFactory;
023import com.echothree.control.user.item.common.spec.ItemWeightSpec;
024import com.echothree.model.control.item.server.control.ItemControl;
025import com.echothree.model.control.item.server.logic.ItemWeightTypeLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.control.uom.common.UomConstants;
030import com.echothree.model.control.uom.server.control.UomControl;
031import com.echothree.model.control.uom.server.util.Conversion;
032import com.echothree.model.data.item.server.entity.Item;
033import com.echothree.model.data.item.server.entity.ItemWeight;
034import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind;
035import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
036import com.echothree.util.common.command.EditMode;
037import com.echothree.util.common.message.ExecutionErrors;
038import com.echothree.util.common.validation.FieldDefinition;
039import com.echothree.util.common.validation.FieldType;
040import com.echothree.util.server.control.BaseAbstractEditCommand;
041import com.echothree.util.server.control.CommandSecurityDefinition;
042import com.echothree.util.server.control.PartyTypeDefinition;
043import com.echothree.util.server.control.SecurityRoleDefinition;
044import com.echothree.util.server.persistence.Session;
045import java.util.List;
046import javax.enterprise.context.RequestScoped;
047
048@RequestScoped
049public class EditItemWeightCommand
050        extends BaseAbstractEditCommand<ItemWeightSpec, ItemWeightEdit, EditItemWeightResult, ItemWeight, Item> {
051
052    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
053    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
054    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
055    
056    static {
057        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
058                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
059                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
060                        new SecurityRoleDefinition(SecurityRoleGroups.ItemWeight.name(), SecurityRoles.Edit.name())
061                ))
062        ));
063
064        SPEC_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("ItemWeightTypeName", FieldType.ENTITY_NAME, true, null, null)
068        );
069        
070        EDIT_FIELD_DEFINITIONS = List.of(
071                new FieldDefinition("WeightUnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("Weight", FieldType.UNSIGNED_LONG, true, null, null)
073        );
074    }
075    
076    /** Creates a new instance of EditItemWeightCommand */
077    public EditItemWeightCommand() {
078        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
079    }
080    
081    @Override
082    public EditItemWeightResult getResult() {
083        return ItemResultFactory.getEditItemWeightResult();
084    }
085
086    @Override
087    public ItemWeightEdit getEdit() {
088        return ItemEditFactory.getItemWeightEdit();
089    }
090
091    UnitOfMeasureKind volumeUnitOfMeasureKind;
092
093    @Override
094    public ItemWeight getEntity(EditItemWeightResult result) {
095        var itemControl = Session.getModelController(ItemControl.class);
096        var uomControl = Session.getModelController(UomControl.class);
097        ItemWeight itemWeight = null;
098        var itemName = spec.getItemName();
099        var item = itemControl.getItemByName(itemName);
100
101        if(item != null) {
102            var unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName();
103            var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(item.getLastDetail().getUnitOfMeasureKind(), unitOfMeasureTypeName);
104
105            if(unitOfMeasureType != null) {
106                var itemWeightType = ItemWeightTypeLogic.getInstance().getItemWeightTypeByName(this, spec.getItemWeightTypeName());
107
108                if(!hasExecutionErrors()) {
109                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
110                        itemWeight = itemControl.getItemWeight(item, unitOfMeasureType, itemWeightType);
111                    } else { // EditMode.UPDATE
112                        itemWeight = itemControl.getItemWeightForUpdate(item, unitOfMeasureType, itemWeightType);
113                    }
114
115                    if(itemWeight == null) {
116                        addExecutionError(ExecutionErrors.UnknownItemWeight.name(), item.getLastDetail().getItemName(),
117                                unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(),
118                                itemWeightType.getLastDetail().getItemWeightTypeName());
119                    }
120                }
121            } else {
122                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
123            }
124        } else {
125            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
126        }
127
128        if(!hasExecutionErrors() && (editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.UPDATE))) {
129            volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_WEIGHT);
130
131            if(volumeUnitOfMeasureKind == null) {
132                addExecutionError(ExecutionErrors.UnknownWeightUnitOfMeasureKind.name());
133            }
134        }
135
136        return itemWeight;
137    }
138
139    @Override
140    public Item getLockEntity(ItemWeight itemWeight) {
141        return itemWeight.getItem();
142    }
143
144    @Override
145    public void fillInResult(EditItemWeightResult result, ItemWeight itemWeight) {
146        var itemControl = Session.getModelController(ItemControl.class);
147
148        result.setItemWeight(itemControl.getItemWeightTransfer(getUserVisit(), itemWeight));
149    }
150
151    @Override
152    public void doLock(ItemWeightEdit edit, ItemWeight itemWeight) {
153        var uomControl = Session.getModelController(UomControl.class);
154
155        weight = itemWeight.getWeight();
156        var weightConversion = weight == null ? null : new Conversion(uomControl, volumeUnitOfMeasureKind, weight).convertToHighestUnitOfMeasureType();
157
158        edit.setWeight(weightConversion.getQuantity().toString());
159        edit.setWeightUnitOfMeasureTypeName(weightConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
160    }
161
162    UnitOfMeasureType weightUnitOfMeasureType;
163    Long weight;
164
165    @Override
166    public void canUpdate(ItemWeight itemWeight) {
167        var uomControl = Session.getModelController(UomControl.class);
168        var weightUnitOfMeasureTypeName = edit.getWeightUnitOfMeasureTypeName();
169
170        weightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, weightUnitOfMeasureTypeName);
171
172        if(weightUnitOfMeasureType != null) {
173            weight = Long.valueOf(edit.getWeight());
174
175            if(weight < 1) {
176                addExecutionError(ExecutionErrors.InvalidWeight.name(), weight);
177            }
178        } else {
179            addExecutionError(ExecutionErrors.UnknownWeightUnitOfMeasureTypeName.name(), weightUnitOfMeasureTypeName);
180        }
181    }
182
183    @Override
184    public void doUpdate(ItemWeight itemWeight) {
185        var itemControl = Session.getModelController(ItemControl.class);
186        var uomControl = Session.getModelController(UomControl.class);
187        var itemWeightValue = itemControl.getItemWeightValue(itemWeight);
188
189        var weightConversion = new Conversion(uomControl, weightUnitOfMeasureType, weight).convertToLowestUnitOfMeasureType();
190
191        itemWeightValue.setWeight(weightConversion.getQuantity());
192
193        itemControl.updateItemWeightFromValue(itemWeightValue, getPartyPK());
194    }
195    
196}