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.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.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 com.echothree.util.server.persistence.Session; 039import java.util.List; 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 /** Creates a new instance of CreateInventoryLocationGroupCommand */ 067 public CreateInventoryLocationGroupCommand() { 068 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 069 } 070 071 @Override 072 protected BaseResult execute() { 073 var result = InventoryResultFactory.getCreateInventoryLocationGroupResult(); 074 var warehouseControl = Session.getModelController(WarehouseControl.class); 075 var warehouseName = form.getWarehouseName(); 076 var warehouse = warehouseControl.getWarehouseByName(warehouseName); 077 078 if(warehouse != null) { 079 var inventoryControl = Session.getModelController(InventoryControl.class); 080 var warehouseParty = warehouse.getParty(); 081 var inventoryLocationGroupName = form.getInventoryLocationGroupName(); 082 var inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouseParty, 083 inventoryLocationGroupName); 084 085 if(inventoryLocationGroup == null) { 086 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 087 var workflowControl = Session.getModelController(WorkflowControl.class); 088 var createdBy = getPartyPK(); 089 var isDefault = Boolean.valueOf(form.getIsDefault()); 090 var sortOrder = Integer.valueOf(form.getSortOrder()); 091 var description = form.getDescription(); 092 093 inventoryLocationGroup = inventoryControl.createInventoryLocationGroup(warehouseParty, inventoryLocationGroupName, 094 isDefault, sortOrder, getPartyPK()); 095 096 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(inventoryLocationGroup.getPrimaryKey()); 097 workflowControl.addEntityToWorkflowUsingNames(null, InventoryLocationGroupStatusConstants.Workflow_INVENTORY_LOCATION_GROUP_STATUS, 098 InventoryLocationGroupStatusConstants.WorkflowEntrance_NEW_INVENTORY_LOCATION_GROUP, entityInstance, null, null, createdBy); 099 100 if(description != null) { 101 inventoryControl.createInventoryLocationGroupDescription(inventoryLocationGroup, getPreferredLanguage(), 102 description, createdBy); 103 } 104 } else { 105 addExecutionError(ExecutionErrors.DuplicateInventoryLocationGroupName.name(), inventoryLocationGroupName); 106 } 107 108 if(inventoryLocationGroup != null) { 109 result.setEntityRef(inventoryLocationGroup.getPrimaryKey().getEntityRef()); 110 result.setWarehouseName(warehouse.getWarehouseName()); 111 result.setInventoryLocationGroupName(inventoryLocationGroup.getLastDetail().getInventoryLocationGroupName()); 112 } 113 } else { 114 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 115 } 116 117 return result; 118 } 119 120}