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.letter.server.command;
018
019import com.echothree.control.user.letter.common.form.CreateLetterSourceForm;
020import com.echothree.model.control.contact.server.control.ContactControl;
021import com.echothree.model.control.letter.server.control.LetterControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.control.security.common.SecurityRoleGroups;
025import com.echothree.model.control.security.common.SecurityRoles;
026import com.echothree.model.data.party.server.entity.Party;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.server.control.BaseSimpleCommand;
033import com.echothree.util.server.control.CommandSecurityDefinition;
034import com.echothree.util.server.control.PartyTypeDefinition;
035import com.echothree.util.server.control.SecurityRoleDefinition;
036import java.util.List;
037import javax.enterprise.context.Dependent;
038import javax.inject.Inject;
039
040@Dependent
041public class CreateLetterSourceCommand
042        extends BaseSimpleCommand<CreateLetterSourceForm> {
043    
044    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046    
047    static {
048        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
049                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
050                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
051                        new SecurityRoleDefinition(SecurityRoleGroups.LetterSource.name(), SecurityRoles.Create.name())
052                ))
053        ));
054        
055        FORM_FIELD_DEFINITIONS = List.of(
056                new FieldDefinition("LetterSourceName", FieldType.ENTITY_NAME, true, null, null),
057                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
058                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
059                new FieldDefinition("EmailAddressContactMechanismName", FieldType.ENTITY_NAME, false, null, null),
060                new FieldDefinition("EmailAddressContactMechanismAliasTypeName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("EmailAddressContactMechanismAlias", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("PostalAddressContactMechanismName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("PostalAddressContactMechanismAliasTypeName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("PostalAddressContactMechanismAlias", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("LetterSourceContactMechanismName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("LetterSourceContactMechanismAliasTypeName", FieldType.ENTITY_NAME, false, null, null),
067                new FieldDefinition("LetterSourceContactMechanismAlias", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
069                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
070                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
071        );
072    }
073
074    @Inject
075    ContactControl contactControl;
076
077    @Inject
078    LetterControl letterControl;
079
080    @Inject
081    PartyControl partyControl;
082
083    
084    /** Creates a new instance of CreateLetterSourceCommand */
085    public CreateLetterSourceCommand() {
086        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
087    }
088    
089    @Override
090    protected BaseResult execute() {
091        var letterSourceName = form.getLetterSourceName();
092        var letterSource = letterControl.getLetterSourceByName(letterSourceName);
093        
094        if(letterSource == null) {
095            var partyName = form.getPartyName();
096            var companyName = form.getCompanyName();
097            var parameterCount = (partyName != null? 1: 0) + (companyName != null? 1: 0);
098            
099            if(parameterCount == 1) {
100                Party companyParty = null;
101                
102                if(partyName != null) {
103                    companyParty = partyControl.getPartyByName(partyName);
104                    
105                    if(companyParty == null) {
106                        addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
107                    } else {
108                        var partyTypeName = companyParty.getLastDetail().getPartyType().getPartyTypeName();
109                        
110                        if(!partyTypeName.equals(PartyTypes.COMPANY.name())) {
111                            addExecutionError(ExecutionErrors.InvalidPartyType.name(), partyTypeName);
112                        }
113                    }
114                } else {
115                    var partyCompany = partyControl.getPartyCompanyByName(companyName);
116                    
117                    if(partyCompany == null) {
118                        addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
119                    } else {
120                        companyParty = partyCompany.getParty();
121                    }
122                }
123                
124                if(!hasExecutionErrors()) {
125                    var letterSourceCommandUtil = LetterSourceCommandUtil.getInstance();
126                    var emailAddressPartyContactMechanism = letterSourceCommandUtil.getEmailAddressContactMechanism(this, form,
127                            contactControl, companyParty);
128                    
129                    if(!hasExecutionErrors()) {
130                        var postalAddressPartyContactMechanism = letterSourceCommandUtil.getPostalAddressContactMechanism(this, form,
131                                contactControl, companyParty);
132                        
133                        if(!hasExecutionErrors()) {
134                            var letterSourcePartyContactMechanism = letterSourceCommandUtil.getLetterSourceContactMechanism(this, form,
135                                    contactControl, companyParty);
136                            
137                            if(!hasExecutionErrors()) {
138                                var partyPK = getPartyPK();
139                                var isDefault = Boolean.valueOf(form.getIsDefault());
140                                var sortOrder = Integer.valueOf(form.getSortOrder());
141                                var description = form.getDescription();
142                                
143                                letterSource = letterControl.createLetterSource(letterSourceName, companyParty,
144                                        emailAddressPartyContactMechanism, postalAddressPartyContactMechanism,
145                                        letterSourcePartyContactMechanism, isDefault, sortOrder, partyPK);
146                                
147                                if(description != null) {
148                                    letterControl.createLetterSourceDescription(letterSource, getPreferredLanguage(), description, partyPK);
149                                }
150                            }
151                        }
152                    }
153                }
154            } else {
155                addExecutionError(ExecutionErrors.InvalidParameterCount.name());
156            }
157        } else {
158            addExecutionError(ExecutionErrors.DuplicateLetterSourceName.name(), letterSourceName);
159        }
160        
161        return null;
162    }
163    
164}