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