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.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.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.server.control.WarehouseControl;
028import com.echothree.model.control.workflow.server.control.WorkflowControl;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.List;
039import javax.inject.Inject;
040import javax.enterprise.context.Dependent;
041
042@Dependent
043public class CreateInventoryLocationGroupCommand
044        extends BaseSimpleCommand<CreateInventoryLocationGroupForm> {
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.InventoryLocationGroup.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("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
061                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
062                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
063        );
064    }
065
066    @Inject
067    EntityInstanceControl entityInstanceControl;
068
069    @Inject
070    InventoryLocationGroupControl inventoryLocationGroupControl;
071
072    @Inject
073    WarehouseControl warehouseControl;
074
075    @Inject
076    WorkflowControl workflowControl;
077
078    /** Creates a new instance of CreateInventoryLocationGroupCommand */
079    public CreateInventoryLocationGroupCommand() {
080        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
081    }
082
083    @Override
084    protected BaseResult execute() {
085        var result = InventoryResultFactory.getCreateInventoryLocationGroupResult();
086        var warehouseName = form.getWarehouseName();
087        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
088
089        if(warehouse != null) {
090            var warehouseParty = warehouse.getParty();
091            var inventoryLocationGroupName = form.getInventoryLocationGroupName();
092            var inventoryLocationGroup = inventoryLocationGroupControl.getInventoryLocationGroupByName(warehouseParty,
093                    inventoryLocationGroupName);
094
095            if(inventoryLocationGroup == null) {
096                var createdBy = getPartyPK();
097                var isDefault = Boolean.valueOf(form.getIsDefault());
098                var sortOrder = Integer.valueOf(form.getSortOrder());
099                var description = form.getDescription();
100
101                inventoryLocationGroup = inventoryLocationGroupControl.createInventoryLocationGroup(warehouseParty, inventoryLocationGroupName,
102                        isDefault, sortOrder, getPartyPK());
103
104                var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(inventoryLocationGroup.getPrimaryKey());
105                workflowControl.addEntityToWorkflowUsingNames(null, InventoryLocationGroupStatusConstants.Workflow_INVENTORY_LOCATION_GROUP_STATUS,
106                        InventoryLocationGroupStatusConstants.WorkflowEntrance_NEW_INVENTORY_LOCATION_GROUP, entityInstance, null, null, createdBy);
107
108                if(description != null) {
109                    inventoryLocationGroupControl.createInventoryLocationGroupDescription(inventoryLocationGroup, getPreferredLanguage(),
110                            description, createdBy);
111                }
112            } else {
113                addExecutionError(ExecutionErrors.DuplicateInventoryLocationGroupName.name(), inventoryLocationGroupName);
114            }
115
116            if(inventoryLocationGroup != null) {
117                result.setEntityRef(inventoryLocationGroup.getPrimaryKey().getEntityRef());
118                result.setWarehouseName(warehouse.getWarehouseName());
119                result.setInventoryLocationGroupName(inventoryLocationGroup.getLastDetail().getInventoryLocationGroupName());
120            }
121        } else {
122            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
123        }
124
125        return result;
126    }
127
128}