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