View Javadoc

1   /**
2    * Copyright 2004-2013 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.hr.time.batch;
17  
18  import java.sql.Date;
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.List;
22  
23  import org.joda.time.DateTime;
24  import org.kuali.hr.lm.leaveplan.LeavePlan;
25  import org.kuali.hr.time.batch.service.BatchJobService;
26  import org.kuali.hr.time.service.base.TkServiceLocator;
27  import org.kuali.hr.time.util.TKUtils;
28  import org.quartz.JobExecutionContext;
29  import org.quartz.JobExecutionException;
30  import org.quartz.SchedulerException;
31  import org.springframework.scheduling.quartz.QuartzJobBean;
32  
33  public class CarryOverSchedulerJob extends QuartzJobBean {
34  
35  	private static int LEAVE_PLAN_POLLING_WINDOW;
36  	
37  	private static BatchJobService batchJobService;
38  
39  	@Override
40  	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
41  		Date asOfDate = TKUtils.getCurrentDate();
42  		List<LeavePlan> leavePlans = TkServiceLocator.getLeavePlanService().getLeavePlansNeedsCarryOverScheduled(getLeavePlanPollingWindow(), asOfDate);
43          try {
44          	if(leavePlans!=null && !leavePlans.isEmpty()) {
45  				DateTime current = new DateTime(asOfDate.getTime());
46  		        DateTime windowStart = current.minusDays(getLeavePlanPollingWindow());
47  		        DateTime windowEnd = current.plusDays(getLeavePlanPollingWindow());
48  
49          		// schedule batch job for all LeavePlans who fall in leave polling window.
50          		for(LeavePlan leavePlan : leavePlans) {
51          			if(leavePlan.getBatchPriorYearCarryOverStartDate() != null  && leavePlan.getBatchPriorYearCarryOverStartTime() != null ) {
52          				java.util.Date batchDate = getBatchJobStartDateTime(leavePlan);
53          				DateTime batchDateTime = new DateTime(batchDate.getTime());
54          				if(batchDateTime.compareTo(windowStart) >= 0 && batchDateTime.compareTo(windowEnd) <= 0) {
55          					getBatchJobService().scheduleLeaveCarryOverJobs(leavePlan);
56          				}
57          			}
58          		}
59          	}
60          } catch (SchedulerException se) {
61          	throw new JobExecutionException("Exception thrown during job scheduling of Carry over for Leave", se);
62          }
63  	}
64  	
65  	public int getLeavePlanPollingWindow() {
66  		return LEAVE_PLAN_POLLING_WINDOW;
67  	}
68  
69  	public void setLeavePlanPollingWindow(int leavePlanPollingWindow) {
70  		LEAVE_PLAN_POLLING_WINDOW = leavePlanPollingWindow;
71  	}
72  
73  	public static BatchJobService getBatchJobService() {
74  		return CarryOverSchedulerJob.batchJobService;
75  	}
76  
77  	public void setBatchJobService(BatchJobService batchJobService) {
78  		CarryOverSchedulerJob.batchJobService = batchJobService;
79  	}
80  	
81  	private java.util.Date getBatchJobStartDateTime(LeavePlan leavePlan) {
82  		
83  		String batchJobDate = leavePlan.getBatchPriorYearCarryOverStartDate();
84  		
85  		java.util.Calendar batchJobTimeCal = java.util.Calendar.getInstance();
86  		batchJobTimeCal.setTimeInMillis(leavePlan.getBatchPriorYearCarryOverStartTime().getTime());
87  		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
88  		sdf.setLenient(false);
89  		
90  		java.util.Date batchJobStart = null;
91  		
92  		try {
93  			batchJobStart = sdf.parse(batchJobDate);
94  		} catch (ParseException e) {
95  		}
96  		
97  		java.util.Calendar batchJobStartDateTime = java.util.Calendar.getInstance();
98  		batchJobStartDateTime.setTime(batchJobStart);
99  		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 }