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