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.GetDepartmentChoicesForm;
020import com.echothree.control.user.party.common.result.GetDepartmentChoicesResult;
021import com.echothree.control.user.party.common.result.PartyResultFactory;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.control.party.server.logic.CompanyLogic;
024import com.echothree.model.control.party.server.logic.DivisionLogic;
025import com.echothree.model.data.party.server.entity.PartyCompany;
026import com.echothree.model.data.party.server.entity.PartyDivision;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.server.control.BaseSimpleCommand;
032import com.echothree.util.server.persistence.Session;
033import java.util.Arrays;
034import java.util.Collections;
035import java.util.List;
036
037public class GetDepartmentChoicesCommand
038        extends BaseSimpleCommand<GetDepartmentChoicesForm> {
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("DivisionName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("DefaultDepartmentChoice", FieldType.ENTITY_NAME, false, null, null),
048                new FieldDefinition("AllowNullChoice", FieldType.BOOLEAN, true, null, null)
049                ));
050    }
051    
052    /** Creates a new instance of GetDepartmentChoicesCommand */
053    public GetDepartmentChoicesCommand(UserVisitPK userVisitPK, GetDepartmentChoicesForm form) {
054        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
055    }
056    
057    @Override
058    protected BaseResult execute() {
059        GetDepartmentChoicesResult result = PartyResultFactory.getGetDepartmentChoicesResult();
060        String companyName = form.getCompanyName();
061        PartyCompany partyCompany = CompanyLogic.getInstance().getPartyCompanyByName(this, companyName, null, null, false);
062
063        if(!hasExecutionErrors()) {
064            String divisionName = form.getDivisionName();
065            String partyName = form.getPartyName();
066            PartyDivision partyDivision = DivisionLogic.getInstance().getPartyDivisionByName(this, partyCompany == null ? null : partyCompany.getParty(), divisionName, partyName, null, true);
067
068            if(!hasExecutionErrors()) {
069                var partyControl = Session.getModelController(PartyControl.class);
070                String defaultDepartmentChoice = form.getDefaultDepartmentChoice();
071                boolean allowNullChoice = Boolean.parseBoolean(form.getAllowNullChoice());
072
073                result.setDepartmentChoices(partyControl.getDepartmentChoices(partyDivision.getParty(), defaultDepartmentChoice, allowNullChoice));
074            }
075        }
076        
077        return result;
078    }
079    
080}