001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.batch;
017    
018    import java.util.Date;
019    import java.util.HashSet;
020    import java.util.List;
021    import java.util.Set;
022    
023    import org.apache.commons.lang.SystemUtils;
024    import org.apache.commons.lang.time.DateUtils;
025    import org.kuali.hr.core.notification.service.KPMENotificationService;
026    import org.kuali.hr.lm.workflow.LeaveCalendarDocumentHeader;
027    import org.kuali.hr.lm.workflow.service.LeaveCalendarDocumentHeaderService;
028    import org.kuali.hr.time.calendar.CalendarEntries;
029    import org.kuali.hr.time.calendar.service.CalendarEntriesService;
030    import org.kuali.hr.time.principal.PrincipalHRAttributes;
031    import org.kuali.hr.time.principal.service.PrincipalHRAttributesService;
032    import org.kuali.hr.time.util.TKUtils;
033    import org.quartz.Job;
034    import org.quartz.JobExecutionContext;
035    import org.quartz.JobExecutionException;
036    
037    public class LeaveCalendarDelinquencyJob implements Job {
038            
039            private static int CALENDAR_ENTRIES_POLLING_WINDOW;
040    
041            private static CalendarEntriesService CALENDAR_ENTRIES_SERVICE;
042            private static KPMENotificationService KPME_NOTIFICATION_SERVICE;
043            private static LeaveCalendarDocumentHeaderService LEAVE_CALENDAR_DOCUMENT_HEADER_SERVICE;
044            private static PrincipalHRAttributesService PRINCIPAL_HR_ATTRIBUTES_SERVICE;
045            
046            @Override
047            public void execute(JobExecutionContext context) throws JobExecutionException {
048                    Set<String> principalIds = new HashSet<String>();
049                    
050                    Date asOfDate = TKUtils.getCurrentDate();
051                    List<CalendarEntries> calendarEntries = getCalendarEntriesService().getCurrentCalendarEntryNeedsScheduled(getCalendarEntriesPollingWindow(), asOfDate);
052                    
053                    for (CalendarEntries calendarEntry : calendarEntries) {
054                            String hrCalendarId = calendarEntry.getHrCalendarId();
055                            Date currentBeginDate = calendarEntry.getBeginPeriodDateTime();
056                            
057                            if (currentBeginDate.before(asOfDate) || DateUtils.isSameDay(currentBeginDate, asOfDate)) {
058                                    CalendarEntries previousCalendarEntry = getCalendarEntriesService().getPreviousCalendarEntriesByCalendarId(hrCalendarId, calendarEntry);
059                                    
060                                    if (previousCalendarEntry != null) {
061                                            String calendarName = previousCalendarEntry.getCalendarName();
062                                            Date previousBeginDate = previousCalendarEntry.getBeginPeriodDateTime();
063                                            List<PrincipalHRAttributes> principalHRAttributes = getPrincipalHRAttributesService().getActiveEmployeesForLeaveCalendar(calendarName, previousBeginDate);
064                                            
065                                            for (PrincipalHRAttributes principalHRAttribute : principalHRAttributes) {
066                                                    String principalId = principalHRAttribute.getPrincipalId();
067                                                    
068                                                    if (!principalIds.contains(principalId)) {
069                                                            List<LeaveCalendarDocumentHeader> leaveCalendarDocumentHeaders = getLeaveCalendarDocumentHeaderService().getSubmissionDelinquentDocumentHeaders(principalId, currentBeginDate);
070                            
071                                                            if (!leaveCalendarDocumentHeaders.isEmpty()) {
072                                                                    principalIds.add(principalId);
073                                                    }
074                                                    }
075                                            }
076                                    }
077                            }
078                    }
079                    
080                    for (String principalId : principalIds) {
081                            String subject = "Delinquent Leave Calendar Reminder";
082                            StringBuilder message = new StringBuilder();
083                            message.append("You currently have one or more DELINQUENT Leave Calendars.");
084                            message.append(SystemUtils.LINE_SEPARATOR);
085                            message.append(SystemUtils.LINE_SEPARATOR);
086                            message.append("Please review/enter your time-off for any prior unapproved months and submit your calendar(s).");
087                            
088                            getKpmeNotificationService().sendNotification(subject, message.toString(), principalId);
089                    }
090            }
091            
092            public static int getCalendarEntriesPollingWindow() {
093                    return CALENDAR_ENTRIES_POLLING_WINDOW;
094            }
095    
096            public static void setCalendarEntriesPollingWindow(int calendarEntriesPollingWindow) {
097                    CALENDAR_ENTRIES_POLLING_WINDOW = calendarEntriesPollingWindow;
098            }
099    
100            public static CalendarEntriesService getCalendarEntriesService() {
101                    return CALENDAR_ENTRIES_SERVICE;
102            }
103    
104            public static void setCalendarEntriesService(CalendarEntriesService calendarEntriesService) {
105                    CALENDAR_ENTRIES_SERVICE = calendarEntriesService;
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    }