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.employee.server.logic; 018 019import com.echothree.model.control.core.server.control.EntityInstanceControl; 020import com.echothree.model.control.employee.common.exception.InvalidLeaveStatusException; 021import com.echothree.model.control.employee.common.exception.UnknownLeaveReasonNameException; 022import com.echothree.model.control.employee.common.exception.UnknownLeaveTypeNameException; 023import com.echothree.model.control.employee.common.workflow.LeaveStatusConstants; 024import com.echothree.model.control.employee.server.control.EmployeeControl; 025import com.echothree.model.control.workflow.server.control.WorkflowControl; 026import com.echothree.model.control.workflow.server.logic.WorkflowLogic; 027import com.echothree.model.data.core.server.entity.EntityInstance; 028import com.echothree.model.data.employee.server.entity.Leave; 029import com.echothree.model.data.employee.server.entity.LeaveReason; 030import com.echothree.model.data.employee.server.entity.LeaveType; 031import com.echothree.model.data.employee.server.value.LeaveDetailValue; 032import com.echothree.model.data.party.common.pk.PartyPK; 033import com.echothree.model.data.party.server.entity.Party; 034import com.echothree.model.data.workflow.server.entity.WorkflowEntrance; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.persistence.BasePK; 037import com.echothree.util.server.control.BaseLogic; 038import com.echothree.util.server.message.ExecutionErrorAccumulator; 039import com.echothree.util.server.persistence.Session; 040import javax.enterprise.context.ApplicationScoped; 041import javax.inject.Inject; 042 043@ApplicationScoped 044public class LeaveLogic 045 extends BaseLogic { 046 047 @Inject 048 EmployeeControl employeeControl; 049 050 @Inject 051 EntityInstanceControl entityInstanceControl; 052 053 @Inject 054 WorkflowControl workflowControl; 055 056 @Inject 057 WorkflowLogic workflowLogic; 058 059 protected LeaveLogic() { 060 super(); 061 } 062 063 public LeaveType getLeaveTypeByName(final ExecutionErrorAccumulator eea, final String leaveTypeName) { 064 var leaveType = employeeControl.getLeaveTypeByName(leaveTypeName); 065 066 if(leaveType == null) { 067 handleExecutionError(UnknownLeaveTypeNameException.class, eea, ExecutionErrors.UnknownLeaveTypeName.name(), leaveTypeName); 068 } 069 070 return leaveType; 071 } 072 073 public LeaveReason getLeaveReasonByName(final ExecutionErrorAccumulator eea, final String leaveReasonName) { 074 var leaveReason = employeeControl.getLeaveReasonByName(leaveReasonName); 075 076 if(leaveReason == null) { 077 handleExecutionError(UnknownLeaveReasonNameException.class, eea, ExecutionErrors.UnknownLeaveReasonName.name(), leaveReasonName); 078 } 079 080 return leaveReason; 081 } 082 083 private void insertLeaveIntoWorkflow(final EntityInstance entityInstance, final WorkflowEntrance leaveStatus, final PartyPK createdBy) { 084 if(leaveStatus == null) { 085 workflowControl.addEntityToWorkflowUsingNames(null, LeaveStatusConstants.Workflow_LEAVE_STATUS, LeaveStatusConstants.WorkflowEntrance_NEW_SUBMITTED, 086 entityInstance, null, null, createdBy); 087 } else { 088 workflowControl.addEntityToWorkflow(leaveStatus, entityInstance, null, null, createdBy); 089 } 090 } 091 092 public Leave createLeave(final Session session, final Party party, final Party companyParty, final LeaveType leaveType, final LeaveReason leaveReason, 093 final Long startTime, final Long endTime, final Long totalTime, final WorkflowEntrance leaveStatus, final PartyPK createdBy) { 094 var leave = employeeControl.createLeave(party, companyParty, leaveType, leaveReason, startTime, endTime, totalTime, createdBy); 095 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(leave.getPrimaryKey()); 096 097 insertLeaveIntoWorkflow(entityInstance, leaveStatus, createdBy); 098 099 return leave; 100 } 101 102 public void updateLeaveFromValue(final ExecutionErrorAccumulator eea, final LeaveDetailValue leaveDetailValue, final PartyPK updatedBy) { 103 var entityInstance = getEntityInstanceByBasePK(leaveDetailValue.getLeavePK()); 104 var workflow = workflowLogic.getWorkflowByName(null, LeaveStatusConstants.Workflow_LEAVE_STATUS); 105 var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance); 106 var workflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName(); 107 String workflowDestinationName = null; 108 109 if(workflowStepName.equals(LeaveStatusConstants.WorkflowStep_APPROVED)) { 110 workflowDestinationName = LeaveStatusConstants.WorkflowDestination_APPROVED_TO_REVISED; 111 } else if(workflowStepName.equals(LeaveStatusConstants.WorkflowStep_DENIED)) { 112 workflowDestinationName = LeaveStatusConstants.WorkflowDestination_DENIED_TO_REVISED; 113 } 114 115 if(workflowDestinationName == null && !workflowStepName.equals(LeaveStatusConstants.WorkflowStep_SUBMITTED)) { 116 handleExecutionError(InvalidLeaveStatusException.class, eea, ExecutionErrors.InvalidLeaveStatus.name(), leaveDetailValue.getLeaveName(), workflowStepName); 117 } else { 118 if(workflowDestinationName != null) { 119 workflowControl.transitionEntityInWorkflowUsingNames(eea, workflowEntityStatus, workflowDestinationName, null, updatedBy); 120 } 121 122 employeeControl.updateLeaveFromValue(leaveDetailValue, updatedBy); 123 } 124 } 125 126 public void deleteLeave(Leave leave, final BasePK deleteBy) { 127 employeeControl.deleteLeave(leave, deleteBy); 128 } 129 130}