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