001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.InventoryControl;
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 com.echothree.util.server.persistence.Session;
042import java.util.List;
043import javax.enterprise.context.RequestScoped;
044
045@RequestScoped
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    /** Creates a new instance of CreateLocationCommand */
072    public CreateLocationCommand() {
073        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
074    }
075    
076    @Override
077    protected BaseResult execute() {
078        var result = WarehouseResultFactory.getCreateLocationResult();
079        var warehouseControl = Session.getModelController(WarehouseControl.class);
080        var warehouseName = form.getWarehouseName();
081        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
082        
083        if(warehouse != null) {
084            var warehouseParty = warehouse.getParty();
085            var locationName = form.getLocationName();
086            var location = warehouseControl.getLocationByName(warehouseParty, locationName);
087            
088            if(location == null) {
089                var locationTypeName = form.getLocationTypeName();
090                var locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName);
091                
092                if(locationType != null) {
093                    LocationLogic.getInstance().validateLocationName(this, locationType, locationName);
094
095                    if(!hasExecutionErrors()) {
096                        var locationUseTypeName = form.getLocationUseTypeName();
097                        var locationUseType = LocationUseTypeLogic.getInstance().getLocationUseTypeByName(this, locationUseTypeName, null, false);
098                        
099                        if(!hasExecutionErrors()) {
100                            var multipleUseError = false;
101                            
102                            if(!locationUseType.getAllowMultiple()) {
103                                if(warehouseControl.countLocationsByLocationUseType(warehouseParty, locationUseType) != 0)
104                                    multipleUseError = true;
105                            }
106                            
107                            if(!multipleUseError) {
108                                var inventoryControl = Session.getModelController(InventoryControl.class);
109                                var inventoryLocationGroupName = form.getInventoryLocationGroupName();
110                                var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty, inventoryLocationGroupName);
111                                
112                                if(inventoryLocationGroup != null) {
113                                    var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
114                                    var velocity = Integer.valueOf(form.getVelocity());
115                                    var workflowControl = Session.getModelController(WorkflowControl.class);
116                                    var createdBy = getPartyPK();
117                                    var description = form.getDescription();
118                                    
119                                    location = warehouseControl.createLocation(warehouseParty, locationName, locationType, locationUseType,
120                                            velocity, inventoryLocationGroup, createdBy);
121
122                                    var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(inventoryLocationGroup.getPrimaryKey());
123                                    var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceUsingNames(InventoryLocationGroupStatusConstants.Workflow_INVENTORY_LOCATION_GROUP_STATUS, entityInstance);
124                                    var workflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
125                                    var workflowEntranceName = switch(workflowStepName) {
126                                        case InventoryLocationGroupStatusConstants.WorkflowStep_ACTIVE ->
127                                                LocationStatusConstants.WorkflowEntrance_NEW_LOCATION_ACTIVE;
128                                        case InventoryLocationGroupStatusConstants.WorkflowStep_INVENTORY_PREP ->
129                                                LocationStatusConstants.WorkflowEntrance_NEW_LOCATION_INVENTORY_PREP;
130                                        case InventoryLocationGroupStatusConstants.WorkflowStep_INVENTORY ->
131                                                LocationStatusConstants.WorkflowEntrance_NEW_LOCATION_INVENTORY;
132                                        default -> null;
133                                    };
134
135                                    entityInstance = entityInstanceControl.getEntityInstanceByBasePK(location.getPrimaryKey());
136                                    workflowControl.addEntityToWorkflowUsingNames(null, LocationStatusConstants.Workflow_LOCATION_STATUS, workflowEntranceName, entityInstance, null, null, createdBy);
137                                    
138                                    if(description != null) {
139                                        warehouseControl.createLocationDescription(location, getPreferredLanguage(), description, createdBy);
140                                    }
141                                } else {
142                                    addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), inventoryLocationGroupName);
143                                }
144                            } else {
145                                addExecutionError(ExecutionErrors.MultipleLocationUseTypesNotAllowed.name());
146                            }
147                        }
148                    }
149                } else {
150                    addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), locationTypeName);
151                }
152            } else {
153                addExecutionError(ExecutionErrors.DuplicateLocationName.name(), locationName);
154            }
155            
156            if(location != null) {
157                result.setEntityRef(location.getPrimaryKey().getEntityRef());
158                result.setWarehouseName(warehouse.getWarehouseName());
159                result.setLocationName(location.getLastDetail().getLocationName());
160            }
161        } else {
162            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
163        }
164        
165        return result;
166    }
167
168}