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 com.echothree.util.server.persistence.Session;
037import java.util.List;
038import javax.enterprise.context.Dependent;
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    /** Creates a new instance of CreateLetterSourceCommand */
075    public CreateLetterSourceCommand() {
076        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
077    }
078    
079    @Override
080    protected BaseResult execute() {
081        var letterControl = Session.getModelController(LetterControl.class);
082        var letterSourceName = form.getLetterSourceName();
083        var letterSource = letterControl.getLetterSourceByName(letterSourceName);
084        
085        if(letterSource == null) {
086            var partyName = form.getPartyName();
087            var companyName = form.getCompanyName();
088            var parameterCount = (partyName != null? 1: 0) + (companyName != null? 1: 0);
089            
090            if(parameterCount == 1) {
091                var partyControl = Session.getModelController(PartyControl.class);
092                Party companyParty = null;
093                
094                if(partyName != null) {
095                    companyParty = partyControl.getPartyByName(partyName);
096                    
097                    if(companyParty == null) {
098                        addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
099                    } else {
100                        var partyTypeName = companyParty.getLastDetail().getPartyType().getPartyTypeName();
101                        
102                        if(!partyTypeName.equals(PartyTypes.COMPANY.name())) {
103                            addExecutionError(ExecutionErrors.InvalidPartyType.name(), partyTypeName);
104                        }
105                    }
106                } else {
107                    var partyCompany = partyControl.getPartyCompanyByName(companyName);
108                    
109                    if(partyCompany == null) {
110                        addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
111                    } else {
112                        companyParty = partyCompany.getParty();
113                    }
114                }
115                
116                if(!hasExecutionErrors()) {
117                    var contactControl = Session.getModelController(ContactControl.class);
118                    var letterSourceCommandUtil = LetterSourceCommandUtil.getInstance();
119                    var emailAddressPartyContactMechanism = letterSourceCommandUtil.getEmailAddressContactMechanism(this, form,
120                            contactControl, companyParty);
121                    
122                    if(!hasExecutionErrors()) {
123                        var postalAddressPartyContactMechanism = letterSourceCommandUtil.getPostalAddressContactMechanism(this, form,
124                                contactControl, companyParty);
125                        
126                        if(!hasExecutionErrors()) {
127                            var letterSourcePartyContactMechanism = letterSourceCommandUtil.getLetterSourceContactMechanism(this, form,
128                                    contactControl, companyParty);
129                            
130                            if(!hasExecutionErrors()) {
131                                var partyPK = getPartyPK();
132                                var isDefault = Boolean.valueOf(form.getIsDefault());
133                                var sortOrder = Integer.valueOf(form.getSortOrder());
134                                var description = form.getDescription();
135                                
136                                letterSource = letterControl.createLetterSource(letterSourceName, companyParty,
137                                        emailAddressPartyContactMechanism, postalAddressPartyContactMechanism,
138                                        letterSourcePartyContactMechanism, isDefault, sortOrder, partyPK);
139                                
140                                if(description != null) {
141                                    letterControl.createLetterSourceDescription(letterSource, getPreferredLanguage(), description, partyPK);
142                                }
143                            }
144                        }
145                    }
146                }
147            } else {
148                addExecutionError(ExecutionErrors.InvalidParameterCount.name());
149            }
150        } else {
151            addExecutionError(ExecutionErrors.DuplicateLetterSourceName.name(), letterSourceName);
152        }
153        
154        return null;
155    }
156    
157}