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.control.user.associate.common.result.AssociateResultFactory;
021import com.echothree.model.control.associate.server.control.AssociateControl;
022import com.echothree.model.control.contact.server.control.ContactControl;
023import com.echothree.model.data.associate.server.entity.AssociatePartyContactMechanism;
024import com.echothree.model.data.contact.server.entity.ContactMechanism;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.server.control.BaseSimpleCommand;
031import java.util.List;
032import javax.enterprise.context.Dependent;
033import javax.inject.Inject;
034
035@Dependent
036public class CreateAssociatePartyContactMechanismCommand
037        extends BaseSimpleCommand<CreateAssociatePartyContactMechanismForm> {
038    
039    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
040    
041    static {
042        FORM_FIELD_DEFINITIONS = List.of(
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    @Inject
056    AssociateControl associateControl;
057
058    @Inject
059    ContactControl contactControl;
060
061    
062    /** Creates a new instance of CreateAssociatePartyContactMechanismCommand */
063    public CreateAssociatePartyContactMechanismCommand() {
064        super(null, FORM_FIELD_DEFINITIONS, false);
065    }
066    
067    @Override
068    protected BaseResult execute() {
069        var result = AssociateResultFactory.getCreateAssociatePartyContactMechanismResult();
070        AssociatePartyContactMechanism associatePartyContactMechanism = null;
071        var contactMechanismName = form.getContactMechanismName();
072        var contactMechanismAliasTypeName = form.getContactMechanismAliasTypeName();
073        var alias = form.getAlias();
074        var parameterCount = (contactMechanismName == null ? 0 : 1) + (contactMechanismAliasTypeName == null && alias == null ? 0 : 1);
075        
076        if(parameterCount == 1) {
077            var associateProgramName = form.getAssociateProgramName();
078            var associateProgram = associateControl.getAssociateProgramByName(associateProgramName);
079            
080            if(associateProgram != null) {
081                var associateName = form.getAssociateName();
082                var associate = associateControl.getAssociateByName(associateProgram, associateName);
083                
084                if(associate != null) {
085                    var associatePartyContactMechanismName = form.getAssociatePartyContactMechanismName();
086
087                    associatePartyContactMechanism = associateControl.getAssociatePartyContactMechanismByName(associate,
088                            associatePartyContactMechanismName);
089                    
090                    if(associatePartyContactMechanism == null) {
091                        ContactMechanism contactMechanism = null;
092                        
093                        if(contactMechanismName != null) {
094                            contactMechanism = contactControl.getContactMechanismByName(contactMechanismName);
095                            
096                            if(contactMechanism == null) {
097                                addExecutionError(ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName);
098                            }
099                        } else {
100                            var contactMechanismAliasType = contactControl.getContactMechanismAliasTypeByName(contactMechanismAliasTypeName);
101                            
102                            if(contactMechanismAliasType != null) {
103                                var contactMechanismAlias = contactControl.getContactMechanismAliasByAlias(contactMechanismAliasType,
104                                        alias);
105                                
106                                if(contactMechanismAlias != null) {
107                                    contactMechanism = contactMechanismAlias.getContactMechanism();
108                                } else {
109                                    addExecutionError(ExecutionErrors.UnknownContactMechanismAlias.name(), alias);
110                                }
111                            } else {
112                                addExecutionError(ExecutionErrors.UnknownContactMechanismAliasTypeName.name(), contactMechanismAliasTypeName);
113                            }
114                        }
115                        
116                        if(!hasExecutionErrors()) {
117                            var partyContactMechanism = contactControl.getPartyContactMechanism(associate.getLastDetail().getParty(),
118                                    contactMechanism);
119                            
120                            if(partyContactMechanism != null) {
121                                var isDefault = Boolean.valueOf(form.getIsDefault());
122                                var sortOrder = Integer.valueOf(form.getSortOrder());
123                                
124                                associatePartyContactMechanism = associateControl.createAssociatePartyContactMechanism(associate, associatePartyContactMechanismName,
125                                        partyContactMechanism, isDefault, sortOrder, getPartyPK());
126                            } else {
127                                addExecutionError(ExecutionErrors.UnknownPartyContactMechanism.name(), contactMechanismName);
128                            }
129                        }
130                    } else {
131                        addExecutionError(ExecutionErrors.DuplicateAssociatePartyContactMechanismName.name(), associatePartyContactMechanismName);
132                    }
133                } else {
134                    addExecutionError(ExecutionErrors.UnknownAssociateName.name(), associateName);
135                }
136            } else {
137                addExecutionError(ExecutionErrors.UnknownAssociateProgramName.name(), associateProgramName);
138            }
139        } else {
140            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
141        }
142        
143        if(associatePartyContactMechanism != null) {
144            result.setEntityRef(associatePartyContactMechanism.getPrimaryKey().getEntityRef());
145            result.setAssociateProgramName(associatePartyContactMechanism.getLastDetail().getAssociate().getLastDetail().getAssociateProgram().getLastDetail().getAssociateProgramName());
146            result.setAssociateName(associatePartyContactMechanism.getLastDetail().getAssociate().getLastDetail().getAssociateName());
147            result.setAssociatePartyContactMechanismName(associatePartyContactMechanism.getLastDetail().getAssociatePartyContactMechanismName());
148        }
149
150        return result;
151    }
152    
153}