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