1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.lm.leave.web;
17
18 import edu.emory.mathcs.backport.java.util.Collections;
19 import org.apache.commons.lang3.ObjectUtils;
20 import org.apache.struts.action.ActionForm;
21 import org.apache.struts.action.ActionForward;
22 import org.apache.struts.action.ActionMapping;
23 import org.kuali.hr.lm.LMConstants;
24 import org.kuali.hr.lm.leaveblock.LeaveBlock;
25 import org.kuali.hr.lm.leaveblock.LeaveBlockHistory;
26 import org.kuali.hr.lm.leaverequest.service.LeaveRequestDocumentService;
27 import org.kuali.hr.lm.workflow.LeaveRequestDocument;
28 import org.kuali.hr.time.base.web.TkAction;
29 import org.kuali.hr.time.calendar.CalendarEntries;
30 import org.kuali.hr.time.service.base.TkServiceLocator;
31 import org.kuali.hr.time.util.TKUser;
32 import org.kuali.hr.time.util.TKUtils;
33 import org.kuali.hr.time.util.TkConstants;
34 import org.kuali.rice.kew.api.KewApiServiceLocator;
35 import org.kuali.rice.kew.api.document.DocumentStatus;
36
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import java.sql.Date;
40 import java.util.*;
41
42 public class LeaveRequestAction extends TkAction {
43 LeaveRequestDocumentService leaveRequestDocumentService;
44
45 @Override
46 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
47 ActionForward forward = super.execute(mapping, form, request, response);
48 LeaveRequestForm leaveForm = (LeaveRequestForm) form;
49 String principalId = TKUser.getCurrentTargetPerson().getPrincipalId();
50 Date currentDate = TKUtils.getTimelessDate(null);
51
52 CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDatesForLeaveCalendar(principalId, currentDate);
53
54
55
56
57
58 CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(principalId, currentDate);
59 Boolean checkLeaveEligible = true;
60 Boolean nonExemptLeaveEligible = TkServiceLocator.getLeaveApprovalService().isActiveAssignmentFoundOnJobFlsaStatus(principalId, TkConstants.FLSA_STATUS_NON_EXEMPT,checkLeaveEligible);
61 if(nonExemptLeaveEligible && calendarEntry != null && payCalendarEntry != null) {
62 if ( payCalendarEntry.getEndPeriodDate().before(calendarEntry.getEndPeriodDate()) ) {
63 calendarEntry = payCalendarEntry;
64 }
65 }
66
67 if(calendarEntry != null) {
68 if(calendarEntry.getEndLocalDateTime().getMillisOfDay() == 0) {
69
70 currentDate = new java.sql.Date(TKUtils.addDates(calendarEntry.getEndPeriodDate(), -1).getTime());
71 } else {
72 currentDate = calendarEntry.getEndPeriodDate();
73 }
74 }
75 List<LeaveBlock> plannedLeaves = getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.PLANNED);
76 plannedLeaves.addAll(getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.DEFERRED));
77 leaveForm.setPlannedLeaves(plannedLeaves);
78 leaveForm.setPendingLeaves(getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.REQUESTED));
79 leaveForm.setApprovedLeaves(getLeaveBlocksWithRequestStatus(principalId, currentDate, LMConstants.REQUEST_STATUS.APPROVED));
80 leaveForm.setDisapprovedLeaves(getDisapprovedLeaveBlockHistory(principalId, currentDate));
81
82 leaveForm.setDocuments(getLeaveRequestDocuments(leaveForm));
83 return forward;
84 }
85
86
87 private List<LeaveBlock> getLeaveBlocksWithRequestStatus(String principalId, Date currentDate, String requestStatus) {
88 List<LeaveBlock> plannedLeaves = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principalId, LMConstants.LEAVE_BLOCK_TYPE.LEAVE_CALENDAR, requestStatus, currentDate);
89
90 Collections.sort(plannedLeaves, new Comparator<LeaveBlock>() {
91 @Override
92 public int compare(LeaveBlock leaveBlock1, LeaveBlock leaveBlock2) {
93 return ObjectUtils.compare(leaveBlock1.getLeaveDate(), leaveBlock2.getLeaveDate());
94 }
95 });
96
97 return plannedLeaves;
98 }
99
100 private List<LeaveBlockHistory> getDisapprovedLeaveBlockHistory(String principalId, Date currentDate) {
101 List<LeaveBlockHistory> historyList = TkServiceLocator.getLeaveBlockHistoryService()
102 .getLeaveBlockHistories(principalId, LMConstants.REQUEST_STATUS.DISAPPROVED, LMConstants.ACTION.DELETE, currentDate);
103
104 Collections.sort(historyList, new Comparator<LeaveBlockHistory>() {
105 @Override
106 public int compare(LeaveBlockHistory lbh1, LeaveBlockHistory lbh2) {
107 return ObjectUtils.compare(lbh1.getLeaveDate(), lbh2.getLeaveDate());
108 }
109 });
110
111 return historyList;
112 }
113
114
115 public ActionForward submitForApproval(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
116 LeaveRequestForm lf = (LeaveRequestForm) form;
117 for(LeaveBlock leaveBlock : lf.getPlannedLeaves()) {
118
119
120 if(leaveBlock.getSubmit()) {
121 LeaveRequestDocument lrd = TkServiceLocator.getLeaveRequestDocumentService().createLeaveRequestDocument(leaveBlock.getLmLeaveBlockId());
122 TkServiceLocator.getLeaveRequestDocumentService().requestLeave(lrd.getDocumentNumber());
123 }
124 }
125 return mapping.findForward("basic");
126 }
127
128 private Map<String, LeaveRequestDocument> getLeaveRequestDocuments(LeaveRequestForm form) {
129 Map<String, LeaveRequestDocument> docs = new HashMap<String, LeaveRequestDocument>();
130
131 if (form == null) {
132 return docs;
133 }
134
135 for (LeaveBlock leaveBlock : form.getPendingLeaves()) {
136 docs.put(leaveBlock.getLmLeaveBlockId(), getLeaveRequestDocumentService().getLeaveRequestDocument(leaveBlock.getLeaveRequestDocumentId()));
137 }
138 for (LeaveBlock leaveBlock : form.getApprovedLeaves()) {
139 docs.put(leaveBlock.getLmLeaveBlockId(), getLeaveRequestDocumentService().getLeaveRequestDocument(leaveBlock.getLeaveRequestDocumentId()));
140 }
141 for (LeaveBlockHistory lbh : form.getDisapprovedLeaves()) {
142 List<LeaveRequestDocument> docList = getLeaveRequestDocumentService().getLeaveRequestDocumentsByLeaveBlockId(lbh.getLmLeaveBlockId());
143 for(LeaveRequestDocument lrd : docList) {
144 DocumentStatus status = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(lrd.getDocumentNumber());
145 if(status != null && DocumentStatus.DISAPPROVED.getCode().equals(status.getCode())) {
146 docs.put(lbh.getLmLeaveBlockId(), getLeaveRequestDocumentService().getLeaveRequestDocument(lrd.getDocumentNumber()));
147 break;
148 }
149 }
150
151 }
152 return docs;
153 }
154
155 private LeaveRequestDocumentService getLeaveRequestDocumentService() {
156 if (leaveRequestDocumentService == null) {
157 leaveRequestDocumentService = TkServiceLocator.getLeaveRequestDocumentService();
158 }
159 return leaveRequestDocumentService;
160 }
161
162 }