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.employee.server.command;
018
019import com.echothree.control.user.employee.common.form.GetPartySkillsForm;
020import com.echothree.control.user.employee.common.result.EmployeeResultFactory;
021import com.echothree.model.control.employee.server.control.EmployeeControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.control.party.server.logic.PartyLogic;
025import com.echothree.model.data.employee.server.entity.PartySkill;
026import com.echothree.model.data.employee.server.entity.SkillType;
027import com.echothree.model.data.employee.server.factory.PartySkillFactory;
028import com.echothree.model.data.party.server.entity.Party;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
034import java.util.Collection;
035import java.util.List;
036import javax.enterprise.context.Dependent;
037import javax.inject.Inject;
038
039@Dependent
040public class GetPartySkillsCommand
041        extends BasePaginatedMultipleEntitiesCommand<PartySkill, GetPartySkillsForm> {
042    
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        FORM_FIELD_DEFINITIONS = List.of(
047                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
048                new FieldDefinition("SkillTypeName", FieldType.ENTITY_NAME, false, null, null)
049        );
050    }
051    
052    @Inject
053    EmployeeControl employeeControl;
054
055    @Inject
056    PartyControl partyControl;
057
058    @Inject
059    PartyLogic partyLogic;
060
061    /** Creates a new instance of GetPartySkillsCommand */
062    public GetPartySkillsCommand() {
063        super(null, FORM_FIELD_DEFINITIONS, true);
064    }
065
066    private Party party;
067    private SkillType skillType;
068
069    @Override
070    protected void handleForm() {
071        var partyName = form.getPartyName();
072        var skillTypeName = form.getSkillTypeName();
073        var parameterCount = (partyName == null ? 0 : 1) + (skillTypeName == null ? 0 : 1);
074
075        if(parameterCount == 1) {
076            if(partyName != null) {
077                party = partyLogic.getPartyByName(this, partyName, PartyTypes.EMPLOYEE.name());
078            } else {
079                skillType = employeeControl.getSkillTypeByName(skillTypeName);
080
081                if(skillType == null) {
082                    addExecutionError(ExecutionErrors.UnknownSkillTypeName.name(), skillTypeName);
083                }
084            }
085        } else {
086            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
087        }
088    }
089
090    @Override
091    protected Long getTotalEntities() {
092        Long total = null;
093
094        if(!hasExecutionErrors()) {
095            if(party != null) {
096                total = employeeControl.countPartySkillsByParty(party);
097            } else {
098                total = employeeControl.countPartySkillsBySkillType(skillType);
099            }
100        }
101
102        return total;
103    }
104
105    @Override
106    protected Collection<PartySkill> getEntities() {
107        Collection<PartySkill> entities = null;
108
109        if(!hasExecutionErrors()) {
110            if(party != null) {
111                entities = employeeControl.getPartySkillsByParty(party);
112            } else {
113                entities = employeeControl.getPartySkillsBySkillType(skillType);
114            }
115        }
116
117        return entities;
118    }
119
120    @Override
121    protected BaseResult getResult(Collection<PartySkill> entities) {
122        var result = EmployeeResultFactory.getGetPartySkillsResult();
123
124        if(entities != null) {
125            if(party != null) {
126                result.setParty(partyControl.getPartyTransfer(getUserVisit(), party));
127            } else {
128                result.setSkillType(employeeControl.getSkillTypeTransfer(getUserVisit(), skillType));
129            }
130
131            if(session.hasLimit(PartySkillFactory.class)) {
132                result.setPartySkillCount(getTotalEntities());
133            }
134
135            result.setPartySkills(employeeControl.getPartySkillTransfers(getUserVisit(), entities));
136        }
137
138        return result;
139    }
140    
141}