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