View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.tklm.leave.batch;
17  
18  import java.util.HashSet;
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.apache.commons.lang.SystemUtils;
23  import org.apache.commons.lang.time.DateUtils;
24  import org.joda.time.DateTime;
25  import org.joda.time.LocalDate;
26  import org.kuali.kpme.core.batch.BatchJob;
27  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
28  import org.kuali.kpme.core.calendar.entry.service.CalendarEntryService;
29  import org.kuali.kpme.core.principal.PrincipalHRAttributes;
30  import org.kuali.kpme.core.principal.service.PrincipalHRAttributesService;
31  import org.kuali.kpme.core.service.notification.KPMENotificationService;
32  import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
33  import org.kuali.kpme.tklm.leave.workflow.service.LeaveCalendarDocumentHeaderService;
34  import org.quartz.Job;
35  import org.quartz.JobExecutionContext;
36  import org.quartz.JobExecutionException;
37  
38  public class LeaveCalendarDelinquencyJob extends BatchJob {
39  	
40  	private static int CALENDAR_ENTRIES_POLLING_WINDOW;
41  
42  	private static CalendarEntryService CALENDAR_ENTRY_SERVICE;
43  	private static KPMENotificationService KPME_NOTIFICATION_SERVICE;
44  	private static LeaveCalendarDocumentHeaderService LEAVE_CALENDAR_DOCUMENT_HEADER_SERVICE;
45  	private static PrincipalHRAttributesService PRINCIPAL_HR_ATTRIBUTES_SERVICE;
46  	
47  	@Override
48  	public void execute(JobExecutionContext context) throws JobExecutionException {
49  		Set<String> principalIds = new HashSet<String>();
50  		
51  		DateTime asOfDate = new LocalDate().toDateTimeAtStartOfDay();
52  		List<CalendarEntry> calendarEntries = getCalendarEntryService().getCurrentCalendarEntriesNeedsScheduled(getCalendarEntriesPollingWindow(), asOfDate);
53  		
54  		for (CalendarEntry calendarEntry : calendarEntries) {
55  			String hrCalendarId = calendarEntry.getHrCalendarId();
56  			DateTime currentBeginDate = calendarEntry.getBeginPeriodFullDateTime();
57  			
58  			if (currentBeginDate.isBefore(asOfDate) || DateUtils.isSameDay(currentBeginDate.toDate(), asOfDate.toDate())) {
59  				CalendarEntry previousCalendarEntry = getCalendarEntryService().getPreviousCalendarEntryByCalendarId(hrCalendarId, calendarEntry);
60  				
61  				if (previousCalendarEntry != null) {
62  					String calendarName = previousCalendarEntry.getCalendarName();
63  					LocalDate previousBeginDate = previousCalendarEntry.getBeginPeriodFullDateTime().toLocalDate();
64  					List<PrincipalHRAttributes> principalHRAttributes = getPrincipalHRAttributesService().getActiveEmployeesForLeaveCalendar(calendarName, previousBeginDate);
65  					
66  					for (PrincipalHRAttributes principalHRAttribute : principalHRAttributes) {
67  						String principalId = principalHRAttribute.getPrincipalId();
68  						
69  						if (!principalIds.contains(principalId)) {
70  							List<LeaveCalendarDocumentHeader> leaveCalendarDocumentHeaders = getLeaveCalendarDocumentHeaderService().getSubmissionDelinquentDocumentHeaders(principalId, currentBeginDate);
71  			
72  							if (!leaveCalendarDocumentHeaders.isEmpty()) {
73  								principalIds.add(principalId);
74  					        }
75  						}
76  					}
77  				}
78  			}
79  		}
80  		
81  		for (String principalId : principalIds) {
82  			String subject = "Delinquent Leave Calendar Reminder";
83  			StringBuilder message = new StringBuilder();
84  			message.append("You currently have one or more DELINQUENT Leave Calendars.");
85  			message.append(SystemUtils.LINE_SEPARATOR);
86  			message.append(SystemUtils.LINE_SEPARATOR);
87  			message.append("Please review/enter your time-off for any prior unapproved months and submit your calendar(s).");
88  			
89  			getKpmeNotificationService().sendNotification(subject, message.toString(), principalId);
90  		}
91  	}
92  	
93  	public static int getCalendarEntriesPollingWindow() {
94  		return CALENDAR_ENTRIES_POLLING_WINDOW;
95  	}
96  
97  	public static void setCalendarEntriesPollingWindow(int calendarEntriesPollingWindow) {
98  		CALENDAR_ENTRIES_POLLING_WINDOW = calendarEntriesPollingWindow;
99  	}
100 
101 	public static CalendarEntryService getCalendarEntryService() {
102 		return CALENDAR_ENTRY_SERVICE;
103 	}
104 
105 	public static void setCalendarEntryService(CalendarEntryService calendarEntryService) {
106 		CALENDAR_ENTRY_SERVICE = calendarEntryService;
107 	}
108 
109 	public static KPMENotificationService getKpmeNotificationService() {
110 		return KPME_NOTIFICATION_SERVICE;
111 	}
112 
113 	public static void setKpmeNotificationService(KPMENotificationService kpmeNotificationService) {
114 		KPME_NOTIFICATION_SERVICE = kpmeNotificationService;
115 	}
116 
117 	public static LeaveCalendarDocumentHeaderService getLeaveCalendarDocumentHeaderService() {
118 		return LEAVE_CALENDAR_DOCUMENT_HEADER_SERVICE;
119 	}
120 
121 	public static void setLeaveCalendarDocumentHeaderService(LeaveCalendarDocumentHeaderService leaveCalendarDocumentHeaderService) {
122 		LEAVE_CALENDAR_DOCUMENT_HEADER_SERVICE = leaveCalendarDocumentHeaderService;
123 	}
124 
125 	public static PrincipalHRAttributesService getPrincipalHRAttributesService() {
126 		return PRINCIPAL_HR_ATTRIBUTES_SERVICE;
127 	}
128 
129 	public static void setPrincipalHRAttributesService(PrincipalHRAttributesService principalHRAttributesService) {
130 		PRINCIPAL_HR_ATTRIBUTES_SERVICE = principalHRAttributesService;
131 	}
132 	
133 }