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.getCurrentTargetPersonId();
50 Date currentDate = TKUtils.getTimelessDate(null);
51
52 Calendar currentCalendar = Calendar.getInstance();
53 if (leaveForm.getNavString() == null) {
54 leaveForm.setYear(currentCalendar.get(Calendar.YEAR));
55 } else if(leaveForm.getNavString().equals("NEXT")) {
56 leaveForm.setYear(leaveForm.getYear() + 1);
57 } else if(leaveForm.getNavString().equals("PREV")) {
58 leaveForm.setYear(leaveForm.getYear() - 1);
59 }
60 currentCalendar.set(leaveForm.getYear(), 0, 1);
61
62 java.util.Date beginDate = TKUtils.getTimelessDate(currentCalendar.getTime());
63 currentCalendar.set(leaveForm.getYear(), 11, 31);
64 java.util.Date endDate = TKUtils.getTimelessDate(currentCalendar.getTime());
65
66
67 CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDatesForLeaveCalendar(principalId, beginDate, endDate);
68
69
70
71
72
73 CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(principalId, beginDate, endDate);
74 Boolean checkLeaveEligible = true;
75 Boolean nonExemptLeaveEligible = TkServiceLocator.getLeaveApprovalService().isActiveAssignmentFoundOnJobFlsaStatus(principalId, TkConstants.FLSA_STATUS_NON_EXEMPT,checkLeaveEligible);
76 if(nonExemptLeaveEligible && calendarEntry != null && payCalendarEntry != null) {
77 if ( payCalendarEntry.getEndPeriodDate().before(calendarEntry.getEndPeriodDate()) ) {
78 calendarEntry = payCalendarEntry;
79 }
80 }
81
82 if(calendarEntry != null) {
83 if(calendarEntry.getEndLocalDateTime().getMillisOfDay() == 0) {
84
85 currentDate = new java.sql.Date(TKUtils.addDates(calendarEntry.getEndPeriodDate(), -1).getTime());
86 } else {
87 currentDate = calendarEntry.getEndPeriodDate();
88 }
89 }
90 List<LeaveBlock> plannedLeaves = getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, LMConstants.REQUEST_STATUS.PLANNED);
91 plannedLeaves.addAll(getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, LMConstants.REQUEST_STATUS.DEFERRED));
92 leaveForm.setPlannedLeaves(plannedLeaves);
93 leaveForm.setPendingLeaves(getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, LMConstants.REQUEST_STATUS.REQUESTED));
94 leaveForm.setApprovedLeaves(getLeaveBlocksWithRequestStatus(principalId, beginDate, endDate, LMConstants.REQUEST_STATUS.APPROVED));
95 leaveForm.setDisapprovedLeaves(getDisapprovedLeaveBlockHistory(principalId, currentDate));
96
97 leaveForm.setDocuments(getLeaveRequestDocuments(leaveForm));
98 return forward;
99 }
100
101
102 private List<LeaveBlock> getLeaveBlocksWithRequestStatus(String principalId, java.util.Date beginDate, java.util.Date endDate, String requestStatus) {
103 List<LeaveBlock> plannedLeaves = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principalId, LMConstants.LEAVE_BLOCK_TYPE.LEAVE_CALENDAR, requestStatus, beginDate, endDate);
104
105 Collections.sort(plannedLeaves, new Comparator<LeaveBlock>() {
106 @Override
107 public int compare(LeaveBlock leaveBlock1, LeaveBlock leaveBlock2) {
108 return ObjectUtils.compare(leaveBlock1.getLeaveDate(), leaveBlock2.getLeaveDate());
109 }
110 });
111
112 return plannedLeaves;
113 }
114
115 private List<LeaveBlockHistory> getDisapprovedLeaveBlockHistory(String principalId, Date currentDate) {
116 List<LeaveBlockHistory> historyList = TkServiceLocator.getLeaveBlockHistoryService()
117 .getLeaveBlockHistories(principalId, LMConstants.REQUEST_STATUS.DISAPPROVED, LMConstants.ACTION.DELETE, currentDate);
118
119 Collections.sort(historyList, new Comparator<LeaveBlockHistory>() {
120 @Override
121 public int compare(LeaveBlockHistory lbh1, LeaveBlockHistory lbh2) {
122 return ObjectUtils.compare(lbh1.getLeaveDate(), lbh2.getLeaveDate());
123 }
124 });
125
126 return historyList;
127 }
128
129
130 public ActionForward submitForApproval(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
131 LeaveRequestForm lf = (LeaveRequestForm) form;
132 for(LeaveBlock leaveBlock : lf.getPlannedLeaves()) {
133
134 if(leaveBlock.getSubmit()) {
135 LeaveRequestDocument lrd = TkServiceLocator.getLeaveRequestDocumentService().createLeaveRequestDocument(leaveBlock.getLmLeaveBlockId());
136 TkServiceLocator.getLeaveRequestDocumentService().requestLeave(lrd.getDocumentNumber());
137 }
138 }
139 return mapping.findForward("basic");
140 }
141
142 private Map<String, LeaveRequestDocument> getLeaveRequestDocuments(LeaveRequestForm form) {
143 Map<String, LeaveRequestDocument> docs = new HashMap<String, LeaveRequestDocument>();
144
145 if (form == null) {
146 return docs;
147 }
148
149 for (LeaveBlock leaveBlock : form.getPendingLeaves()) {
150 if(leaveBlock.getLeaveRequestDocumentId() != null && !leaveBlock.getLeaveRequestDocumentId().isEmpty()){
151 docs.put(leaveBlock.getLmLeaveBlockId(), getLeaveRequestDocumentService().getLeaveRequestDocument(leaveBlock.getLeaveRequestDocumentId()));
152 }
153 }
154 for (LeaveBlock leaveBlock : form.getApprovedLeaves()) {
155 if(leaveBlock.getLeaveRequestDocumentId() != null && !leaveBlock.getLeaveRequestDocumentId().isEmpty()){
156 docs.put(leaveBlock.getLmLeaveBlockId(), getLeaveRequestDocumentService().getLeaveRequestDocument(leaveBlock.getLeaveRequestDocumentId()));
157 }
158 }
159 for (LeaveBlockHistory lbh : form.getDisapprovedLeaves()) {
160 List<LeaveRequestDocument> docList = getLeaveRequestDocumentService().getLeaveRequestDocumentsByLeaveBlockId(lbh.getLmLeaveBlockId());
161 for(LeaveRequestDocument lrd : docList) {
162 if(lrd.getDocumentNumber() != null && !lrd.getDocumentNumber().isEmpty()){
163 DocumentStatus status = KewApiServiceLocator.getWorkflowDocumentService().getDocumentStatus(lrd.getDocumentNumber());
164 if(status != null && DocumentStatus.DISAPPROVED.getCode().equals(status.getCode())) {
165
166 /
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185