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.contact.server.command; 018 019import com.echothree.control.user.contact.common.form.GetContactMechanismPurposeChoicesForm; 020import com.echothree.control.user.contact.common.result.ContactResultFactory; 021import com.echothree.model.control.contact.server.control.ContactControl; 022import com.echothree.model.control.contactlist.server.logic.ContactListLogic; 023import com.echothree.model.data.contact.server.entity.ContactMechanismType; 024import com.echothree.model.data.user.common.pk.UserVisitPK; 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.common.command.BaseResult; 029import com.echothree.util.server.control.BaseSimpleCommand; 030import java.util.List; 031import javax.enterprise.context.Dependent; 032import javax.inject.Inject; 033 034@Dependent 035public class GetContactMechanismPurposeChoicesCommand 036 extends BaseSimpleCommand<GetContactMechanismPurposeChoicesForm> { 037 038 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 039 040 static { 041 FORM_FIELD_DEFINITIONS = List.of( 042 new FieldDefinition("ContactMechanismName", FieldType.ENTITY_NAME, false, null, null), 043 new FieldDefinition("ContactMechanismTypeName", FieldType.ENTITY_NAME, false, null, null), 044 new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, false, null, null), 045 new FieldDefinition("DefaultContactMechanismPurposeChoice", FieldType.ENTITY_NAME, false, null, null), 046 new FieldDefinition("AllowNullChoice", FieldType.BOOLEAN, true, null, null) 047 ); 048 } 049 050 @Inject 051 ContactControl contactControl; 052 053 @Inject 054 ContactListLogic contactListLogic; 055 056 057 /** Creates a new instance of GetContactMechanismPurposeChoicesCommand */ 058 public GetContactMechanismPurposeChoicesCommand() { 059 super(null, FORM_FIELD_DEFINITIONS, false); 060 } 061 062 @Override 063 protected BaseResult execute() { 064 var result = ContactResultFactory.getGetContactMechanismPurposeChoicesResult(); 065 var contactMechanismName = form.getContactMechanismName(); 066 var contactMechanismTypeName = form.getContactMechanismTypeName(); 067 var contactListName = form.getContactListName(); 068 var parameterCount = (contactMechanismName == null ? 0 : 1) + (contactMechanismTypeName == null ? 0 : 1) + (contactListName == null ? 0 : 1); 069 070 if(parameterCount < 2) { 071 ContactMechanismType contactMechanismType = null; 072 var defaultContactMechanismPurposeChoice = form.getDefaultContactMechanismPurposeChoice(); 073 var allowNullChoice = Boolean.parseBoolean(form.getAllowNullChoice()); 074 075 if(contactMechanismName != null) { 076 var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName); 077 078 if(contactMechanism != null) { 079 contactMechanismType = contactMechanism.getLastDetail().getContactMechanismType(); 080 } else { 081 addExecutionError(ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName); 082 } 083 } else if(contactMechanismTypeName != null) { 084 contactMechanismType = contactControl.getContactMechanismTypeByName(contactMechanismTypeName); 085 086 if(contactMechanismType == null) { 087 addExecutionError(ExecutionErrors.UnknownContactMechanismTypeName.name(), contactMechanismTypeName); 088 } 089 } else if(contactListName != null) { 090 var contactList = contactListLogic.getContactListByName(this, contactListName); 091 092 if(!hasExecutionErrors()) { 093 result.setContactMechanismPurposeChoices(contactControl.getContactMechanismPurposeChoicesByContactList(defaultContactMechanismPurposeChoice, 094 getPreferredLanguage(), allowNullChoice, contactList)); 095 } 096 } else { 097 result.setContactMechanismPurposeChoices(contactControl.getContactMechanismPurposeChoices(defaultContactMechanismPurposeChoice, 098 getPreferredLanguage(), allowNullChoice)); 099 } 100 101 if(contactMechanismType != null) { 102 result.setContactMechanismPurposeChoices(contactControl.getContactMechanismPurposeChoicesByContactMechanismType(contactMechanismType, 103 defaultContactMechanismPurposeChoice, getPreferredLanguage(), allowNullChoice)); 104 } 105 } else { 106 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 107 } 108 109 return result; 110 } 111 112}