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.letter.server.command;
018
019import com.echothree.control.user.letter.common.edit.LetterSourceEdit;
020import com.echothree.model.control.contact.common.ContactMechanismTypes;
021import com.echothree.model.control.contact.server.control.ContactControl;
022import com.echothree.model.data.contact.server.entity.ContactMechanism;
023import com.echothree.model.data.contact.server.entity.PartyContactMechanism;
024import com.echothree.model.data.party.server.entity.Party;
025import com.echothree.util.common.message.ExecutionErrors;
026import com.echothree.util.server.message.ExecutionErrorAccumulator;
027
028public class LetterSourceCommandUtil {
029    
030    private LetterSourceCommandUtil() {
031        super();
032    }
033    
034    private static class LetterSourceCommandUtilHolder {
035        static LetterSourceCommandUtil instance = new LetterSourceCommandUtil();
036    }
037    
038    public static LetterSourceCommandUtil getInstance() {
039        return LetterSourceCommandUtilHolder.instance;
040    }
041    
042    public PartyContactMechanism getEmailAddressContactMechanism(ExecutionErrorAccumulator eea,
043            LetterSourceEdit edit, ContactControl contactControl, Party companyParty) {
044        PartyContactMechanism partyContactMechanism = null;
045        var emailAddressContactMechanismName = edit.getEmailAddressContactMechanismName();
046        var emailAddressContactMechanismAliasTypeName = edit.getEmailAddressContactMechanismAliasTypeName();
047        var alias = edit.getEmailAddressContactMechanismAlias();
048        var parameterCount = (emailAddressContactMechanismName != null && emailAddressContactMechanismAliasTypeName == null && alias == null? 1: 0)
049                + (emailAddressContactMechanismName == null && emailAddressContactMechanismAliasTypeName != null && alias != null? 1: 0);
050        
051        if(parameterCount == 1) {
052            ContactMechanism emailAddressContactMechanism = null;
053            
054            if(emailAddressContactMechanismName != null) {
055                emailAddressContactMechanism = contactControl.getContactMechanismByName(emailAddressContactMechanismName);
056                
057                if(emailAddressContactMechanism == null) {
058                    eea.addExecutionError(ExecutionErrors.UnknownEmailAddressContactMechanismName.name(), emailAddressContactMechanismName);
059                }
060            } else {
061                var emailAddressContactMechanismAliasType = contactControl.getContactMechanismAliasTypeByName(emailAddressContactMechanismAliasTypeName);
062                
063                if(emailAddressContactMechanismAliasType != null) {
064                    var emailAddressContactMechanismAlias = contactControl.getContactMechanismAliasByAlias(emailAddressContactMechanismAliasType, alias);
065                    
066                    if(emailAddressContactMechanismAlias != null) {
067                        emailAddressContactMechanism = emailAddressContactMechanismAlias.getContactMechanism();
068                    } else {
069                        eea.addExecutionError(ExecutionErrors.UnknownEmailAddressContactMechanismAlias.name(), emailAddressContactMechanismAliasTypeName, alias);
070                    }
071                } else {
072                    eea.addExecutionError(ExecutionErrors.UnknownEmailAddressContactMechanismAliasTypeName.name(), emailAddressContactMechanismAliasTypeName);
073                }
074            }
075            
076            if(emailAddressContactMechanism != null) {
077                var contactMechanismTypeName = emailAddressContactMechanism.getLastDetail().getContactMechanismType().getContactMechanismTypeName();
078                
079                if(contactMechanismTypeName.equals(ContactMechanismTypes.EMAIL_ADDRESS.name())) {
080                    partyContactMechanism = contactControl.getPartyContactMechanism(companyParty, emailAddressContactMechanism);
081                    
082                    if(partyContactMechanism == null) {
083                        eea.addExecutionError(ExecutionErrors.UnknownEmailAddressPartyContactMechanism.name());
084                    }
085                } else {
086                    eea.addExecutionError(ExecutionErrors.InvalidContactMechanismType.name());
087                }
088            }
089        } else {
090            eea.addExecutionError(ExecutionErrors.InvalidParameterCount.name());
091        }
092        
093        return partyContactMechanism;
094    }
095    
096    public PartyContactMechanism getPostalAddressContactMechanism(ExecutionErrorAccumulator eea,
097            LetterSourceEdit edit, ContactControl contactControl, Party companyParty) {
098        PartyContactMechanism partyContactMechanism = null;
099        var postalAddressContactMechanismName = edit.getPostalAddressContactMechanismName();
100        var postalAddressContactMechanismAliasTypeName = edit.getPostalAddressContactMechanismAliasTypeName();
101        var alias = edit.getPostalAddressContactMechanismAlias();
102        var parameterCount = (postalAddressContactMechanismName != null && postalAddressContactMechanismAliasTypeName == null && alias == null? 1: 0)
103                + (postalAddressContactMechanismName == null && postalAddressContactMechanismAliasTypeName != null && alias != null? 1: 0);
104        
105        if(parameterCount == 1) {
106            ContactMechanism postalAddressContactMechanism = null;
107            
108            if(postalAddressContactMechanismName != null) {
109                postalAddressContactMechanism = contactControl.getContactMechanismByName(postalAddressContactMechanismName);
110                
111                if(postalAddressContactMechanism == null) {
112                    eea.addExecutionError(ExecutionErrors.UnknownPostalAddressContactMechanismName.name(), postalAddressContactMechanismName);
113                }
114            } else {
115                var postalAddressContactMechanismAliasType = contactControl.getContactMechanismAliasTypeByName(postalAddressContactMechanismAliasTypeName);
116                
117                if(postalAddressContactMechanismAliasType != null) {
118                    var postalAddressContactMechanismAlias = contactControl.getContactMechanismAliasByAlias(postalAddressContactMechanismAliasType, alias);
119                    
120                    if(postalAddressContactMechanismAlias != null) {
121                        postalAddressContactMechanism = postalAddressContactMechanismAlias.getContactMechanism();
122                    } else {
123                        eea.addExecutionError(ExecutionErrors.UnknownPostalAddressContactMechanismAlias.name(), postalAddressContactMechanismAliasTypeName, alias);
124                    }
125                } else {
126                    eea.addExecutionError(ExecutionErrors.UnknownPostalAddressContactMechanismAliasTypeName.name(), postalAddressContactMechanismAliasTypeName);
127                }
128            }
129            
130            if(postalAddressContactMechanism != null) {
131                var contactMechanismTypeName = postalAddressContactMechanism.getLastDetail().getContactMechanismType().getContactMechanismTypeName();
132                
133                if(contactMechanismTypeName.equals(ContactMechanismTypes.POSTAL_ADDRESS.name())) {
134                    partyContactMechanism = contactControl.getPartyContactMechanism(companyParty, postalAddressContactMechanism);
135                    
136                    if(partyContactMechanism == null) {
137                        eea.addExecutionError(ExecutionErrors.UnknownEmailAddressPartyContactMechanism.name());
138                    }
139                } else {
140                    eea.addExecutionError(ExecutionErrors.InvalidContactMechanismType.name());
141                }
142            }
143        } else {
144            eea.addExecutionError(ExecutionErrors.InvalidParameterCount.name());
145        }
146        
147        return partyContactMechanism;
148    }
149    
150    public PartyContactMechanism getLetterSourceContactMechanism(ExecutionErrorAccumulator eea,
151            LetterSourceEdit edit, ContactControl contactControl, Party companyParty) {
152        PartyContactMechanism partyContactMechanism = null;
153        var letterSourceContactMechanismName = edit.getLetterSourceContactMechanismName();
154        var letterSourceContactMechanismAliasTypeName = edit.getLetterSourceContactMechanismAliasTypeName();
155        var alias = edit.getLetterSourceContactMechanismAlias();
156        var parameterCount = (letterSourceContactMechanismName != null && letterSourceContactMechanismAliasTypeName == null && alias == null? 1: 0)
157                + (letterSourceContactMechanismName == null && letterSourceContactMechanismAliasTypeName != null && alias != null? 1: 0);
158        
159        if(parameterCount == 1) {
160            ContactMechanism letterSourceContactMechanism = null;
161            
162            if(letterSourceContactMechanismName != null) {
163                letterSourceContactMechanism = contactControl.getContactMechanismByName(letterSourceContactMechanismName);
164                
165                if(letterSourceContactMechanism == null) {
166                    eea.addExecutionError(ExecutionErrors.UnknownLetterSourceContactMechanismName.name(), letterSourceContactMechanismName);
167                }
168            } else {
169                var letterSourceContactMechanismAliasType = contactControl.getContactMechanismAliasTypeByName(letterSourceContactMechanismAliasTypeName);
170                
171                if(letterSourceContactMechanismAliasType != null) {
172                    var letterSourceContactMechanismAlias = contactControl.getContactMechanismAliasByAlias(letterSourceContactMechanismAliasType, alias);
173                    
174                    if(letterSourceContactMechanismAlias != null) {
175                        letterSourceContactMechanism = letterSourceContactMechanismAlias.getContactMechanism();
176                    } else {
177                        eea.addExecutionError(ExecutionErrors.UnknownLetterSourceContactMechanismAlias.name(), letterSourceContactMechanismAliasTypeName, alias);
178                    }
179                } else {
180                    eea.addExecutionError(ExecutionErrors.UnknownLetterSourceContactMechanismAliasTypeName.name(), letterSourceContactMechanismAliasTypeName);
181                }
182            }
183            
184            if(letterSourceContactMechanism != null) {
185                var contactMechanismTypeName = letterSourceContactMechanism.getLastDetail().getContactMechanismType().getContactMechanismTypeName();
186                
187                if(contactMechanismTypeName.equals(ContactMechanismTypes.WEB_ADDRESS.name())) {
188                    partyContactMechanism = contactControl.getPartyContactMechanism(companyParty, letterSourceContactMechanism);
189                    
190                    if(partyContactMechanism == null) {
191                        eea.addExecutionError(ExecutionErrors.UnknownEmailAddressPartyContactMechanism.name());
192                    }
193                } else {
194                    eea.addExecutionError(ExecutionErrors.InvalidContactMechanismType.name());
195                }
196            }
197        } else {
198            eea.addExecutionError(ExecutionErrors.InvalidParameterCount.name());
199        }
200        
201        return partyContactMechanism;
202    }
203    
204}