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 com.echothree.util.server.persistence.Session;
031import java.util.List;
032import javax.enterprise.context.Dependent;
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    /** Creates a new instance of GetContactMechanismPurposeChoicesCommand */
051    public GetContactMechanismPurposeChoicesCommand() {
052        super(null, FORM_FIELD_DEFINITIONS, false);
053    }
054    
055    @Override
056    protected BaseResult execute() {
057        var result = ContactResultFactory.getGetContactMechanismPurposeChoicesResult();
058        var contactMechanismName = form.getContactMechanismName();
059        var contactMechanismTypeName = form.getContactMechanismTypeName();
060        var contactListName = form.getContactListName();
061        var parameterCount = (contactMechanismName == null ? 0 : 1) + (contactMechanismTypeName == null ? 0 : 1) + (contactListName == null ? 0 : 1);
062        
063        if(parameterCount < 2) {
064            var contactControl = Session.getModelController(ContactControl.class);
065            ContactMechanismType contactMechanismType = null;
066            var defaultContactMechanismPurposeChoice = form.getDefaultContactMechanismPurposeChoice();
067            var allowNullChoice = Boolean.parseBoolean(form.getAllowNullChoice());
068            
069            if(contactMechanismName != null) {
070                var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName);
071                
072                if(contactMechanism != null) {
073                    contactMechanismType = contactMechanism.getLastDetail().getContactMechanismType();
074                } else {
075                    addExecutionError(ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName);
076                }
077            } else if(contactMechanismTypeName != null) {
078                contactMechanismType = contactControl.getContactMechanismTypeByName(contactMechanismTypeName);
079
080                if(contactMechanismType == null) {
081                    addExecutionError(ExecutionErrors.UnknownContactMechanismTypeName.name(), contactMechanismTypeName);
082                }
083            } else if(contactListName != null) {
084                var contactList = ContactListLogic.getInstance().getContactListByName(this, contactListName);
085                
086                if(!hasExecutionErrors()) {
087                    result.setContactMechanismPurposeChoices(contactControl.getContactMechanismPurposeChoicesByContactList(defaultContactMechanismPurposeChoice,
088                            getPreferredLanguage(), allowNullChoice, contactList));
089                }
090            } else {
091                result.setContactMechanismPurposeChoices(contactControl.getContactMechanismPurposeChoices(defaultContactMechanismPurposeChoice,
092                        getPreferredLanguage(), allowNullChoice));
093            }
094
095            if(contactMechanismType != null) {
096                result.setContactMechanismPurposeChoices(contactControl.getContactMechanismPurposeChoicesByContactMechanismType(contactMechanismType,
097                        defaultContactMechanismPurposeChoice, getPreferredLanguage(), allowNullChoice));
098            }
099        } else {
100            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
101        }
102        
103        return result;
104    }
105    
106}