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.sql.Date;
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.apache.log4j.Logger;
023    import org.kuali.hr.time.assignment.Assignment;
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    import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
029    
030    
031    public class InitiateBatchJob extends BatchJob {
032        private Logger LOG = Logger.getLogger(InitiateBatchJob.class);
033    
034    
035        public InitiateBatchJob(String hrPyCalendarEntryId) {
036            super();
037            this.setBatchJobName(TkConstants.BATCH_JOB_NAMES.INITIATE);
038            this.setHrPyCalendarEntryId(hrPyCalendarEntryId);
039        }
040    
041            @Override
042            public void doWork() {
043                    Date asOfDate = TKUtils.getCurrentDate();
044                    List<Assignment> lstAssignments = TkServiceLocator.getAssignmentService().getActiveAssignments(asOfDate);
045                    CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(this.getHrPyCalendarEntryId());
046                    for(Assignment assign : lstAssignments){
047                            TimesheetDocumentHeader tkDocHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(assign.getPrincipalId(), payCalendarEntry.getBeginPeriodDateTime(), payCalendarEntry.getEndPeriodDateTime());
048                            if(tkDocHeader == null || StringUtils.equals(tkDocHeader.getDocumentStatus(),TkConstants.ROUTE_STATUS.CANCEL)){
049                                    populateBatchJobEntry(assign);
050                            }
051                    }
052            }
053    
054    
055        @Override
056        protected void populateBatchJobEntry(Object o) {
057            Assignment assign = (Assignment)o;
058            String ip = this.getNextIpAddressInCluster();
059            if(StringUtils.isNotBlank(ip)){
060                //insert a batch job entry here
061                BatchJobEntry entry = this.createBatchJobEntry(this.getBatchJobName(), ip, assign.getPrincipalId(), null,null);
062                TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(entry);
063            } else {
064                LOG.info("No ip found in cluster to assign batch jobs");
065            }
066        }
067    
068    }