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.form.CreateLocationForm;
020import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory;
021import com.echothree.model.control.core.server.control.EntityInstanceControl;
022import com.echothree.model.control.inventory.common.workflow.InventoryLocationGroupStatusConstants;
023import com.echothree.model.control.inventory.server.control.InventoryLocationGroupControl;
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.warehouse.common.workflow.LocationStatusConstants;
028import com.echothree.model.control.warehouse.server.control.WarehouseControl;
029import com.echothree.model.control.warehouse.server.logic.LocationLogic;
030import com.echothree.model.control.warehouse.server.logic.LocationUseTypeLogic;
031import com.echothree.model.control.workflow.server.control.WorkflowControl;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.server.control.BaseSimpleCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043import javax.inject.Inject;
044
045@Dependent
046public class CreateLocationCommand
047        extends BaseSimpleCommand<CreateLocationForm> {
048
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
051
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.Location.name(), SecurityRoles.Create.name())
057                ))
058        ));
059
060        FORM_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("LocationName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("LocationTypeName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("LocationUseTypeName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("Velocity", FieldType.UNSIGNED_INTEGER, true, null, null),
066                new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
068        );
069    }
070
071    @Inject
072    EntityInstanceControl entityInstanceControl;
073
074    @Inject
075    InventoryLocationGroupControl inventoryLocationGroupControl;
076
077    @Inject
078    WarehouseControl warehouseControl;
079
080    @Inject
081    WorkflowControl workflowControl;
082
083    @Inject
084    LocationLogic locationLogic;
085
086    @Inject
087    LocationUseTypeLogic locationUseTypeLogic;
088
089    
090    /** Creates a new instance of CreateLocationCommand */
091    public CreateLocationCommand() {
092        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
093    }
094    
095    @Override
096    protected BaseResult execute() {
097        var result = WarehouseResultFactory.getCreateLocationResult();
098        var warehouseName = form.getWarehouseName();
099        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
100        
101        if(warehouse != null) {
102            var warehouseParty = warehouse.getParty();
103            var locationName = form.getLocationName();
104            var location = warehouseControl.getLocationByName(warehouseParty, locationName);
105            
106            if(location == null) {
107                var locationTypeName = form.getLocationTypeName();
108                var locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName);
109                
110                if(locationType != null) {
111                    locationLogic.validateLocationName(this, locationType, locationName);
112
113                    if(!hasExecutionErrors()) {
114                        var locationUseTypeName = form.getLocationUseTypeName();
115                        var locationUseType = locationUseTypeLogic.getLocationUseTypeByName(this, locationUseTypeName, null, false);
116                        
117                        if(!hasExecutionErrors()) {
118                            var multipleUseError = false;
119                            
120                            if(!locationUseType.getAllowMultiple()) {
121                                if(warehouseControl.countLocationsByLocationUseType(warehouseParty, locationUseType) != 0)
122                                    multipleUseError = true;
123                            }
124                            
125                            if(!multipleUseError) {
126                                var inventoryLocationGroupName = form.getInventoryLocationGroupName();
127                                var inventoryLocationGroup = inventoryLocationGroupControl.getInventoryLocationGroupByName(warehouseParty, inventoryLocationGroupName);
128                                
129                                if(inventoryLocationGroup != null) {
130                                    var velocity = Integer.valueOf(form.getVelocity());
131                                    var createdBy = getPartyPK();
132                                    var description = form.getDescription();
133                                    
134                                    location = warehouseControl.createLocation(warehouseParty, locationName, locationType, locationUseType,
135                                            velocity, inventoryLocationGroup, createdBy);
136
137                                    var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(inventoryLocationGroup.getPrimaryKey());
138                                    var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceUsingNames(InventoryLocationGroupStatusConstants.Workflow_INVENTORY_LOCATION_GROUP_STATUS, entityInstance);
139                                    var workflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
140                                    var workflowEntranceName = switch(workflowStepName) {
141                                        case InventoryLocationGroupStatusConstants.WorkflowStep_ACTIVE ->
142                                                LocationStatusConstants.WorkflowEntrance_NEW_LOCATION_ACTIVE;
143                                        case InventoryLocationGroupStatusConstants.WorkflowStep_INVENTORY_PREP ->
144                                                LocationStatusConstants.WorkflowEntrance_NEW_LOCATION_INVENTORY_PREP;
145                                        case InventoryLocationGroupStatusConstants.WorkflowStep_INVENTORY ->
146                                                LocationStatusConstants.WorkflowEntrance_NEW_LOCATION_INVENTORY;
147                                        default -> null;
148                                    };
149
150                                    entityInstance = entityInstanceControl.getEntityInstanceByBasePK(location.getPrimaryKey());
151                                    workflowControl.addEntityToWorkflowUsingNames(null, LocationStatusConstants.Workflow_LOCATION_STATUS, workflowEntranceName, entityInstance, null, null, createdBy);
152                                    
153                                    if(description != null) {
154                                        warehouseControl.createLocationDescription(location, getPreferredLanguage(), description, createdBy);
155                                    }
156                                } else {
157                                    addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName);
158                                }
159                            } else {
160                                addExecutionError(ExecutionErrors.MultipleLocationUseTypesNotAllowed.name());
161                            }
162                        }
163                    }
164                } else {
165                    addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), locationTypeName);
166                }
167            } else {
168                addExecutionError(ExecutionErrors.DuplicateLocationName.name(), locationName);
169            }
170            
171            if(location != null) {
172                result.setEntityRef(location.getPrimaryKey().getEntityRef());
173                result.setWarehouseName(warehouse.getWarehouseName());
174                result.setLocationName(location.getLastDetail().getLocationName());
175            }
176        } else {
177            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
178        }
179        
180        return result;
181    }
182
183}