1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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> calendarEntries = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntryNeedsScheduled(numOfDaysToPoll, asOfDate);
55
56 LOG.info("Scanning for batch jobs to run: (" + asOfDate.toString() + ")");
57
58 List<BatchJob> batchJobs = new ArrayList<BatchJob>();
59 for (CalendarEntries calendarEntry : calendarEntries) {
60 List<BatchJob> existingBatchJobs = TkServiceLocator.getBatchJobService().getBatchJobs(calendarEntry.getHrCalendarEntriesId());
61
62 if (calendarEntry.getBeginPeriodDateTime() != null && calendarEntry.getEndPeriodDateTime() != null && !jobExists(existingBatchJobs, TkConstants.BATCH_JOB_NAMES.BATCH_APPROVE_MISSED_PUNCH)) {
63 BatchJob job = new BatchApproveMissedPunchJob(calendarEntry);
64 TkServiceLocator.getBatchJobService().saveBatchJob(job);
65 batchJobs.add(job);
66 }
67
68 if (calendarEntry.getBatchInitiateDate() != null && !jobExists(existingBatchJobs, TkConstants.BATCH_JOB_NAMES.INITIATE)) {
69 BatchJob job = new InitiateBatchJob(calendarEntry);
70 TkServiceLocator.getBatchJobService().saveBatchJob(job);
71 batchJobs.add(job);
72 }
73
74 if (calendarEntry.getBatchEndPayPeriodDate() != null && !jobExists(existingBatchJobs, TkConstants.BATCH_JOB_NAMES.PAY_PERIOD_END)) {
75 BatchJob job = new PayPeriodEndBatchJob(calendarEntry);
76 TkServiceLocator.getBatchJobService().saveBatchJob(job);
77 batchJobs.add(job);
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92 }
93
94 for (BatchJob batchJob : batchJobs) {
95 batchJob.runJob();
96 }
97
98 try {
99 Thread.sleep(1000 * secondsToSleep);
100 } catch (Exception e) {
101 LOG.error(e);
102 }
103 }
104 }
105
106 private boolean jobExists(List<BatchJob> batchJobs, String batchJobName) {
107 for (BatchJob batchJob : batchJobs) {
108 if (StringUtils.equals(batchJob.getBatchJobName(), batchJobName)) {
109 return true;
110 }
111 }
112
113 return false;
114 }
115
116
117
118 }