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.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> 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  // TODO: Batch jobs going to exception status
81  //                if (calendarEntry.getBatchEmployeeApprovalDate() != null && !jobExists(existingBatchJobs, TkConstants.BATCH_JOB_NAMES.APPROVE)) {
82  //                    BatchJob job = new EmployeeApprovalBatchJob(calendarEntry);
83  //                    TkServiceLocator.getBatchJobService().saveBatchJob(job);
84  //                    batchJobs.add(job);
85  //                }
86  //
87  //                if (calendarEntry.getBatchSupervisorApprovalDate() != null && !jobExists(existingBatchJobs, TkConstants.BATCH_JOB_NAMES.SUPERVISOR_APPROVAL)) {
88  //                    BatchJob job = new SupervisorApprovalBatchJob(calendarEntry);
89  //                    TkServiceLocator.getBatchJobService().saveBatchJob(job);
90  //                    batchJobs.add(job);
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 }