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