View Javadoc

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.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  
34  
35      public InitiateBatchJob(String hrPyCalendarEntryId) {
36          super();
37          this.setBatchJobName(TkConstants.BATCH_JOB_NAMES.INITIATE);
38          this.setHrPyCalendarEntryId(hrPyCalendarEntryId);
39      }
40  
41  	@Override
42  	public void doWork() {
43  		Date asOfDate = TKUtils.getCurrentDate();
44  		List<Assignment> lstAssignments = TkServiceLocator.getAssignmentService().getActiveAssignments(asOfDate);
45  		CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(this.getHrPyCalendarEntryId());
46  		for(Assignment assign : lstAssignments){
47  			TimesheetDocumentHeader tkDocHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(assign.getPrincipalId(), payCalendarEntry.getBeginPeriodDateTime(), payCalendarEntry.getEndPeriodDateTime());
48  			if(tkDocHeader == null || StringUtils.equals(tkDocHeader.getDocumentStatus(),TkConstants.ROUTE_STATUS.CANCEL)){
49  				populateBatchJobEntry(assign);
50  			}
51  		}
52  	}
53  
54  
55      @Override
56      protected void populateBatchJobEntry(Object o) {
57          Assignment assign = (Assignment)o;
58          String ip = this.getNextIpAddressInCluster();
59          if(StringUtils.isNotBlank(ip)){
60              //insert a batch job entry here
61              BatchJobEntry entry = this.createBatchJobEntry(this.getBatchJobName(), ip, assign.getPrincipalId(), null,null);
62              TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(entry);
63          } else {
64              LOG.info("No ip found in cluster to assign batch jobs");
65          }
66      }
67  
68  }