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