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.employee.server.control.EmployeeControl;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.control.workrequirement.server.control.WorkRequirementControl;
024import com.echothree.model.data.party.server.entity.Party;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.model.data.workrequirement.server.factory.WorkAssignmentFactory;
027import com.echothree.util.common.message.ExecutionErrors;
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.List;
034import javax.enterprise.context.Dependent;
035
036@Dependent
037public class GetWorkAssignmentsCommand
038        extends BaseSimpleCommand<GetWorkAssignmentsForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = List.of(
044                new FieldDefinition("EmployeeName", FieldType.ENTITY_NAME, false, null, null),
045                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null)
046                );
047    }
048    
049    /** Creates a new instance of GetWorkAssignmentsCommand */
050    public GetWorkAssignmentsCommand() {
051        super(null, FORM_FIELD_DEFINITIONS, true);
052    }
053    
054    @Override
055    protected BaseResult execute() {
056        var partyControl = Session.getModelController(PartyControl.class);
057        var result = WorkRequirementResultFactory.getGetWorkAssignmentsResult();
058        var employeeName = form.getEmployeeName();
059        var partyName = form.getPartyName();
060        var parameterCount = (employeeName == null ? 0 : 1) + (partyName == null ? 0 : 1);
061
062        if(parameterCount < 2) {
063            Party party = null;
064
065            if(employeeName != null) {
066                var employeeControl = Session.getModelController(EmployeeControl.class);
067                var partyEmployee = employeeControl.getPartyEmployeeByName(employeeName);
068
069                if(partyEmployee != null) {
070                    party = partyEmployee.getParty();
071                } else {
072                    addExecutionError(ExecutionErrors.UnknownEmployeeName.name(), employeeName);
073                }
074            } else if(partyName != null) {
075                party = partyControl.getPartyByName(partyName);
076
077                if(party == null) {
078                    addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
079                }
080            } else {
081                party = getParty();
082            }
083
084            if(!hasExecutionErrors()) {
085                var workRequirementControl = Session.getModelController(WorkRequirementControl.class);
086                var userVisit = getUserVisit();
087
088                if(session.hasLimit(WorkAssignmentFactory.class)) {
089                    result.setWorkAssignmentCount(workRequirementControl.countWorkAssignmentsByParty(party));
090                }
091
092                result.setParty(partyControl.getPartyTransfer(userVisit, party));
093                result.setWorkAssignments(workRequirementControl.getWorkAssignmentTransfersByParty(userVisit, party));
094            }
095        } else {
096            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
097        }
098        
099        return result;
100    }
101    
102}