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.model.control.workrequirement.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.party.server.control.PartyControl;
021import com.echothree.model.control.workflow.server.control.WorkflowControl;
022import com.echothree.model.control.workrequirement.common.transfer.WorkAssignmentTransfer;
023import com.echothree.model.control.workrequirement.common.workflow.WorkAssignmentStatusConstants;
024import com.echothree.model.control.workrequirement.server.control.WorkRequirementControl;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import com.echothree.model.data.workrequirement.server.entity.WorkAssignment;
027import javax.enterprise.context.RequestScoped;
028
029@RequestScoped
030public class WorkAssignmentTransferCache
031        extends BaseWorkRequirementTransferCache<WorkAssignment, WorkAssignmentTransfer> {
032
033    @Inject
034    PartyControl partyControl;
035
036    @Inject
037    WorkRequirementControl workRequirementControl;
038
039    @Inject
040    WorkflowControl workflowControl;
041
042    /** Creates a new instance of WorkAssignmentTransferCache */
043    protected WorkAssignmentTransferCache() {
044        super();
045        
046        setIncludeEntityInstance(true);
047    }
048    
049    public WorkAssignmentTransfer getWorkAssignmentTransfer(UserVisit userVisit, WorkAssignment workAssignment) {
050        var workAssignmentTransfer = get(workAssignment);
051        
052        if(workAssignmentTransfer == null) {
053            var workAssignmentDetail = workAssignment.getLastDetail();
054            var workRequirement = workRequirementControl.getWorkRequirementTransfer(userVisit, workAssignmentDetail.getWorkRequirement());
055            var workAssignmentSequence = workAssignmentDetail.getWorkAssignmentSequence();
056            var party = partyControl.getPartyTransfer(userVisit, workAssignmentDetail.getParty());
057            var unformattedStartTime = workAssignmentDetail.getStartTime();
058            var startTime = formatTypicalDateTime(userVisit, unformattedStartTime);
059            var unformattedEndTime = workAssignmentDetail.getEndTime();
060            var endTime = formatTypicalDateTime(userVisit, unformattedEndTime);
061
062            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(workAssignment.getPrimaryKey());
063            var workAssignmentStatus = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
064                    WorkAssignmentStatusConstants.Workflow_WORK_ASSIGNMENT_STATUS, entityInstance);
065
066            workAssignmentTransfer = new WorkAssignmentTransfer(workRequirement, workAssignmentSequence, party, unformattedStartTime, startTime,
067                    unformattedEndTime, endTime, workAssignmentStatus);
068            put(userVisit, workAssignment, workAssignmentTransfer);
069        }
070        
071        return workAssignmentTransfer;
072    }
073    
074}