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.warehouse.server.command;
018
019import com.echothree.control.user.warehouse.common.edit.LocationCapacityEdit;
020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory;
021import com.echothree.control.user.warehouse.common.form.EditLocationCapacityForm;
022import com.echothree.control.user.warehouse.common.result.EditLocationCapacityResult;
023import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory;
024import com.echothree.control.user.warehouse.common.spec.LocationCapacitySpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.uom.server.control.UomControl;
029import com.echothree.model.control.warehouse.server.control.WarehouseControl;
030import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind;
031import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.model.data.warehouse.server.entity.Location;
034import com.echothree.model.data.warehouse.server.entity.LocationCapacity;
035import com.echothree.model.data.warehouse.server.entity.Warehouse;
036import com.echothree.model.data.warehouse.server.value.LocationCapacityValue;
037import com.echothree.util.common.command.BaseResult;
038import com.echothree.util.common.command.EditMode;
039import com.echothree.util.common.message.ExecutionErrors;
040import com.echothree.util.common.validation.FieldDefinition;
041import com.echothree.util.common.validation.FieldType;
042import com.echothree.util.server.control.BaseEditCommand;
043import com.echothree.util.server.control.CommandSecurityDefinition;
044import com.echothree.util.server.control.PartyTypeDefinition;
045import com.echothree.util.server.control.SecurityRoleDefinition;
046import com.echothree.util.server.persistence.Session;
047import java.util.Arrays;
048import java.util.Collections;
049import java.util.List;
050
051public class EditLocationCapacityCommand
052        extends BaseEditCommand<LocationCapacitySpec, LocationCapacityEdit> {
053
054    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
055    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
056    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
057    
058    static {
059        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
060                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
061                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
062                        new SecurityRoleDefinition(SecurityRoleGroups.LocationCapacity.name(), SecurityRoles.Edit.name())
063                ))
064        ));
065
066        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
067                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("LocationName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("UnitOfMeasureKindName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null)
071                ));
072        
073        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
074                new FieldDefinition("Capacity", FieldType.UNSIGNED_LONG, true, null, null)
075                ));
076    }
077    
078    /** Creates a new instance of EditLocationCapacityCommand */
079    public EditLocationCapacityCommand(UserVisitPK userVisitPK, EditLocationCapacityForm form) {
080        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082    
083    @Override
084    protected BaseResult execute() {
085        var warehouseControl = Session.getModelController(WarehouseControl.class);
086        EditLocationCapacityResult result = WarehouseResultFactory.getEditLocationCapacityResult();
087        String warehouseName = spec.getWarehouseName();
088        Warehouse warehouse = warehouseControl.getWarehouseByName(warehouseName);
089        
090        if(warehouse != null) {
091            String locationName = spec.getLocationName();
092            Location location = warehouseControl.getLocationByName(warehouse.getParty(), locationName);
093            
094            if(location != null) {
095                var uomControl = Session.getModelController(UomControl.class);
096                String unitOfMeasureKindName = spec.getUnitOfMeasureKindName();
097                UnitOfMeasureKind unitOfMeasureKind = uomControl.getUnitOfMeasureKindByName(unitOfMeasureKindName);
098                
099                if(unitOfMeasureKind != null) {
100                    String unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName();
101                    UnitOfMeasureType unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
102                    
103                    if(unitOfMeasureType != null) {
104                        if(editMode.equals(EditMode.LOCK)) {
105                            LocationCapacity locationCapacity = warehouseControl.getLocationCapacity(location, unitOfMeasureType);
106                            
107                            if(locationCapacity != null) {
108                                result.setLocationCapacity(warehouseControl.getLocationCapacityTransfer(getUserVisit(), locationCapacity));
109                                
110                                if(lockEntity(location)) {
111                                    LocationCapacityEdit edit = WarehouseEditFactory.getLocationCapacityEdit();
112                                    
113                                    result.setEdit(edit);
114                                    edit.setCapacity(locationCapacity.getCapacity().toString());
115                                } else {
116                                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
117                                }
118                                
119                                result.setEntityLock(getEntityLockTransfer(location));
120                            } else {
121                                addExecutionError(ExecutionErrors.UnknownLocationCapacity.name());
122                            }
123                        } else if(editMode.equals(EditMode.UPDATE)) {
124                            LocationCapacityValue locationCapacityValue = warehouseControl.getLocationCapacityValueForUpdate(location, unitOfMeasureType);
125                            
126                            if(locationCapacityValue != null) {
127                                if(lockEntityForUpdate(location)) {
128                                    try {
129                                        locationCapacityValue.setCapacity(Long.valueOf(edit.getCapacity()));
130                                        
131                                        warehouseControl.updateLocationCapacityFromValue(locationCapacityValue, getPartyPK());
132                                    } finally {
133                                        unlockEntity(location);
134                                    }
135                                } else {
136                                    addExecutionError(ExecutionErrors.EntityLockStale.name());
137                                }
138                            } else {
139                                addExecutionError(ExecutionErrors.UnknownLocationCapacity.name());
140                            }
141                        }
142                    } else {
143                        addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
144                    }
145                } else {
146                    addExecutionError(ExecutionErrors.UnknownUnitOfMeasureKindName.name(), unitOfMeasureKindName);
147                }
148            } else {
149                addExecutionError(ExecutionErrors.UnknownLocationName.name(), locationName);
150            }
151        } else {
152            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
153        }
154        
155        return result;
156    }
157    
158}