001 /**
002 * Copyright 2004-2012 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.util.ArrayList;
019 import java.util.Date;
020 import java.util.List;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.apache.log4j.Logger;
024 import org.kuali.hr.time.calendar.CalendarEntries;
025 import org.kuali.hr.time.service.base.TkServiceLocator;
026 import org.kuali.hr.time.util.TKUtils;
027 import org.kuali.hr.time.util.TkConstants;
028
029 public class BatchJobManagerThread extends Thread {
030 private static final Logger LOG = Logger.getLogger(BatchJobManagerThread.class);
031
032 int startupSleep = 120;
033 //This represents a number of days on both sides of today
034 int numOfDaysToPoll = 30;
035 int secondsToSleep = 120;
036
037 public BatchJobManagerThread(int secondsToSleep, int numberOfDaysToPoll, int startupSleep) {
038 this.numOfDaysToPoll = numberOfDaysToPoll;
039 this.secondsToSleep = secondsToSleep;
040 this.startupSleep = startupSleep;
041 }
042
043 @Override
044 public void run() {
045
046 try {
047 Thread.sleep(1000 * startupSleep);
048 } catch (Exception e) {
049 LOG.error(e);
050 }
051
052 while (true) {
053 Date asOfDate = TKUtils.getCurrentDate();
054 List<CalendarEntries> payCalendarEntries = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntryNeedsScheduled(numOfDaysToPoll, asOfDate);
055
056 LOG.info("Scanning for batch jobs to run: ("+asOfDate.toString()+")");
057
058 List<BatchJob> jobsToRun = new ArrayList<BatchJob>();
059 for(CalendarEntries payCalendarEntry: payCalendarEntries){
060
061 List<BatchJob> batchJobs = TkServiceLocator.getBatchJobService().getBatchJobs(payCalendarEntry.getHrCalendarEntriesId());
062
063 // batchJobs.clear();
064 // DumbJob dj = new DumbJob();
065 // jobsToRun.add(dj);
066 if ((payCalendarEntry.getBatchInitiateDate() != null) && (!jobPresentInJobsList(batchJobs, TkConstants.BATCH_JOB_NAMES.INITIATE)) ) {
067 BatchJob job = new InitiateBatchJob(payCalendarEntry.getHrCalendarEntriesId());
068 TkServiceLocator.getBatchJobService().saveBatchJob(job);
069 batchJobs.add(job);
070 jobsToRun.add(job);
071 }
072 //
073 // if ((payCalendarEntry.getBatchEmployeeApprovalDate() != null) && (!jobPresentInJobsList(batchJobs, TkConstants.BATCH_JOB_NAMES.APPROVE)) ) {
074 // BatchJob job = new EmployeeApprovalBatchJob(payCalendarEntry);
075 // TkServiceLocator.getBatchJobService().saveBatchJob(job);
076 // batchJobs.add(job);
077 // }
078 //
079 // if ((payCalendarEntry.getBatchEndPayPeriodDate() != null) && (!jobPresentInJobsList(batchJobs, TkConstants.BATCH_JOB_NAMES.PAY_PERIOD_END)) ) {
080 // BatchJob job = new PayPeriodEndBatchJob(payCalendarEntry.getHrPyCalendarEntriesId());
081 // TkServiceLocator.getBatchJobService().saveBatchJob(job);
082 // batchJobs.add(job);
083 // }
084 //
085 // if ((payCalendarEntry.getBatchSupervisorApprovalDate() != null) && (!jobPresentInJobsList(batchJobs, TkConstants.BATCH_JOB_NAMES.SUPERVISOR_APPROVAL)) ) {
086 // BatchJob job = new SupervisorApprovalBatchJob(payCalendarEntry.getHrPyCalendarEntriesId());
087 // TkServiceLocator.getBatchJobService().saveBatchJob(job);
088 // batchJobs.add(job);
089 // }
090 }
091
092 for (BatchJob job : jobsToRun) {
093 job.runJob();
094 }
095
096 try {
097 Thread.sleep(1000 * secondsToSleep);
098 } catch (Exception e) {
099 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 }