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.associate.server.command; 018 019import com.echothree.control.user.associate.common.form.CreateAssociatePartyContactMechanismForm; 020import com.echothree.model.control.associate.server.control.AssociateControl; 021import com.echothree.model.control.contact.server.control.ContactControl; 022import com.echothree.model.data.contact.server.entity.ContactMechanism; 023import com.echothree.model.data.user.common.pk.UserVisitPK; 024import com.echothree.util.common.message.ExecutionErrors; 025import com.echothree.util.common.validation.FieldDefinition; 026import com.echothree.util.common.validation.FieldType; 027import com.echothree.util.common.command.BaseResult; 028import com.echothree.util.server.control.BaseSimpleCommand; 029import java.util.List; 030import javax.enterprise.context.Dependent; 031import javax.inject.Inject; 032 033@Dependent 034public class CreateAssociatePartyContactMechanismCommand 035 extends BaseSimpleCommand<CreateAssociatePartyContactMechanismForm> { 036 037 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 038 039 static { 040 FORM_FIELD_DEFINITIONS = List.of( 041 new FieldDefinition("AssociateProgramName", FieldType.ENTITY_NAME, true, null, null), 042 new FieldDefinition("AssociateName", FieldType.ENTITY_NAME, true, null, null), 043 new FieldDefinition("AssociatePartyContactMechanismName", FieldType.ENTITY_NAME, true, null, null), 044 new FieldDefinition("ContactMechanismName", FieldType.ENTITY_NAME, false, null, null), 045 new FieldDefinition("ContactMechanismAliasTypeName", FieldType.ENTITY_NAME, false, null, null), 046 new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null), 047 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 048 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 049 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 050 ); 051 } 052 053 @Inject 054 AssociateControl associateControl; 055 056 @Inject 057 ContactControl contactControl; 058 059 060 /** Creates a new instance of CreateAssociatePartyContactMechanismCommand */ 061 public CreateAssociatePartyContactMechanismCommand() { 062 super(null, FORM_FIELD_DEFINITIONS, false); 063 } 064 065 @Override 066 protected BaseResult execute() { 067 var contactMechanismName = form.getContactMechanismName(); 068 var contactMechanismAliasTypeName = form.getContactMechanismAliasTypeName(); 069 var alias = form.getAlias(); 070 var parameterCount = (contactMechanismName == null ? 0 : 1) + (contactMechanismAliasTypeName == null && alias == null ? 0 : 1); 071 072 if(parameterCount == 1) { 073 var associateProgramName = form.getAssociateProgramName(); 074 var associateProgram = associateControl.getAssociateProgramByName(associateProgramName); 075 076 if(associateProgram != null) { 077 var associateName = form.getAssociateName(); 078 var associate = associateControl.getAssociateByName(associateProgram, associateName); 079 080 if(associate != null) { 081 var associatePartyContactMechanismName = form.getAssociatePartyContactMechanismName(); 082 var associatePartyContactMechanism = associateControl.getAssociatePartyContactMechanismByName(associate, 083 associatePartyContactMechanismName); 084 085 if(associatePartyContactMechanism == null) { 086 ContactMechanism contactMechanism = null; 087 088 if(contactMechanismName != null) { 089 contactMechanism = contactControl.getContactMechanismByName(contactMechanismName); 090 091 if(contactMechanism == null) { 092 addExecutionError(ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName); 093 } 094 } else { 095 var contactMechanismAliasType = contactControl.getContactMechanismAliasTypeByName(contactMechanismAliasTypeName); 096 097 if(contactMechanismAliasType != null) { 098 var contactMechanismAlias = contactControl.getContactMechanismAliasByAlias(contactMechanismAliasType, 099 alias); 100 101 if(contactMechanismAlias != null) { 102 contactMechanism = contactMechanismAlias.getContactMechanism(); 103 } else { 104 addExecutionError(ExecutionErrors.UnknownContactMechanismAlias.name(), alias); 105 } 106 } else { 107 addExecutionError(ExecutionErrors.UnknownContactMechanismAliasTypeName.name(), contactMechanismAliasTypeName); 108 } 109 } 110 111 if(!hasExecutionErrors()) { 112 var partyContactMechanism = contactControl.getPartyContactMechanism(associate.getLastDetail().getParty(), 113 contactMechanism); 114 115 if(partyContactMechanism != null) { 116 var isDefault = Boolean.valueOf(form.getIsDefault()); 117 var sortOrder = Integer.valueOf(form.getSortOrder()); 118 119 associateControl.createAssociatePartyContactMechanism(associate, associatePartyContactMechanismName, 120 partyContactMechanism, isDefault, sortOrder, getPartyPK()); 121 } else { 122 addExecutionError(ExecutionErrors.UnknownPartyContactMechanism.name(), contactMechanismName); 123 } 124 } 125 } else { 126 addExecutionError(ExecutionErrors.DuplicateAssociatePartyContactMechanismName.name(), associatePartyContactMechanismName); 127 } 128 } else { 129 addExecutionError(ExecutionErrors.UnknownAssociateName.name(), associateName); 130 } 131 } else { 132 addExecutionError(ExecutionErrors.UnknownAssociateProgramName.name(), associateProgramName); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 136 } 137 138 return null; 139 } 140 141}