1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.leave.batch;
17
18 import org.apache.commons.lang.SystemUtils;
19 import org.apache.commons.lang.time.DateUtils;
20 import org.joda.time.DateTime;
21 import org.joda.time.LocalDate;
22 import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
23 import org.kuali.kpme.core.api.calendar.entry.service.CalendarEntryService;
24 import org.kuali.kpme.core.api.principal.PrincipalHRAttributes;
25 import org.kuali.kpme.core.api.principal.service.PrincipalHRAttributesService;
26 import org.kuali.kpme.core.batch.BatchJob;
27 import org.kuali.kpme.core.service.notification.KPMENotificationService;
28 import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
29 import org.kuali.kpme.tklm.leave.workflow.service.LeaveCalendarDocumentHeaderService;
30 import org.quartz.JobExecutionContext;
31 import org.quartz.JobExecutionException;
32
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
36
37 public class LeaveCalendarDelinquencyJob extends BatchJob {
38
39 private static int CALENDAR_ENTRIES_POLLING_WINDOW;
40
41 private static CalendarEntryService CALENDAR_ENTRY_SERVICE;
42 private static KPMENotificationService KPME_NOTIFICATION_SERVICE;
43 private static LeaveCalendarDocumentHeaderService LEAVE_CALENDAR_DOCUMENT_HEADER_SERVICE;
44 private static PrincipalHRAttributesService PRINCIPAL_HR_ATTRIBUTES_SERVICE;
45
46 @Override
47 public void execute(JobExecutionContext context) throws JobExecutionException {
48 Set<String> principalIds = new HashSet<String>();
49
50 DateTime asOfDate = new LocalDate().toDateTimeAtStartOfDay();
51 List<CalendarEntry> calendarEntries = getCalendarEntryService().getCurrentCalendarEntriesNeedsScheduled(getCalendarEntriesPollingWindow(), asOfDate);
52
53 for (CalendarEntry calendarEntry : calendarEntries) {
54 String hrCalendarId = calendarEntry.getHrCalendarId();
55 DateTime currentBeginDate = calendarEntry.getBeginPeriodFullDateTime();
56
57 if (currentBeginDate.isBefore(asOfDate) || DateUtils.isSameDay(currentBeginDate.toDate(), asOfDate.toDate())) {
58 CalendarEntry previousCalendarEntry = getCalendarEntryService().getPreviousCalendarEntryByCalendarId(hrCalendarId, calendarEntry);
59
60 if (previousCalendarEntry != null) {
61 String calendarName = previousCalendarEntry.getCalendarName();
62 LocalDate previousBeginDate = previousCalendarEntry.getBeginPeriodFullDateTime().toLocalDate();
63 List<PrincipalHRAttributes> principalHRAttributes = getPrincipalHRAttributesService().getActiveEmployeesForLeaveCalendar(calendarName, previousBeginDate);
64
65 for (PrincipalHRAttributes principalHRAttribute : principalHRAttributes) {
66 String principalId = principalHRAttribute.getPrincipalId();
67
68 if (!principalIds.contains(principalId)) {
69 List<LeaveCalendarDocumentHeader> leaveCalendarDocumentHeaders = getLeaveCalendarDocumentHeaderService().getSubmissionDelinquentDocumentHeaders(principalId, currentBeginDate);
70
71 if (!leaveCalendarDocumentHeaders.isEmpty()) {
72 principalIds.add(principalId);
73 }
74 }
75 }
76 }
77 }
78 }
79
80 for (String principalId : principalIds) {
81 String subject = "Delinquent Leave Calendar Reminder";
82 StringBuilder message = new StringBuilder();
83 message.append("You currently have one or more DELINQUENT Leave Calendars.");
84 message.append(SystemUtils.LINE_SEPARATOR);
85 message.append(SystemUtils.LINE_SEPARATOR);
86 message.append("Please review/enter your time-off for any prior unapproved months and submit your calendar(s).");
87
88 getKpmeNotificationService().sendNotification(subject, message.toString(), principalId);
89 }
90 }
91
92 public static int getCalendarEntriesPollingWindow() {
93 return CALENDAR_ENTRIES_POLLING_WINDOW;
94 }
95
96 public static void setCalendarEntriesPollingWindow(int calendarEntriesPollingWindow) {
97 CALENDAR_ENTRIES_POLLING_WINDOW = calendarEntriesPollingWindow;
98 }
99
100 public static CalendarEntryService getCalendarEntryService() {
101 return CALENDAR_ENTRY_SERVICE;
102 }
103
104 public static void setCalendarEntryService(CalendarEntryService calendarEntryService) {
105 CALENDAR_ENTRY_SERVICE = calendarEntryService;
106 }
107
108 public static KPMENotificationService getKpmeNotificationService() {
109 return KPME_NOTIFICATION_SERVICE;
110 }
111
112 public static void setKpmeNotificationService(KPMENotificationService kpmeNotificationService) {
113 KPME_NOTIFICATION_SERVICE = kpmeNotificationService;
114 }
115
116 public static LeaveCalendarDocumentHeaderService getLeaveCalendarDocumentHeaderService() {
117 return LEAVE_CALENDAR_DOCUMENT_HEADER_SERVICE;
118 }
119
120 public static void setLeaveCalendarDocumentHeaderService(LeaveCalendarDocumentHeaderService leaveCalendarDocumentHeaderService) {
121 LEAVE_CALENDAR_DOCUMENT_HEADER_SERVICE = leaveCalendarDocumentHeaderService;
122 }
123
124 public static PrincipalHRAttributesService getPrincipalHRAttributesService() {
125 return PRINCIPAL_HR_ATTRIBUTES_SERVICE;
126 }
127
128 public static void setPrincipalHRAttributesService(PrincipalHRAttributesService principalHRAttributesService) {
129 PRINCIPAL_HR_ATTRIBUTES_SERVICE = principalHRAttributesService;
130 }
131
132 }