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