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 com.echothree.util.server.persistence.Session;
031import java.util.List;
032import javax.enterprise.context.Dependent;
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    /** Creates a new instance of GetDivisionChoicesCommand */
050    public GetDivisionChoicesCommand() {
051        super(null, FORM_FIELD_DEFINITIONS, false);
052    }
053    
054    @Override
055    protected BaseResult execute() {
056        var result = PartyResultFactory.getGetDivisionChoicesResult();
057        var companyName = form.getCompanyName();
058        var partyName = form.getPartyName();
059        var parameterCount = (companyName == null ? 0 : 1) + (partyName == null ? 0 : 1);
060        
061        if(parameterCount == 1) {
062            var partyControl = Session.getModelController(PartyControl.class);
063            Party party = null;
064            
065            if(companyName != null) {
066                var partyCompany = partyControl.getPartyCompanyByName(companyName);
067                
068                if(partyCompany == null) {
069                    addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName);
070                } else {
071                    party = partyCompany.getParty();
072                }
073            } else {
074                party = partyControl.getPartyByName(partyName);
075                
076                if(party != null) {
077                    var partyType = partyControl.getPartyTypeByName(PartyTypes.COMPANY.name());
078                    
079                    if(!party.getLastDetail().getPartyType().equals(partyType)) {
080                        addExecutionError(ExecutionErrors.InvalidPartyType.name());
081                    }
082                } else {
083                    addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
084                }
085            }
086            
087            if(!hasExecutionErrors()) {
088                var defaultDivisionChoice = form.getDefaultDivisionChoice();
089                var allowNullChoice = Boolean.parseBoolean(form.getAllowNullChoice());
090                
091                result.setDivisionChoices(partyControl.getDivisionChoices(party, defaultDivisionChoice, allowNullChoice));
092            }
093        } else {
094            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
095        }
096        
097        return result;
098    }
099    
100}