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.shipment.server.command; 018 019import com.echothree.control.user.shipment.common.form.CreateShipmentAliasForm; 020import com.echothree.control.user.shipment.server.command.util.ShipmentAliasUtil; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.security.common.SecurityRoles; 023import com.echothree.model.control.shipment.server.control.ShipmentControl; 024import com.echothree.util.common.command.BaseResult; 025import com.echothree.util.common.message.ExecutionErrors; 026import com.echothree.util.common.validation.FieldDefinition; 027import com.echothree.util.common.validation.FieldType; 028import com.echothree.util.server.control.BaseSimpleCommand; 029import com.echothree.util.server.control.CommandSecurityDefinition; 030import com.echothree.util.server.control.PartyTypeDefinition; 031import com.echothree.util.server.control.SecurityRoleDefinition; 032import com.echothree.util.server.persistence.Session; 033import java.util.Arrays; 034import java.util.Collections; 035import java.util.List; 036import java.util.regex.Pattern; 037import javax.enterprise.context.RequestScoped; 038 039@RequestScoped 040public class CreateShipmentAliasCommand 041 extends BaseSimpleCommand<CreateShipmentAliasForm> { 042 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 047 new FieldDefinition("ShipmentTypeName", FieldType.ENTITY_NAME, true, null, null), 048 new FieldDefinition("ShipmentName", FieldType.ENTITY_NAME, true, null, null), 049 new FieldDefinition("ShipmentAliasTypeName", FieldType.ENTITY_NAME, true, null, null), 050 new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null) 051 )); 052 } 053 054 /** Creates a new instance of CreateShipmentAliasCommand */ 055 public CreateShipmentAliasCommand() { 056 super(null, FORM_FIELD_DEFINITIONS, false); 057 } 058 059 @Override 060 protected CommandSecurityDefinition getCommandSecurityDefinition() { 061 return new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 062 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 063 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 064 new SecurityRoleDefinition(ShipmentAliasUtil.getInstance().getSecurityRoleGroupNameByShipmentTypeSpec(form), SecurityRoles.Create.name()) 065 ))) 066 ))); 067 } 068 069 @Override 070 protected BaseResult execute() { 071 var shipmentControl = Session.getModelController(ShipmentControl.class); 072 var shipmentTypeName = form.getShipmentTypeName(); 073 var shipmentType = shipmentControl.getShipmentTypeByName(shipmentTypeName); 074 075 if(shipmentType != null) { 076 var shipmentName = form.getShipmentName(); 077 var shipment = shipmentControl.getShipmentByName(shipmentType, shipmentName); 078 079 if(shipment != null) { 080 var shipmentAliasTypeName = form.getShipmentAliasTypeName(); 081 var shipmentAliasType = shipmentControl.getShipmentAliasTypeByName(shipmentType, shipmentAliasTypeName); 082 083 if(shipmentAliasType != null) { 084 var shipmentAliasTypeDetail = shipmentAliasType.getLastDetail(); 085 var validationPattern = shipmentAliasTypeDetail.getValidationPattern(); 086 var alias = form.getAlias(); 087 088 if(validationPattern != null) { 089 var pattern = Pattern.compile(validationPattern); 090 var m = pattern.matcher(alias); 091 092 if(!m.matches()) { 093 addExecutionError(ExecutionErrors.InvalidAlias.name(), alias); 094 } 095 } 096 097 if(!hasExecutionErrors()) { 098 if(shipmentControl.getShipmentAlias(shipment, shipmentAliasType) == null && shipmentControl.getShipmentAliasByAlias(shipmentAliasType, alias) == null) { 099 shipmentControl.createShipmentAlias(shipment, shipmentAliasType, alias, getPartyPK()); 100 } else { 101 addExecutionError(ExecutionErrors.DuplicateShipmentAlias.name(), shipmentTypeName, shipmentName, shipmentAliasTypeName, alias); 102 } 103 } 104 } else { 105 addExecutionError(ExecutionErrors.UnknownShipmentAliasTypeName.name(), shipmentTypeName, shipmentAliasTypeName); 106 } 107 } else { 108 addExecutionError(ExecutionErrors.UnknownShipmentName.name(), shipmentTypeName, shipmentName); 109 } 110 } else { 111 addExecutionError(ExecutionErrors.UnknownShipmentTypeName.name(), shipmentTypeName); 112 } 113 114 return null; 115 } 116 117}