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.party.server.command;
018
019import com.echothree.control.user.party.common.form.GetDivisionChoicesForm;
020import com.echothree.control.user.party.common.result.PartyResultFactory;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.data.party.server.entity.Party;
024import com.echothree.model.data.user.common.pk.UserVisitPK;
025import com.echothree.util.common.message.ExecutionErrors;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.common.validation.FieldType;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.server.control.BaseSimpleCommand;
030import com.echothree.util.server.persistence.Session;
031import java.util.Arrays;
032import java.util.Collections;
033import java.util.List;
034import javax.enterprise.context.RequestScoped;
035
036@RequestScoped
037public class GetDivisionChoicesCommand
038        extends BaseSimpleCommand<GetDivisionChoicesForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
044                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
045                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("DefaultDivisionChoice", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("AllowNullChoice", FieldType.BOOLEAN, true, null, null)
048                ));
049    }
050    
051    /** Creates a new instance of GetDivisionChoicesCommand */
052    public GetDivisionChoicesCommand() {
053        super(null, FORM_FIELD_DEFINITIONS, false);
054    }
055    
056    @Override
057    protected BaseResult execute() {
058        var result = PartyResultFactory.getGetDivisionChoicesResult();
059        var companyName = form.getCompanyName();
060        var partyName = form.getPartyName();
061        var parameterCount = (companyName == null ? 0 : 1) + (partyName == null ? 0 : 1);
062        
063        if(parameterCount == 1) {
064            var partyControl = Session.getModelController(PartyControl.class);
065            Party party = null;
066            
067            if(companyName != null) {
068                var partyCompany = partyControl.getPartyCompanyByName(companyName);
069                
070                if(partyCompany == null) {
071                    addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
072                } else {
073                    party = partyCompany.getParty();
074                }
075            } else {
076                party = partyControl.getPartyByName(partyName);
077                
078                if(party != null) {
079                    var partyType = partyControl.getPartyTypeByName(PartyTypes.COMPANY.name());
080                    
081                    if(!party.getLastDetail().getPartyType().equals(partyType)) {
082                        addExecutionError(ExecutionErrors.InvalidPartyType.name());
083                    }
084                } else {
085                    addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
086                }
087            }
088            
089            if(!hasExecutionErrors()) {
090                var defaultDivisionChoice = form.getDefaultDivisionChoice();
091                var allowNullChoice = Boolean.parseBoolean(form.getAllowNullChoice());
092                
093                result.setDivisionChoices(partyControl.getDivisionChoices(party, defaultDivisionChoice, allowNullChoice));
094            }
095        } else {
096            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
097        }
098        
099        return result;
100    }
101    
102}