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