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    /** Creates a new instance of EditLocationCapacityCommand */
071    public EditLocationCapacityCommand() {
072        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
073    }
074
075    @Inject
076    UomControl uomControl;
077
078    @Inject
079    WarehouseControl warehouseControl;
080
081    @Override
082    public EditLocationCapacityResult getResult() {
083        return WarehouseResultFactory.getEditLocationCapacityResult();
084    }
085
086    @Override
087    public LocationCapacityEdit getEdit() {
088        return WarehouseEditFactory.getLocationCapacityEdit();
089    }
090
091    @Override
092    public LocationCapacity getEntity(EditLocationCapacityResult result) {
093        LocationCapacity locationCapacity = null;
094        var warehouseName = spec.getWarehouseName();
095        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
096
097        if(warehouse != null) {
098            var locationName = spec.getLocationName();
099            var location = warehouseControl.getLocationByName(warehouse.getParty(), locationName);
100
101            if(location != null) {
102                var unitOfMeasureKindName = spec.getUnitOfMeasureKindName();
103                var unitOfMeasureKind = uomControl.getUnitOfMeasureKindByName(unitOfMeasureKindName);
104
105                if(unitOfMeasureKind != null) {
106                    var unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName();
107                    var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
108
109                    if(unitOfMeasureType != null) {
110                        locationCapacity = warehouseControl.getLocationCapacity(location, unitOfMeasureType, editModeToEntityPermission(editMode));
111
112                        if(locationCapacity == null) {
113                            addExecutionError(ExecutionErrors.UnknownLocationCapacity.name(),
114                                    warehouse.getWarehouseName(), location.getLastDetail().getLocationName(),
115                                    unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(),
116                                    unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName());
117                        }
118                    } else {
119                        addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(),
120                                unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(), unitOfMeasureTypeName);
121                    }
122                } else {
123                    addExecutionError(ExecutionErrors.UnknownUnitOfMeasureKindName.name(), unitOfMeasureKindName);
124                }
125            } else {
126                addExecutionError(ExecutionErrors.UnknownLocationName.name(), warehouse.getWarehouseName(), locationName);
127            }
128        } else {
129            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
130        }
131
132        return locationCapacity;
133    }
134
135    @Override
136    public Location getLockEntity(LocationCapacity locationCapacity) {
137        return locationCapacity.getLocation();
138    }
139
140    @Override
141    public void fillInResult(EditLocationCapacityResult result, LocationCapacity locationCapacity) {
142        result.setLocationCapacity(warehouseControl.getLocationCapacityTransfer(getUserVisit(), locationCapacity));
143    }
144
145    @Override
146    public void doLock(LocationCapacityEdit edit, LocationCapacity locationCapacity) {
147        edit.setCapacity(locationCapacity.getCapacity().toString());
148    }
149
150    @Override
151    public void doUpdate(LocationCapacity locationCapacity) {
152        var locationCapacityValue = locationCapacity.getLocationCapacityValue().clone();
153
154        locationCapacityValue.setCapacity(Long.valueOf(edit.getCapacity()));
155
156        warehouseControl.updateLocationCapacityFromValue(locationCapacityValue, getPartyPK());
157    }
158    
159}