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