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.inventory.server.command;
018
019import com.echothree.control.user.inventory.common.form.CreateInventoryLocationGroupForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.common.workflow.InventoryLocationGroupStatusConstants;
022import com.echothree.model.control.inventory.server.control.InventoryControl;
023import com.echothree.model.control.warehouse.server.control.WarehouseControl;
024import com.echothree.model.control.workflow.server.control.WorkflowControl;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.model.data.warehouse.server.entity.Warehouse;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BaseSimpleCommand;
032import com.echothree.util.server.persistence.Session;
033import java.util.Arrays;
034import java.util.Collections;
035import java.util.List;
036
037public class CreateInventoryLocationGroupCommand
038        extends BaseSimpleCommand<CreateInventoryLocationGroupForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
044        new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
045        new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null),
046        new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
047        new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
048        new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
049        ));
050    }
051    
052    /** Creates a new instance of CreateInventoryLocationGroupCommand */
053    public CreateInventoryLocationGroupCommand(UserVisitPK userVisitPK, CreateInventoryLocationGroupForm form) {
054        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
055    }
056    
057    @Override
058    protected BaseResult execute() {
059        var result = InventoryResultFactory.getCreateInventoryLocationGroupResult();
060        var warehouseControl = Session.getModelController(WarehouseControl.class);
061        String warehouseName = form.getWarehouseName();
062        Warehouse warehouse = warehouseControl.getWarehouseByName(warehouseName);
063        
064        if(warehouse != null) {
065            var inventoryControl = Session.getModelController(InventoryControl.class);
066            var warehouseParty = warehouse.getParty();
067            var inventoryLocationGroupName = form.getInventoryLocationGroupName();
068            var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty,
069                    inventoryLocationGroupName);
070            
071            if(inventoryLocationGroup == null) {
072                var coreControl = getCoreControl();
073                var workflowControl = Session.getModelController(WorkflowControl.class);
074                var createdBy = getPartyPK();
075                var isDefault = Boolean.valueOf(form.getIsDefault());
076                var sortOrder = Integer.valueOf(form.getSortOrder());
077                var description = form.getDescription();
078                
079                inventoryLocationGroup = inventoryControl.createInventoryLocationGroup(warehouseParty, inventoryLocationGroupName,
080                        isDefault, sortOrder, getPartyPK());
081
082                var entityInstance = coreControl.getEntityInstanceByBasePK(inventoryLocationGroup.getPrimaryKey());
083                workflowControl.addEntityToWorkflowUsingNames(null, InventoryLocationGroupStatusConstants.Workflow_INVENTORY_LOCATION_GROUP_STATUS,
084                        InventoryLocationGroupStatusConstants.WorkflowEntrance_NEW_INVENTORY_LOCATION_GROUP, entityInstance, null, null, createdBy);
085                
086                if(description != null) {
087                    inventoryControl.createInventoryLocationGroupDescription(inventoryLocationGroup, getPreferredLanguage(),
088                            description, createdBy);
089                }
090            } else {
091                addExecutionError(ExecutionErrors.DuplicateInventoryLocationGroupName.name(), inventoryLocationGroupName);
092            }
093
094            if(inventoryLocationGroup != null) {
095                result.setEntityRef(inventoryLocationGroup.getPrimaryKey().getEntityRef());
096                result.setWarehouseName(warehouse.getWarehouseName());
097                result.setInventoryLocationGroupName(inventoryLocationGroup.getLastDetail().getInventoryLocationGroupName());
098            }
099        } else {
100            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
101        }
102        
103        return result;
104    }
105    
106}