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.workrequirement.server.command;
018
019import com.echothree.control.user.workrequirement.common.form.GetWorkAssignmentsForm;
020import com.echothree.control.user.workrequirement.common.result.WorkRequirementResultFactory;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.control.party.server.logic.EmployeeLogic;
023import com.echothree.model.control.party.server.logic.PartyLogic;
024import com.echothree.model.control.workrequirement.server.control.WorkRequirementControl;
025import com.echothree.model.data.party.server.entity.Party;
026import com.echothree.model.data.workrequirement.server.entity.WorkAssignment;
027import com.echothree.model.data.workrequirement.server.factory.WorkAssignmentFactory;
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 GetWorkAssignmentsCommand
040        extends BasePaginatedMultipleEntitiesCommand<WorkAssignment, GetWorkAssignmentsForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("EmployeeName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null)
048        );
049    }
050
051    @Inject
052    PartyControl partyControl;
053
054    @Inject
055    WorkRequirementControl workRequirementControl;
056
057    @Inject
058    EmployeeLogic employeeLogic;
059
060    @Inject
061    PartyLogic partyLogic;
062
063    /** Creates a new instance of GetWorkAssignmentsCommand */
064    public GetWorkAssignmentsCommand() {
065        super(null, FORM_FIELD_DEFINITIONS, true);
066    }
067
068    private Party party;
069
070    @Override
071    protected void handleForm() {
072        var employeeName = form.getEmployeeName();
073        var partyName = form.getPartyName();
074        var parameterCount = (employeeName == null ? 0 : 1) + (partyName == null ? 0 : 1);
075
076        if(parameterCount < 2) {
077            if(employeeName != null) {
078                var partyEmployee = employeeLogic.getPartyEmployeeByName(this, employeeName, null);
079
080                if(!hasExecutionErrors()) {
081                    party = partyEmployee.getParty();
082                }
083            } else if(partyName != null) {
084                party = partyLogic.getPartyByName(this, partyName);
085            } else {
086                party = getParty();
087            }
088        } else {
089            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
090        }
091    }
092
093    @Override
094    protected Long getTotalEntities() {
095        return hasExecutionErrors() ? null : workRequirementControl.countWorkAssignmentsByParty(party);
096    }
097
098    @Override
099    protected Collection<WorkAssignment> getEntities() {
100        return hasExecutionErrors() ? null : workRequirementControl.getWorkAssignmentsByParty(party);
101    }
102
103    @Override
104    protected BaseResult getResult(Collection<WorkAssignment> entities) {
105        var result = WorkRequirementResultFactory.getGetWorkAssignmentsResult();
106
107        if(entities != null) {
108            var userVisit = getUserVisit();
109
110            result.setParty(partyControl.getPartyTransfer(userVisit, party));
111
112            if(session.hasLimit(WorkAssignmentFactory.class)) {
113                result.setWorkAssignmentCount(getTotalEntities());
114            }
115
116            result.setWorkAssignments(workRequirementControl.getWorkAssignmentTransfers(userVisit, entities));
117        }
118
119        return result;
120    }
121    
122}