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