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