001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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 java.util.List;
045import javax.enterprise.context.Dependent;
046import javax.inject.Inject;
047
048@Dependent
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    @Inject
077    ItemControl itemControl;
078
079    @Inject
080    UomControl uomControl;
081
082    @Inject
083    ItemWeightTypeLogic itemWeightTypeLogic;
084
085    
086    /** Creates a new instance of EditItemWeightCommand */
087    public EditItemWeightCommand() {
088        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
089    }
090    
091    @Override
092    public EditItemWeightResult getResult() {
093        return ItemResultFactory.getEditItemWeightResult();
094    }
095
096    @Override
097    public ItemWeightEdit getEdit() {
098        return ItemEditFactory.getItemWeightEdit();
099    }
100
101    UnitOfMeasureKind volumeUnitOfMeasureKind;
102
103    @Override
104    public ItemWeight getEntity(EditItemWeightResult result) {
105        ItemWeight itemWeight = null;
106        var itemName = spec.getItemName();
107        var item = itemControl.getItemByName(itemName);
108
109        if(item != null) {
110            var unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName();
111            var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(item.getLastDetail().getUnitOfMeasureKind(), unitOfMeasureTypeName);
112
113            if(unitOfMeasureType != null) {
114                var itemWeightType = itemWeightTypeLogic.getItemWeightTypeByName(this, spec.getItemWeightTypeName());
115
116                if(!hasExecutionErrors()) {
117                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
118                        itemWeight = itemControl.getItemWeight(item, unitOfMeasureType, itemWeightType);
119                    } else { // EditMode.UPDATE
120                        itemWeight = itemControl.getItemWeightForUpdate(item, unitOfMeasureType, itemWeightType);
121                    }
122
123                    if(itemWeight == null) {
124                        addExecutionError(ExecutionErrors.UnknownItemWeight.name(), item.getLastDetail().getItemName(),
125                                unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(),
126                                itemWeightType.getLastDetail().getItemWeightTypeName());
127                    }
128                }
129            } else {
130                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
131            }
132        } else {
133            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
134        }
135
136        if(!hasExecutionErrors() && (editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.UPDATE))) {
137            volumeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_WEIGHT);
138
139            if(volumeUnitOfMeasureKind == null) {
140                addExecutionError(ExecutionErrors.UnknownWeightUnitOfMeasureKind.name());
141            }
142        }
143
144        return itemWeight;
145    }
146
147    @Override
148    public Item getLockEntity(ItemWeight itemWeight) {
149        return itemWeight.getItem();
150    }
151
152    @Override
153    public void fillInResult(EditItemWeightResult result, ItemWeight itemWeight) {
154        result.setItemWeight(itemControl.getItemWeightTransfer(getUserVisit(), itemWeight));
155    }
156
157    @Override
158    public void doLock(ItemWeightEdit edit, ItemWeight itemWeight) {
159        weight = itemWeight.getWeight();
160        var weightConversion = weight == null ? null : new Conversion(uomControl, volumeUnitOfMeasureKind, weight).convertToHighestUnitOfMeasureType();
161
162        edit.setWeight(weightConversion.getQuantity().toString());
163        edit.setWeightUnitOfMeasureTypeName(weightConversion.getUnitOfMeasureType().getLastDetail().getUnitOfMeasureTypeName());
164    }
165
166    UnitOfMeasureType weightUnitOfMeasureType;
167    Long weight;
168
169    @Override
170    public void canUpdate(ItemWeight itemWeight) {
171        var weightUnitOfMeasureTypeName = edit.getWeightUnitOfMeasureTypeName();
172
173        weightUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(volumeUnitOfMeasureKind, weightUnitOfMeasureTypeName);
174
175        if(weightUnitOfMeasureType != null) {
176            weight = Long.valueOf(edit.getWeight());
177
178            if(weight < 1) {
179                addExecutionError(ExecutionErrors.InvalidWeight.name(), weight);
180            }
181        } else {
182            addExecutionError(ExecutionErrors.UnknownWeightUnitOfMeasureTypeName.name(), weightUnitOfMeasureTypeName);
183        }
184    }
185
186    @Override
187    public void doUpdate(ItemWeight itemWeight) {
188        var itemWeightValue = itemControl.getItemWeightValue(itemWeight);
189
190        var weightConversion = new Conversion(uomControl, weightUnitOfMeasureType, weight).convertToLowestUnitOfMeasureType();
191
192        itemWeightValue.setWeight(weightConversion.getQuantity());
193
194        itemControl.updateItemWeightFromValue(itemWeightValue, getPartyPK());
195    }
196    
197}