001package org.kuali.ole.batch.service;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.deliver.bo.OleBatchJobBo;
005import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
006import org.kuali.rice.krad.service.KRADServiceLocator;
007import org.quartz.JobDetail;
008import org.quartz.impl.StdScheduler;
009import org.springframework.scheduling.quartz.CronTriggerBean;
010
011import java.util.HashMap;
012import java.util.List;
013import java.util.Map;
014
015/**
016 * Created with IntelliJ IDEA.
017 * User: ?
018 * Date: 3/5/13
019 * Time: 5:28 PM
020 * To change this template use File | Settings | File Templates.
021 */
022public class OleBatchJobService {
023
024    private static final Logger LOG = Logger.getLogger(OleBatchJobService.class);
025    public OleBatchJobService(){
026        addAvailableJobsToScheduler();
027    }
028
029    /*
030     This method is used to add the active job  to the scheduler.
031     */
032    public void addAvailableJobsToScheduler(){
033
034        Map<String,String> batchMap = new HashMap<String, String>();
035        batchMap.put("jobEnableStatus","true");
036        List<OleBatchJobBo> oleDeliverBatchJobBoList =(List<OleBatchJobBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleBatchJobBo.class,batchMap);
037        StdScheduler scheduler = (StdScheduler) GlobalResourceLoader.getService("scheduler");
038
039      if(oleDeliverBatchJobBoList.size()>0){
040          for(OleBatchJobBo oleDeliverBatchJobBo:oleDeliverBatchJobBoList){
041
042              try {
043                  JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleDeliverBatchJobBo.getJobTriggerName());
044                  CronTriggerBean cronTriggerBean = new CronTriggerBean();
045                  cronTriggerBean.setName(oleDeliverBatchJobBo.getJobTriggerName()+"Trigger");
046                  cronTriggerBean.setCronExpression(oleDeliverBatchJobBo.getJobCronExpression());
047                  cronTriggerBean.setJobName(jobDetail.getName());
048                  cronTriggerBean.setJobGroup(jobDetail.getGroup());
049                  cronTriggerBean.setJobDetail(jobDetail);
050                  try{
051                      scheduler.scheduleJob(jobDetail, cronTriggerBean);
052                      LOG.info(jobDetail.getName()+" job is scheduled");
053                  }
054                  catch (Exception e){
055                      scheduler.rescheduleJob(oleDeliverBatchJobBo.getJobTriggerName(),jobDetail.getName(),cronTriggerBean);
056                  }   } catch (Exception e) {
057                  LOG.info(e.getMessage()+" : Unable to schedule the job");
058                    //To change body of catch statement use File | Settings | File Templates.
059              }
060          }
061      }
062
063    }
064}