1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.leave.approval.web;
17
18 import java.math.BigDecimal;
19 import java.text.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.apache.commons.collections.CollectionUtils;
34 import org.apache.commons.lang.ObjectUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.commons.lang.math.NumberUtils;
37 import org.apache.commons.lang.time.DateUtils;
38 import org.apache.struts.action.ActionForm;
39 import org.apache.struts.action.ActionForward;
40 import org.apache.struts.action.ActionMapping;
41 import org.displaytag.tags.TableTagParameters;
42 import org.displaytag.util.ParamEncoder;
43 import org.hsqldb.lib.StringUtil;
44 import org.joda.time.LocalDate;
45 import org.json.simple.JSONValue;
46 import org.kuali.kpme.core.calendar.Calendar;
47 import org.kuali.kpme.core.calendar.entry.CalendarEntry;
48 import org.kuali.kpme.core.service.HrServiceLocator;
49 import org.kuali.kpme.core.util.HrConstants;
50 import org.kuali.kpme.core.util.HrContext;
51 import org.kuali.kpme.core.util.TKUtils;
52 import org.kuali.kpme.tklm.common.CalendarApprovalFormAction;
53 import org.kuali.kpme.tklm.leave.calendar.LeaveCalendarDocument;
54 import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
55 import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
56
57 public class LeaveApprovalAction extends CalendarApprovalFormAction {
58
59 @Override
60 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
61 ActionForward actionForward = super.execute(mapping, form, request, response);
62
63 LeaveApprovalActionForm leaveApprovalActionForm = (LeaveApprovalActionForm) form;
64 String documentId = leaveApprovalActionForm.getDocumentId();
65
66 setSearchFields(leaveApprovalActionForm);
67
68 CalendarEntry calendarEntry = null;
69 if (StringUtils.isNotBlank(documentId)) {
70 LeaveCalendarDocument leaveCalendarDocument = LmServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(documentId);
71
72 if (leaveCalendarDocument != null) {
73 calendarEntry = leaveCalendarDocument.getCalendarEntry();
74 leaveApprovalActionForm.setCalendarDocument(leaveCalendarDocument);
75 }
76 } else if (StringUtils.isNotBlank(leaveApprovalActionForm.getHrCalendarEntryId())) {
77 calendarEntry = HrServiceLocator.getCalendarEntryService().getCalendarEntry(leaveApprovalActionForm.getHrCalendarEntryId());
78 } else if (StringUtils.isNotBlank(leaveApprovalActionForm.getSelectedPayPeriod())) {
79 calendarEntry = HrServiceLocator.getCalendarEntryService().getCalendarEntry(leaveApprovalActionForm.getSelectedPayPeriod());
80 } else {
81 Calendar calendar = HrServiceLocator.getCalendarService().getCalendarByGroup(leaveApprovalActionForm.getSelectedPayCalendarGroup());
82 if (calendar != null) {
83 calendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarEntryByCalendarId(calendar.getHrCalendarId(), LocalDate.now().toDateTimeAtStartOfDay());
84 }
85 }
86
87 if (calendarEntry != null) {
88 leaveApprovalActionForm.setHrCalendarEntryId(calendarEntry.getHrCalendarEntryId());
89 leaveApprovalActionForm.setCalendarEntry(calendarEntry);
90 leaveApprovalActionForm.setBeginCalendarEntryDate(calendarEntry.getBeginPeriodDateTime());
91 leaveApprovalActionForm.setEndCalendarEntryDate(DateUtils.addMilliseconds(calendarEntry.getEndPeriodDateTime(), -1));
92
93 CalendarEntry prevCalendarEntry = HrServiceLocator.getCalendarEntryService().getPreviousCalendarEntryByCalendarId(calendarEntry.getHrCalendarId(), calendarEntry);
94 leaveApprovalActionForm.setPrevHrCalendarEntryId(prevCalendarEntry != null ? prevCalendarEntry.getHrCalendarEntryId() : null);
95
96 CalendarEntry nextCalendarEntry = HrServiceLocator.getCalendarEntryService().getNextCalendarEntryByCalendarId(calendarEntry.getHrCalendarId(), calendarEntry);
97 leaveApprovalActionForm.setNextHrCalendarEntryId(nextCalendarEntry != null ? nextCalendarEntry.getHrCalendarEntryId() : null);
98
99 setCalendarFields(leaveApprovalActionForm);
100
101 leaveApprovalActionForm.setLeaveCalendarDates(LmServiceLocator.getLeaveSummaryService().getLeaveSummaryDates(calendarEntry));
102
103 List<String> allPIdsList = getPrincipalIds(leaveApprovalActionForm);
104 List<String> pidList = new ArrayList<String>();
105 pidList.addAll(allPIdsList);
106
107 String docIdSearchTerm = "";
108 if(StringUtils.equals(leaveApprovalActionForm.getMethodToCall(), "searchResult") ) {
109 if(StringUtils.equals(leaveApprovalActionForm.getSearchField(), "principalName") ) {
110 if (StringUtils.isNotBlank(leaveApprovalActionForm.getSearchTerm())) {
111 String searchTerm = leaveApprovalActionForm.getSearchTerm();
112 pidList = new ArrayList<String>();
113 for(String anId : allPIdsList) {
114 if(anId.contains(searchTerm)) {
115 pidList.add(anId);
116 }
117 }
118 }
119 }
120
121 if(StringUtils.equals(leaveApprovalActionForm.getSearchField(), "documentId") )
122 docIdSearchTerm = leaveApprovalActionForm.getSearchTerm();
123 }
124
125 setApprovalTables(leaveApprovalActionForm, request, pidList, docIdSearchTerm);
126 }
127
128 return actionForward;
129 }
130
131 @Override
132 protected List<String> getCalendars(List<String> principalIds) {
133 return HrServiceLocator.getPrincipalHRAttributeService().getUniqueLeaveCalendars(principalIds);
134 }
135
136 public ActionForward selectNewPayCalendar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
137 LeaveApprovalActionForm leaveApprovalActionForm = (LeaveApprovalActionForm) form;
138 CalendarEntry calendarEntry = null;
139 leaveApprovalActionForm.setLeaveApprovalRows(new ArrayList<ApprovalLeaveSummaryRow>());
140 Calendar calendar = HrServiceLocator.getCalendarService().getCalendarByGroup(leaveApprovalActionForm.getSelectedPayCalendarGroup());
141
142 if (calendar != null) {
143 calendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarEntryByCalendarId(calendar.getHrCalendarId(), LocalDate.now().toDateTimeAtStartOfDay());
144 }
145
146 if (calendarEntry != null) {
147 leaveApprovalActionForm.setBeginCalendarEntryDate(calendarEntry.getBeginPeriodDateTime());
148 leaveApprovalActionForm.setEndCalendarEntryDate(DateUtils.addMilliseconds(calendarEntry.getEndPeriodDateTime(), -1));
149 leaveApprovalActionForm.setHrCalendarEntryId(calendarEntry.getHrCalendarEntryId());
150 leaveApprovalActionForm.setCalendarEntry(calendarEntry);
151
152 this.setCalendarFields(leaveApprovalActionForm);
153 }
154 return mapping.findForward("basic");
155 }
156
157 public ActionForward selectNewDept(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
158 LeaveApprovalActionForm leaveApprovalActionForm = (LeaveApprovalActionForm) form;
159
160 if (leaveApprovalActionForm.getCalendarEntry() != null) {
161 setApprovalTables(leaveApprovalActionForm, request, getPrincipalIds(leaveApprovalActionForm), "");
162 }
163 return mapping.findForward("basic");
164 }
165
166 public ActionForward selectNewWorkArea(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
167 LeaveApprovalActionForm leaveApprovalActionForm = (LeaveApprovalActionForm) form;
168 if (leaveApprovalActionForm.getCalendarEntry() != null) {
169 setApprovalTables(leaveApprovalActionForm, request, getPrincipalIds(leaveApprovalActionForm), "");
170 }
171 return mapping.findForward("basic");
172 }
173
174 public ActionForward searchResult(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
175 LeaveApprovalActionForm leaveApprovalActionForm = (LeaveApprovalActionForm) form;
176
177 if(StringUtils.isBlank(leaveApprovalActionForm.getSearchField())
178 && StringUtils.isNotBlank(request.getParameter("searchField"))) {
179 leaveApprovalActionForm.setSearchField(request.getParameter("searchField"));
180 }
181 if(StringUtils.isBlank(leaveApprovalActionForm.getSearchTerm())
182 && StringUtils.isNotBlank(request.getParameter("leaveSearchValue"))) {
183 leaveApprovalActionForm.setSearchTerm(request.getParameter("leaveSearchValue"));
184 }
185 if(StringUtils.isBlank(leaveApprovalActionForm.getSelectedPayPeriod())
186 && StringUtils.isNotBlank(request.getParameter("selectedPayPeriod"))) {
187 leaveApprovalActionForm.setSelectedPayPeriod(request.getParameter("selectedPayPeriod"));
188 }
189 return mapping.findForward("basic");
190 }
191
192 public ActionForward resetSearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
193 LeaveApprovalActionForm leaveApprovalActionForm = (LeaveApprovalActionForm) form;
194 leaveApprovalActionForm.setSearchField("");
195 leaveApprovalActionForm.setSearchTerm("");
196
197 return mapping.findForward("basic");
198 }
199
200 public ActionForward goToCurrentPeriod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
201 LeaveApprovalActionForm leaveApprovalActionForm = (LeaveApprovalActionForm) form;
202 leaveApprovalActionForm.setSearchField("");
203 leaveApprovalActionForm.setSearchTerm("");
204 leaveApprovalActionForm.setSelectedPayPeriod("");
205 leaveApprovalActionForm.setHrCalendarEntryId("");
206 return mapping.findForward("basic");
207 }
208
209 protected List<String> getPrincipalIds(LeaveApprovalActionForm leaveApprovalActionForm) {
210 List<String> workAreas = new ArrayList<String>();
211 if (StringUtil.isEmpty(leaveApprovalActionForm.getSelectedWorkArea())) {
212 for (Long workAreaKey : leaveApprovalActionForm.getWorkAreaDescr().keySet()) {
213 workAreas.add(workAreaKey.toString());
214 }
215 } else {
216 workAreas.add(leaveApprovalActionForm.getSelectedWorkArea());
217 }
218 String calendar = leaveApprovalActionForm.getSelectedPayCalendarGroup();
219 LocalDate endDate = leaveApprovalActionForm.getCalendarEntry().getEndPeriodFullDateTime().toLocalDate().minusDays(1);
220 LocalDate beginDate = leaveApprovalActionForm.getCalendarEntry().getBeginPeriodFullDateTime().toLocalDate();
221
222 return LmServiceLocator.getLeaveApprovalService().getLeavePrincipalIdsWithSearchCriteria(workAreas, calendar, endDate, beginDate, endDate);
223 }
224
225 private void setApprovalTables(LeaveApprovalActionForm leaveApprovalActionForm, HttpServletRequest request, List<String> principalIds, String docIdSearchTerm) {
226 if (principalIds.isEmpty()) {
227 leaveApprovalActionForm.setLeaveApprovalRows(new ArrayList<ApprovalLeaveSummaryRow>());
228 leaveApprovalActionForm.setResultSize(0);
229 } else {
230 List<ApprovalLeaveSummaryRow> approvalRows = getApprovalLeaveRows(leaveApprovalActionForm, principalIds, docIdSearchTerm);
231 String sortField = getSortField(request);
232 if (StringUtils.isEmpty(sortField) || StringUtils.equals(sortField, "name")) {
233 final boolean sortNameAscending = getAscending(request);
234 Collections.sort(approvalRows, new Comparator<ApprovalLeaveSummaryRow>() {
235 @Override
236 public int compare(ApprovalLeaveSummaryRow row1, ApprovalLeaveSummaryRow row2) {
237 if (sortNameAscending) {
238 return ObjectUtils.compare(StringUtils.lowerCase(row1.getName()), StringUtils.lowerCase(row2.getName()));
239 } else {
240 return ObjectUtils.compare(StringUtils.lowerCase(row2.getName()), StringUtils.lowerCase(row1.getName()));
241 }
242 }
243 });
244 } else if (StringUtils.equals(sortField, "documentID")) {
245 final boolean sortDocumentIdAscending = getAscending(request);
246 Collections.sort(approvalRows, new Comparator<ApprovalLeaveSummaryRow>() {
247 @Override
248 public int compare(ApprovalLeaveSummaryRow row1, ApprovalLeaveSummaryRow row2) {
249 if (sortDocumentIdAscending) {
250 return ObjectUtils.compare(NumberUtils.toInt(row1.getDocumentId()), NumberUtils.toInt(row2.getDocumentId()));
251 } else {
252 return ObjectUtils.compare(NumberUtils.toInt(row2.getDocumentId()), NumberUtils.toInt(row1.getDocumentId()));
253 }
254 }
255 });
256 } else if (StringUtils.equals(sortField, "status")) {
257 final boolean sortStatusIdAscending = getAscending(request);
258 Collections.sort(approvalRows, new Comparator<ApprovalLeaveSummaryRow>() {
259 @Override
260 public int compare(ApprovalLeaveSummaryRow row1, ApprovalLeaveSummaryRow row2) {
261 if (sortStatusIdAscending) {
262 return ObjectUtils.compare(StringUtils.lowerCase(row1.getApprovalStatus()), StringUtils.lowerCase(row2.getApprovalStatus()));
263 } else {
264 return ObjectUtils.compare(StringUtils.lowerCase(row2.getApprovalStatus()), StringUtils.lowerCase(row1.getApprovalStatus()));
265 }
266 }
267 });
268 }
269
270 String page = request.getParameter((new ParamEncoder(HrConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_PAGE)));
271 Integer beginIndex = StringUtils.isBlank(page) || StringUtils.equals(page, "1") ? 0 : (Integer.parseInt(page) - 1) * leaveApprovalActionForm.getPageSize();
272 Integer endIndex = beginIndex + leaveApprovalActionForm.getPageSize() > approvalRows.size() ? approvalRows.size() : beginIndex + leaveApprovalActionForm.getPageSize();
273
274 List<ApprovalLeaveSummaryRow> sublist = new ArrayList<ApprovalLeaveSummaryRow>();
275 sublist.addAll(approvalRows.subList(beginIndex, endIndex));
276 leaveApprovalActionForm.setLeaveApprovalRows(sublist);
277 leaveApprovalActionForm.setResultSize(approvalRows.size());
278
279 Map<String, String> userColorMap = new HashMap<String, String>();
280 Set<String> randomColors = new HashSet<String>();
281 List<Map<String, String>> approvalRowsMap = new ArrayList<Map<String, String>>();
282 if(CollectionUtils.isNotEmpty(approvalRows)) {
283 for (ApprovalLeaveSummaryRow row : approvalRows) {
284 for (Date date : leaveApprovalActionForm.getLeaveCalendarDates()) {
285 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
286 String dateString = formatter.format(date);
287 Map<String, BigDecimal> earnCodeMap = row.getEarnCodeLeaveHours().get(date);
288 if(earnCodeMap != null && !earnCodeMap.isEmpty()) {
289 for (String key : earnCodeMap.keySet()){
290 String color = null;
291
292 Map<String, String> event = new HashMap<String, String>();
293 event.put("title", (key.split("[|]"))[0] + "-" +earnCodeMap.get(key).toString());
294 event.put("start", dateString);
295
296 if(!userColorMap.containsKey(row.getPrincipalId())) {
297 color = TKUtils.getRandomColor(randomColors);
298 randomColors.add(color);
299 userColorMap.put(row.getPrincipalId(), color);
300 }
301 row.setColor(userColorMap.get(row.getPrincipalId()));
302 event.put("color", userColorMap.get(row.getPrincipalId()));
303 event.put("className", "event-approval");
304 approvalRowsMap.add(event);
305 }
306 }
307 }
308 }
309 }
310
311 leaveApprovalActionForm.setOutputString(JSONValue.toJSONString(approvalRowsMap));
312 }
313 }
314
315 protected List<ApprovalLeaveSummaryRow> getApprovalLeaveRows(LeaveApprovalActionForm leaveApprovalActionForm, List<String> assignmentPrincipalIds, String docIdSearchTerm) {
316 return LmServiceLocator.getLeaveApprovalService().getLeaveApprovalSummaryRows(assignmentPrincipalIds, leaveApprovalActionForm.getCalendarEntry(), leaveApprovalActionForm.getLeaveCalendarDates(), docIdSearchTerm);
317 }
318
319 public ActionForward approve(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
320 LeaveApprovalActionForm laaf = (LeaveApprovalActionForm) form;
321
322 List<ApprovalLeaveSummaryRow> lstLeaveRows = laaf.getLeaveApprovalRows();
323 for (ApprovalLeaveSummaryRow ar : lstLeaveRows) {
324 if (ar.isApprovable() && StringUtils.equals(ar.getSelected(), "on")) {
325 String documentNumber = ar.getDocumentId();
326 LeaveCalendarDocument lcd = LmServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(documentNumber);
327 LmServiceLocator.getLeaveCalendarService().approveLeaveCalendar(HrContext.getPrincipalId(), lcd);
328 }
329 }
330
331 return mapping.findForward("basic");
332 }
333
334 }