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