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.warehouse.server.command; 018 019import com.echothree.control.user.warehouse.common.form.CreateLocationNameElementForm; 020import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.security.common.SecurityRoleGroups; 023import com.echothree.model.control.security.common.SecurityRoles; 024import com.echothree.model.control.warehouse.server.control.WarehouseControl; 025import com.echothree.model.data.user.common.pk.UserVisitPK; 026import com.echothree.util.common.command.BaseResult; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.persistence.BasePK; 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.control.CommandSecurityDefinition; 033import com.echothree.util.server.control.PartyTypeDefinition; 034import com.echothree.util.server.control.SecurityRoleDefinition; 035import com.echothree.util.server.persistence.Session; 036import java.util.Arrays; 037import java.util.Collections; 038import java.util.List; 039import javax.enterprise.context.RequestScoped; 040 041@RequestScoped 042public class CreateLocationNameElementCommand 043 extends BaseSimpleCommand<CreateLocationNameElementForm> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 047 048 static { 049 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 050 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 051 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 052 new SecurityRoleDefinition(SecurityRoleGroups.LocationNameElement.name(), SecurityRoles.Create.name()) 053 )) 054 )); 055 056 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 057 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null), 058 new FieldDefinition("LocationTypeName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("LocationNameElementName", FieldType.ENTITY_NAME, true, null, null), 060 new FieldDefinition("Offset", FieldType.UNSIGNED_INTEGER, true, null, null), 061 new FieldDefinition("Length", FieldType.UNSIGNED_INTEGER, true, null, null), 062 new FieldDefinition("ValidationPattern", FieldType.REGULAR_EXPRESSION, false, null, null), 063 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 064 )); 065 } 066 067 /** Creates a new instance of CreateLocationNameElementCommand */ 068 public CreateLocationNameElementCommand() { 069 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 070 } 071 072 @Override 073 protected BaseResult execute() { 074 var result = WarehouseResultFactory.getCreateLocationNameElementResult(); 075 var warehouseControl = Session.getModelController(WarehouseControl.class); 076 var warehouseName = form.getWarehouseName(); 077 var warehouse = warehouseControl.getWarehouseByName(warehouseName); 078 079 if(warehouse != null) { 080 var warehouseParty = warehouse.getParty(); 081 var locationTypeName = form.getLocationTypeName(); 082 var locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName); 083 084 if(locationType != null) { 085 var locationNameElementName = form.getLocationNameElementName(); 086 var locationNameElement = warehouseControl.getLocationNameElementByName(locationType, 087 locationNameElementName); 088 089 if(locationNameElement == null) { 090 BasePK createdBy = getPartyPK(); 091 var offset = Integer.valueOf(form.getOffset()); 092 var length = Integer.valueOf(form.getLength()); 093 var validationPattern = form.getValidationPattern(); 094 var description = form.getDescription(); 095 096 locationNameElement = warehouseControl.createLocationNameElement(locationType, locationNameElementName, offset, 097 length, validationPattern, createdBy); 098 099 if(description != null) { 100 warehouseControl.createLocationNameElementDescription(locationNameElement, getPreferredLanguage(), 101 description, createdBy); 102 } 103 } else { 104 addExecutionError(ExecutionErrors.DuplicateLocationNameElementName.name(), locationNameElementName); 105 } 106 107 if(locationNameElement != null) { 108 result.setEntityRef(locationNameElement.getPrimaryKey().getEntityRef()); 109 result.setWarehouseName(warehouse.getWarehouseName()); 110 result.setLocationTypeName(locationType.getLastDetail().getLocationTypeName()); 111 result.setLocationNameElementName(locationNameElement.getLastDetail().getLocationNameElementName()); 112 } 113 } else { 114 addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), locationTypeName); 115 } 116 } else { 117 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 118 } 119 120 return result; 121 } 122 123}