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.GetPartyResponsibilitiesForm;
020import com.echothree.control.user.employee.common.result.EmployeeResultFactory;
021import com.echothree.model.control.employee.server.control.EmployeeControl;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.control.party.server.logic.PartyLogic;
024import com.echothree.model.data.employee.server.entity.PartyResponsibility;
025import com.echothree.model.data.employee.server.entity.ResponsibilityType;
026import com.echothree.model.data.employee.server.factory.PartyResponsibilityFactory;
027import com.echothree.model.data.party.server.entity.Party;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
033import java.util.Collection;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036import javax.inject.Inject;
037
038@Dependent
039public class GetPartyResponsibilitiesCommand
040        extends BasePaginatedMultipleEntitiesCommand<PartyResponsibility, GetPartyResponsibilitiesForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("ResponsibilityTypeName", FieldType.ENTITY_NAME, false, null, null)
048        );
049    }
050
051    @Inject
052    EmployeeControl employeeControl;
053
054    @Inject
055    PartyControl partyControl;
056
057    @Inject
058    PartyLogic partyLogic;
059
060
061    /** Creates a new instance of GetPartyResponsibilitiesCommand */
062    public GetPartyResponsibilitiesCommand() {
063        super(null, FORM_FIELD_DEFINITIONS, true);
064    }
065
066    private Party party;
067    private ResponsibilityType responsibilityType;
068
069    @Override
070    protected void handleForm() {
071        var partyName = form.getPartyName();
072        var responsibilityTypeName = form.getResponsibilityTypeName();
073        var parameterCount = (partyName == null ? 0 : 1) + (responsibilityTypeName == null ? 0 : 1);
074
075        if(parameterCount == 1) {
076            if(partyName != null) {
077                party = partyLogic.getPartyByName(this, partyName);
078            } else {
079                responsibilityType = employeeControl.getResponsibilityTypeByName(responsibilityTypeName);
080
081                if(responsibilityType == null) {
082                    addExecutionError(ExecutionErrors.UnknownResponsibilityTypeName.name(), responsibilityTypeName);
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.countPartyResponsibilitiesByParty(party);
097            } else {
098                total = employeeControl.countPartyResponsibilitiesByResponsibilityType(responsibilityType);
099            }
100        }
101
102        return total;
103    }
104
105    @Override
106    protected Collection<PartyResponsibility> getEntities() {
107        Collection<PartyResponsibility> entities = null;
108
109        if(!hasExecutionErrors()) {
110            if(party != null) {
111                entities = employeeControl.getPartyResponsibilitiesByParty(party);
112            } else {
113                entities = employeeControl.getPartyResponsibilitiesByResponsibilityType(responsibilityType);
114            }
115        }
116
117        return entities;
118    }
119
120    @Override
121    protected BaseResult getResult(Collection<PartyResponsibility> entities) {
122        var result = EmployeeResultFactory.getGetPartyResponsibilitiesResult();
123
124        if(entities != null) {
125            if(party != null) {
126                result.setParty(partyControl.getPartyTransfer(getUserVisit(), party));
127            } else {
128                result.setResponsibilityType(employeeControl.getResponsibilityTypeTransfer(getUserVisit(), responsibilityType));
129            }
130
131            if(session.hasLimit(PartyResponsibilityFactory.class)) {
132                result.setPartyResponsibilityCount(getTotalEntities());
133            }
134
135            result.setPartyResponsibilities(employeeControl.getPartyResponsibilityTransfers(getUserVisit(), entities));
136        }
137
138        return result;
139    }
140    
141}