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
062    /** Creates a new instance of GetPartySkillsCommand */
063    public GetPartySkillsCommand() {
064        super(null, FORM_FIELD_DEFINITIONS, true);
065    }
066
067    private Party party;
068    private SkillType skillType;
069
070    @Override
071    protected void handleForm() {
072        var partyName = form.getPartyName();
073        var skillTypeName = form.getSkillTypeName();
074        var parameterCount = (partyName == null ? 0 : 1) + (skillTypeName == null ? 0 : 1);
075
076        if(parameterCount == 1) {
077            if(partyName != null) {
078                party = partyLogic.getPartyByName(this, partyName, PartyTypes.EMPLOYEE.name());
079            } else {
080                skillType = employeeControl.getSkillTypeByName(skillTypeName);
081
082                if(skillType == null) {
083                    addExecutionError(ExecutionErrors.UnknownSkillTypeName.name(), skillTypeName);
084                }
085            }
086        } else {
087            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
088        }
089    }
090
091    @Override
092    protected Long getTotalEntities() {
093        Long total = null;
094
095        if(!hasExecutionErrors()) {
096            if(party != null) {
097                total = employeeControl.countPartySkillsByParty(party);
098            } else {
099                total = employeeControl.countPartySkillsBySkillType(skillType);
100            }
101        }
102
103        return total;
104    }
105
106    @Override
107    protected Collection<PartySkill> getEntities() {
108        Collection<PartySkill> entities = null;
109
110        if(!hasExecutionErrors()) {
111            if(party != null) {
112                entities = employeeControl.getPartySkillsByParty(party);
113            } else {
114                entities = employeeControl.getPartySkillsBySkillType(skillType);
115            }
116        }
117
118        return entities;
119    }
120
121    @Override
122    protected BaseResult getResult(Collection<PartySkill> entities) {
123        var result = EmployeeResultFactory.getGetPartySkillsResult();
124
125        if(entities != null) {
126            if(party != null) {
127                result.setParty(partyControl.getPartyTransfer(getUserVisit(), party));
128            } else {
129                result.setSkillType(employeeControl.getSkillTypeTransfer(getUserVisit(), skillType));
130            }
131
132            if(session.hasLimit(PartySkillFactory.class)) {
133                result.setPartySkillCount(getTotalEntities());
134            }
135
136            result.setPartySkills(employeeControl.getPartySkillTransfers(getUserVisit(), entities));
137        }
138
139        return result;
140    }
141    
142}