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.inventory.server.command;
018
019import com.echothree.control.user.inventory.common.edit.InventoryEditFactory;
020import com.echothree.control.user.inventory.common.edit.InventoryLocationGroupCapacityEdit;
021import com.echothree.control.user.inventory.common.form.EditInventoryLocationGroupCapacityForm;
022import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
023import com.echothree.control.user.inventory.common.spec.InventoryLocationGroupCapacitySpec;
024import com.echothree.model.control.inventory.server.control.InventoryControl;
025import com.echothree.model.control.uom.server.control.UomControl;
026import com.echothree.model.control.warehouse.server.control.WarehouseControl;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.command.EditMode;
033import com.echothree.util.server.control.BaseEditCommand;
034import com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038import javax.enterprise.context.RequestScoped;
039
040@RequestScoped
041public class EditInventoryLocationGroupCapacityCommand
042        extends BaseEditCommand<InventoryLocationGroupCapacitySpec, InventoryLocationGroupCapacityEdit> {
043    
044    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
045    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
046    
047    static {
048        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
049                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null),
051                new FieldDefinition("UnitOfMeasureKindName", FieldType.ENTITY_NAME, true, null, null),
052                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null)
053                ));
054        
055        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
056                new FieldDefinition("Capacity", FieldType.UNSIGNED_LONG, true, null, null)
057                ));
058    }
059    
060    /** Creates a new instance of EditInventoryLocationGroupCapacityCommand */
061    public EditInventoryLocationGroupCapacityCommand() {
062        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
063    }
064    
065    @Override
066    protected BaseResult execute() {
067        var warehouseControl = Session.getModelController(WarehouseControl.class);
068        var result = InventoryResultFactory.getEditInventoryLocationGroupCapacityResult();
069        var warehouseName = spec.getWarehouseName();
070        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
071        
072        if(warehouse != null) {
073            var inventoryControl = Session.getModelController(InventoryControl.class);
074            var inventoryLocationGroupName = spec.getInventoryLocationGroupName();
075            var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouse.getParty(), inventoryLocationGroupName);
076            
077            if(inventoryLocationGroup != null) {
078                var uomControl = Session.getModelController(UomControl.class);
079                var unitOfMeasureKindName = spec.getUnitOfMeasureKindName();
080                var unitOfMeasureKind = uomControl.getUnitOfMeasureKindByName(unitOfMeasureKindName);
081                
082                if(unitOfMeasureKind != null) {
083                    var unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName();
084                    var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
085                    
086                    if(unitOfMeasureType != null) {
087                        if(editMode.equals(EditMode.LOCK)) {
088                            var inventoryLocationGroupCapacity = inventoryControl.getInventoryLocationGroupCapacity(inventoryLocationGroup, unitOfMeasureType);
089                            
090                            if(inventoryLocationGroupCapacity != null) {
091                                result.setInventoryLocationGroupCapacity(inventoryControl.getInventoryLocationGroupCapacityTransfer(getUserVisit(), inventoryLocationGroupCapacity));
092                                
093                                if(lockEntity(inventoryLocationGroup)) {
094                                    var edit = InventoryEditFactory.getInventoryLocationGroupCapacityEdit();
095                                    
096                                    result.setEdit(edit);
097                                    edit.setCapacity(inventoryLocationGroupCapacity.getCapacity().toString());
098                                } else {
099                                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
100                                }
101                                
102                                result.setEntityLock(getEntityLockTransfer(inventoryLocationGroup));
103                            } else {
104                                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupCapacity.name());
105                            }
106                        } else if(editMode.equals(EditMode.UPDATE)) {
107                            var inventoryLocationGroupCapacityValue = inventoryControl.getInventoryLocationGroupCapacityValueForUpdate(inventoryLocationGroup, unitOfMeasureType);
108                            
109                            if(inventoryLocationGroupCapacityValue != null) {
110                                if(lockEntityForUpdate(inventoryLocationGroup)) {
111                                    try {
112                                        inventoryLocationGroupCapacityValue.setCapacity(Long.valueOf(edit.getCapacity()));
113                                        
114                                        inventoryControl.updateInventoryLocationGroupCapacityFromValue(inventoryLocationGroupCapacityValue, getPartyPK());
115                                    } finally {
116                                        unlockEntity(inventoryLocationGroup);
117                                    }
118                                } else {
119                                    addExecutionError(ExecutionErrors.EntityLockStale.name());
120                                }
121                            } else {
122                                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupCapacity.name());
123                            }
124                        }
125                    } else {
126                        addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
127                    }
128                } else {
129                    addExecutionError(ExecutionErrors.UnknownUnitOfMeasureKindName.name(), unitOfMeasureKindName);
130                }
131            } else {
132                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName);
133            }
134        } else {
135            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
136        }
137        
138        return result;
139    }
140    
141}