1 /**
2 * Copyright 2004-2012 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.util.ArrayList;
19 import java.util.Date;
20 import java.util.List;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.log4j.Logger;
24 import org.kuali.hr.time.calendar.CalendarEntries;
25 import org.kuali.hr.time.service.base.TkServiceLocator;
26 import org.kuali.hr.time.util.TKUtils;
27 import org.kuali.hr.time.util.TkConstants;
28
29 public class BatchJobManagerThread extends Thread {
30 private static final Logger LOG = Logger.getLogger(BatchJobManagerThread.class);
31
32 int startupSleep = 120;
33 //This represents a number of days on both sides of today
34 int numOfDaysToPoll = 30;
35 int secondsToSleep = 120;
36
37 public BatchJobManagerThread(int secondsToSleep, int numberOfDaysToPoll, int startupSleep) {
38 this.numOfDaysToPoll = numberOfDaysToPoll;
39 this.secondsToSleep = secondsToSleep;
40 this.startupSleep = startupSleep;
41 }
42
43 @Override
44 public void run() {
45
46 try {
47 Thread.sleep(1000 * startupSleep);
48 } catch (Exception e) {
49 LOG.error(e);
50 }
51
52 while (true) {
53 Date asOfDate = TKUtils.getCurrentDate();
54 List<CalendarEntries> payCalendarEntries = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntryNeedsScheduled(numOfDaysToPoll, asOfDate);
55
56 LOG.info("Scanning for batch jobs to run: ("+asOfDate.toString()+")");
57
58 List<BatchJob> jobsToRun = new ArrayList<BatchJob>();
59 for(CalendarEntries payCalendarEntry: payCalendarEntries){
60
61 List<BatchJob> batchJobs = TkServiceLocator.getBatchJobService().getBatchJobs(payCalendarEntry.getHrCalendarEntriesId());
62
63 // batchJobs.clear();
64 // DumbJob dj = new DumbJob();
65 // jobsToRun.add(dj);
66 if ((payCalendarEntry.getBatchInitiateDate() != null) && (!jobPresentInJobsList(batchJobs, TkConstants.BATCH_JOB_NAMES.INITIATE)) ) {
67 BatchJob job = new InitiateBatchJob(payCalendarEntry.getHrCalendarEntriesId());
68 TkServiceLocator.getBatchJobService().saveBatchJob(job);
69 batchJobs.add(job);
70 jobsToRun.add(job);
71 }
72 //
73 // if ((payCalendarEntry.getBatchEmployeeApprovalDate() != null) && (!jobPresentInJobsList(batchJobs, TkConstants.BATCH_JOB_NAMES.APPROVE)) ) {
74 // BatchJob job = new EmployeeApprovalBatchJob(payCalendarEntry);
75 // TkServiceLocator.getBatchJobService().saveBatchJob(job);
76 // batchJobs.add(job);
77 // }
78 //
79 // if ((payCalendarEntry.getBatchEndPayPeriodDate() != null) && (!jobPresentInJobsList(batchJobs, TkConstants.BATCH_JOB_NAMES.PAY_PERIOD_END)) ) {
80 // BatchJob job = new PayPeriodEndBatchJob(payCalendarEntry.getHrPyCalendarEntriesId());
81 // TkServiceLocator.getBatchJobService().saveBatchJob(job);
82 // batchJobs.add(job);
83 // }
84 //
85 // if ((payCalendarEntry.getBatchSupervisorApprovalDate() != null) && (!jobPresentInJobsList(batchJobs, TkConstants.BATCH_JOB_NAMES.SUPERVISOR_APPROVAL)) ) {
86 // BatchJob job = new SupervisorApprovalBatchJob(payCalendarEntry.getHrPyCalendarEntriesId());
87 // TkServiceLocator.getBatchJobService().saveBatchJob(job);
88 // batchJobs.add(job);
89 // }
90 }
91
92 for (BatchJob job : jobsToRun) {
93 job.runJob();
94 }
95
96 try {
97 Thread.sleep(1000 * secondsToSleep);
98 } catch (Exception e) {
99 LOG.error(e);
100 }
101 }
102 }
103
104 private boolean jobPresentInJobsList(List<BatchJob> batchJobs, String batchJobName) {
105 for (BatchJob batchJob : batchJobs) {
106 if (StringUtils.equals(batchJob.getBatchJobName(), batchJobName)) {
107 return true;
108 }
109 }
110
111 return false;
112 }
113
114
115
116 }