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