001    /**
002     * Copyright 2004-2013 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        private CalendarEntries calendarEntry;
034    
035        public InitiateBatchJob(CalendarEntries calendarEntry) {
036            this.setBatchJobName(TkConstants.BATCH_JOB_NAMES.INITIATE);
037            this.setHrPyCalendarEntryId(calendarEntry.getHrCalendarEntriesId());
038            this.calendarEntry = calendarEntry;
039        }
040    
041            @Override
042            public void doWork() {
043                    Date asOfDate = TKUtils.getCurrentDate();
044                    List<Assignment> lstAssignments = TkServiceLocator.getAssignmentService().getActiveAssignments(asOfDate);
045                    for(Assignment assign : lstAssignments){
046                            TimesheetDocumentHeader tkDocHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(assign.getPrincipalId(), calendarEntry.getBeginPeriodDateTime(), calendarEntry.getEndPeriodDateTime());
047                            if(tkDocHeader == null || StringUtils.equals(tkDocHeader.getDocumentStatus(),TkConstants.ROUTE_STATUS.CANCEL)){
048                                    populateBatchJobEntry(assign);
049                            }
050                    }
051            }
052    
053    
054        @Override
055        protected void populateBatchJobEntry(Object o) {
056            Assignment assign = (Assignment)o;
057            String ip = this.getNextIpAddressInCluster();
058            if(StringUtils.isNotBlank(ip)){
059                //insert a batch job entry here
060                BatchJobEntry entry = this.createBatchJobEntry(this.getBatchJobName(), ip, assign.getPrincipalId(), null,null);
061                TkServiceLocator.getBatchJobEntryService().saveBatchJobEntry(entry);
062            } else {
063                LOG.info("No ip found in cluster to assign batch jobs");
064            }
065        }
066    
067    }