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