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.model.control.employee.server.transfer;
018
019import com.echothree.model.control.core.server.control.CoreControl;
020import com.echothree.model.control.employee.common.transfer.LeaveReasonTransfer;
021import com.echothree.model.control.employee.common.transfer.LeaveTransfer;
022import com.echothree.model.control.employee.common.transfer.LeaveTypeTransfer;
023import com.echothree.model.control.employee.server.control.EmployeeControl;
024import com.echothree.model.control.party.common.transfer.CompanyTransfer;
025import com.echothree.model.control.party.common.transfer.PartyTransfer;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.uom.common.UomConstants;
028import com.echothree.model.control.uom.server.control.UomControl;
029import com.echothree.model.control.employee.common.workflow.LeaveStatusConstants;
030import com.echothree.model.control.workflow.common.transfer.WorkflowEntityStatusTransfer;
031import com.echothree.model.control.workflow.server.control.WorkflowControl;
032import com.echothree.model.data.core.server.entity.EntityInstance;
033import com.echothree.model.data.employee.server.entity.Leave;
034import com.echothree.model.data.employee.server.entity.LeaveDetail;
035import com.echothree.model.data.employee.server.entity.LeaveReason;
036import com.echothree.model.data.employee.server.entity.LeaveType;
037import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind;
038import com.echothree.model.data.user.server.entity.UserVisit;
039import com.echothree.util.server.persistence.Session;
040import com.echothree.util.server.string.UnitOfMeasureUtils;
041
042public class LeaveTransferCache
043        extends BaseEmployeeTransferCache<Leave, LeaveTransfer> {
044    
045    CoreControl coreControl = Session.getModelController(CoreControl.class);
046    PartyControl partyControl = Session.getModelController(PartyControl.class);
047    UomControl uomControl = Session.getModelController(UomControl.class);
048    WorkflowControl workflowControl = Session.getModelController(WorkflowControl.class);
049
050    UnitOfMeasureKind timeUnitOfMeasureKind;
051    UnitOfMeasureUtils unitOfMeasureUtils;
052
053    /** Creates a new instance of LeaveTransferCache */
054    public LeaveTransferCache(UserVisit userVisit, EmployeeControl employeeControl) {
055        super(userVisit, employeeControl);
056        
057        setIncludeEntityInstance(true);
058        
059        timeUnitOfMeasureKind = uomControl.getUnitOfMeasureKindByUnitOfMeasureKindUseTypeUsingNames(UomConstants.UnitOfMeasureKindUseType_TIME);
060        unitOfMeasureUtils = UnitOfMeasureUtils.getInstance();
061    }
062    
063    public LeaveTransfer getLeaveTransfer(Leave leave) {
064        LeaveTransfer leaveTransfer = get(leave);
065        
066        if(leaveTransfer == null) {
067            LeaveDetail leaveDetail = leave.getLastDetail();
068            String leaveName = leaveDetail.getLeaveName();
069            PartyTransfer partyTransfer = partyControl.getPartyTransfer(userVisit, leaveDetail.getParty());
070            CompanyTransfer companyTransfer = partyControl.getCompanyTransfer(userVisit, leaveDetail.getCompanyParty());
071            LeaveType leaveType = leaveDetail.getLeaveType();
072            LeaveTypeTransfer leaveTypeTransfer = leaveType == null ? null : employeeControl.getLeaveTypeTransfer(userVisit, leaveType);
073            LeaveReason leaveReason = leaveDetail.getLeaveReason();
074            LeaveReasonTransfer leaveReasonTransfer = leaveReason == null ? null : employeeControl.getLeaveReasonTransfer(userVisit, leaveReason);
075            Long unformattedStartTime = leaveDetail.getStartTime();
076            String startTime = formatTypicalDateTime(unformattedStartTime);
077            Long unformattedEndTime = leaveDetail.getEndTime();
078            String endTime = formatTypicalDateTime(unformattedEndTime);
079            Long unformattedTotalTime = leaveDetail.getTotalTime();
080            String totalTime = unformattedTotalTime == null ? null : unitOfMeasureUtils.formatUnitOfMeasure(userVisit, timeUnitOfMeasureKind, unformattedTotalTime);
081            
082            EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(leave.getPrimaryKey());
083            WorkflowEntityStatusTransfer leaveStatus = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
084                    LeaveStatusConstants.Workflow_LEAVE_STATUS, entityInstance);
085
086            leaveTransfer = new LeaveTransfer(leaveName, partyTransfer, companyTransfer, leaveTypeTransfer, leaveReasonTransfer, unformattedStartTime,
087                    startTime, unformattedEndTime, endTime, unformattedTotalTime, totalTime, leaveStatus);
088            put(leave, leaveTransfer);
089        }
090        
091        return leaveTransfer;
092    }
093    
094}