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.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.log4j.Logger;
23  import org.kuali.hr.time.assignment.Assignment;
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  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
29  
30  
31  public class InitiateBatchJob extends BatchJob {
32      private Logger LOG = Logger.getLogger(InitiateBatchJob.class);
33      private CalendarEntries calendarEntry;
34  
35      public InitiateBatchJob(CalendarEntries calendarEntry) {
36          this.setBatchJobName(TkConstants.BATCH_JOB_NAMES.INITIATE);
37          this.setHrPyCalendarEntryId(calendarEntry.getHrCalendarEntriesId());
38          this.calendarEntry = calendarEntry;
39      }
40  
41  	@Override
42  	public void doWork() {
43  		Date asOfDate = TKUtils.getCurrentDate();
44  		List<Assignment> lstAssignments = TkServiceLocator.getAssignmentService().getActiveAssignments(asOfDate);
45  		for(Assignment assign : lstAssignments){
46  			TimesheetDocumentHeader tkDocHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(assign.getPrincipalId(), calendarEntry.getBeginPeriodDateTime(), calendarEntry.getEndPeriodDateTime());
47  			if(tkDocHeader == null || StringUtils.equals(tkDocHeader.getDocumentStatus(),TkConstants.ROUTE_STATUS.CANCEL)){
48  				populateBatchJobEntry(assign);
49  			}
50  		}
51  	}
52  
53  
54      @Override
55      protected void populateBatchJobEntry(Object o) {
56          Assignment assign = (Assignment)o;
57          String ip = this.getNextIpAddressInCluster();
58          if(StringUtils.isNotBlank(ip)){
59              //insert a batch job entry here
60              BatchJobEntry entry = this.createBatchJobEntry(this.getBatchJobName(), ip, assign.getPrincipalId(), null,null);
61              TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(entry);
62          } else {
63              LOG.info("No ip found in cluster to assign batch jobs");
64          }
65      }
66  
67  }