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.party.server.command;
018
019import com.echothree.control.user.party.common.form.GetDivisionChoicesForm;
020import com.echothree.control.user.party.common.result.GetDivisionChoicesResult;
021import com.echothree.control.user.party.common.result.PartyResultFactory;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.data.party.server.entity.Party;
025import com.echothree.model.data.party.server.entity.PartyCompany;
026import com.echothree.model.data.party.server.entity.PartyType;
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.persistence.Session;
034import java.util.Arrays;
035import java.util.Collections;
036import java.util.List;
037
038public class GetDivisionChoicesCommand
039        extends BaseSimpleCommand<GetDivisionChoicesForm> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
045                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("DefaultDivisionChoice", FieldType.ENTITY_NAME, false, null, null),
048                new FieldDefinition("AllowNullChoice", FieldType.BOOLEAN, true, null, null)
049                ));
050    }
051    
052    /** Creates a new instance of GetDivisionChoicesCommand */
053    public GetDivisionChoicesCommand(UserVisitPK userVisitPK, GetDivisionChoicesForm form) {
054        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
055    }
056    
057    @Override
058    protected BaseResult execute() {
059        GetDivisionChoicesResult result = PartyResultFactory.getGetDivisionChoicesResult();
060        String companyName = form.getCompanyName();
061        String partyName = form.getPartyName();
062        var parameterCount = (companyName == null ? 0 : 1) + (partyName == null ? 0 : 1);
063        
064        if(parameterCount == 1) {
065            var partyControl = Session.getModelController(PartyControl.class);
066            Party party = null;
067            
068            if(companyName != null) {
069                PartyCompany 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                    PartyType 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                String defaultDivisionChoice = form.getDefaultDivisionChoice();
092                boolean 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}