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.contact.server.command; 018 019import com.echothree.control.user.contact.common.form.CreatePartyContactMechanismAliasForm; 020import com.echothree.model.control.contact.server.control.ContactControl; 021import com.echothree.model.control.party.server.control.PartyControl; 022import com.echothree.model.data.user.common.pk.UserVisitPK; 023import com.echothree.util.common.message.ExecutionErrors; 024import com.echothree.util.common.validation.FieldDefinition; 025import com.echothree.util.common.validation.FieldType; 026import com.echothree.util.common.command.BaseResult; 027import com.echothree.util.server.control.BaseSimpleCommand; 028import com.echothree.util.server.persistence.Session; 029import java.util.Arrays; 030import java.util.Collections; 031import java.util.List; 032import java.util.regex.Pattern; 033import javax.enterprise.context.RequestScoped; 034 035@RequestScoped 036public class CreatePartyContactMechanismAliasCommand 037 extends BaseSimpleCommand<CreatePartyContactMechanismAliasForm> { 038 039 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 040 041 static { 042 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 043 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null), 044 new FieldDefinition("ContactMechanismName", FieldType.ENTITY_NAME, true, null, null), 045 new FieldDefinition("ContactMechanismAliasTypeName", FieldType.ENTITY_NAME, true, null, null), 046 new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null) 047 )); 048 } 049 050 /** Creates a new instance of CreatePartyContactMechanismAliasCommand */ 051 public CreatePartyContactMechanismAliasCommand() { 052 super(null, FORM_FIELD_DEFINITIONS, false); 053 } 054 055 @Override 056 protected BaseResult execute() { 057 var partyControl = Session.getModelController(PartyControl.class); 058 var partyName = form.getPartyName(); 059 var party = partyControl.getPartyByName(partyName); 060 061 if(party != null) { 062 var contactControl = Session.getModelController(ContactControl.class); 063 var contactMechanismName = form.getContactMechanismName(); 064 var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName); 065 066 if(contactMechanism != null) { 067 var contactMechanismAliasTypeName = form.getContactMechanismAliasTypeName(); 068 var contactMechanismAliasType = contactControl.getContactMechanismAliasTypeByName(contactMechanismAliasTypeName); 069 070 if(contactMechanismAliasType != null) { 071 var contactMechanismAliasTypeDetail = contactMechanismAliasType.getLastDetail(); 072 var validationPattern = contactMechanismAliasTypeDetail.getValidationPattern(); 073 var alias = form.getAlias(); 074 075 if(validationPattern != null) { 076 var pattern = Pattern.compile(validationPattern); 077 var m = pattern.matcher(alias); 078 079 if(!m.matches()) { 080 addExecutionError(ExecutionErrors.InvalidAlias.name(), alias); 081 } 082 } 083 084 if(!hasExecutionErrors()) { 085 var partyContactMechanismAlias = contactControl.getPartyContactMechanismAliasByAlias(party, 086 contactMechanismAliasType, alias); 087 088 if(partyContactMechanismAlias == null) { 089 contactControl.createPartyContactMechanismAlias(party, contactMechanism, contactMechanismAliasType, alias, getPartyPK()); 090 } else { 091 addExecutionError(ExecutionErrors.DuplicatePartyContactMechanismAlias.name(), partyName, contactMechanismName, 092 contactMechanismAliasTypeName, alias); 093 } 094 } 095 } else { 096 addExecutionError(ExecutionErrors.UnknownContactMechanismAliasTypeName.name(), contactMechanismAliasTypeName); 097 } 098 } else { 099 addExecutionError(ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName); 100 } 101 } else { 102 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 103 } 104 105 return null; 106 } 107 108}