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.sql.Date;
019    import java.text.ParseException;
020    import java.text.SimpleDateFormat;
021    import java.util.List;
022    
023    import org.joda.time.DateTime;
024    import org.kuali.hr.lm.leaveplan.LeavePlan;
025    import org.kuali.hr.time.batch.service.BatchJobService;
026    import org.kuali.hr.time.service.base.TkServiceLocator;
027    import org.kuali.hr.time.util.TKUtils;
028    import org.quartz.JobExecutionContext;
029    import org.quartz.JobExecutionException;
030    import org.quartz.SchedulerException;
031    import org.springframework.scheduling.quartz.QuartzJobBean;
032    
033    public class CarryOverSchedulerJob extends QuartzJobBean {
034    
035            private static int LEAVE_PLAN_POLLING_WINDOW;
036            
037            private static BatchJobService batchJobService;
038    
039            @Override
040            protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
041                    Date asOfDate = TKUtils.getCurrentDate();
042                    List<LeavePlan> leavePlans = TkServiceLocator.getLeavePlanService().getLeavePlansNeedsCarryOverScheduled(getLeavePlanPollingWindow(), asOfDate);
043            try {
044                    if(leavePlans!=null && !leavePlans.isEmpty()) {
045                                    DateTime current = new DateTime(asOfDate.getTime());
046                            DateTime windowStart = current.minusDays(getLeavePlanPollingWindow());
047                            DateTime windowEnd = current.plusDays(getLeavePlanPollingWindow());
048    
049                            // schedule batch job for all LeavePlans who fall in leave polling window.
050                            for(LeavePlan leavePlan : leavePlans) {
051                                    if(leavePlan.getBatchPriorYearCarryOverStartDate() != null  && leavePlan.getBatchPriorYearCarryOverStartTime() != null ) {
052                                            java.util.Date batchDate = getBatchJobStartDateTime(leavePlan);
053                                            DateTime batchDateTime = new DateTime(batchDate.getTime());
054                                            if(batchDateTime.compareTo(windowStart) >= 0 && batchDateTime.compareTo(windowEnd) <= 0) {
055                                                    getBatchJobService().scheduleLeaveCarryOverJobs(leavePlan);
056                                            }
057                                    }
058                            }
059                    }
060            } catch (SchedulerException se) {
061                    throw new JobExecutionException("Exception thrown during job scheduling of Carry over for Leave", se);
062            }
063            }
064            
065            public int getLeavePlanPollingWindow() {
066                    return LEAVE_PLAN_POLLING_WINDOW;
067            }
068    
069            public void setLeavePlanPollingWindow(int leavePlanPollingWindow) {
070                    LEAVE_PLAN_POLLING_WINDOW = leavePlanPollingWindow;
071            }
072    
073            public static BatchJobService getBatchJobService() {
074                    return CarryOverSchedulerJob.batchJobService;
075            }
076    
077            public void setBatchJobService(BatchJobService batchJobService) {
078                    CarryOverSchedulerJob.batchJobService = batchJobService;
079            }
080            
081            private java.util.Date getBatchJobStartDateTime(LeavePlan leavePlan) {
082                    
083                    String batchJobDate = leavePlan.getBatchPriorYearCarryOverStartDate();
084                    
085                    java.util.Calendar batchJobTimeCal = java.util.Calendar.getInstance();
086                    batchJobTimeCal.setTimeInMillis(leavePlan.getBatchPriorYearCarryOverStartTime().getTime());
087                    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
088                    sdf.setLenient(false);
089                    
090                    java.util.Date batchJobStart = null;
091                    
092                    try {
093                            batchJobStart = sdf.parse(batchJobDate);
094                    } catch (ParseException e) {
095                    }
096                    
097                    java.util.Calendar batchJobStartDateTime = java.util.Calendar.getInstance();
098                    batchJobStartDateTime.setTime(batchJobStart);
099                    batchJobStartDateTime.set(java.util.Calendar.YEAR,java.util.Calendar.getInstance().get(java.util.Calendar.YEAR));
100                    batchJobStartDateTime.set(java.util.Calendar.HOUR_OF_DAY,batchJobTimeCal.get(java.util.Calendar.HOUR_OF_DAY));
101                    batchJobStartDateTime.set(java.util.Calendar.MINUTE,batchJobTimeCal.get(java.util.Calendar.MINUTE));
102                    batchJobStartDateTime.set(java.util.Calendar.SECOND, 0);
103                    batchJobStartDateTime.set(java.util.Calendar.MILLISECOND, 0);
104                    
105                    return batchJobStartDateTime.getTime();
106            }
107    
108    }