View Javadoc
1   package org.kuali.ole.batch.rule;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.commons.lang3.time.DateUtils;
5   import org.apache.log4j.Logger;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.batch.bo.OLEBatchProcessScheduleBo;
8   import org.kuali.ole.batch.helper.OLESchedulerHelper;
9   import org.kuali.ole.deliver.bo.OleBatchJobBo;
10  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
11  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
12  import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
13  import org.quartz.JobDetail;
14  import org.quartz.SchedulerException;
15  import org.quartz.impl.StdScheduler;
16  import org.springframework.scheduling.quartz.CronTriggerBean;
17  
18  import java.sql.Timestamp;
19  import java.text.DateFormat;
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  
24  /**
25   * Created with IntelliJ IDEA.
26   * User: ?
27   * Date: 3/1/13
28   * Time: 8:18 PM
29   * To change this template use File | Settings | File Templates.
30   */
31  public class OleBatchJobRule extends MaintenanceDocumentRuleBase {
32  
33      private static final Logger LOG = Logger.getLogger(OleBatchJobRule.class);
34      @Override
35      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
36          boolean isValid = true;
37          OleBatchJobBo oleBatchJobBo = (OleBatchJobBo) document.getNewMaintainableObject().getDataObject();
38          if (oleBatchJobBo.getRunNowOrSchedule().equalsIgnoreCase("RunNow")) {
39              OLEBatchProcessScheduleBo oleBatchProcessScheduleBo = oleBatchJobBo.getOleBatchProcessScheduleBo();
40              oleBatchProcessScheduleBo.setCreateTime(new Timestamp(new Date().getTime()));
41              oleBatchProcessScheduleBo.setOneTimeOrRecurring("onetime");
42              oleBatchProcessScheduleBo.setOneTimeStartDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
43              oleBatchProcessScheduleBo.setOneTimeStartTime(new SimpleDateFormat("HH:mm").format(DateUtils.addMinutes(new Date(), 1)));
44              oleBatchJobBo.setJobCronExpression(OLESchedulerHelper.getInstance().getCronExpression(oleBatchProcessScheduleBo));
45          }
46          if (StringUtils.isNotBlank(oleBatchJobBo.getCronOrSchedule()) && oleBatchJobBo.getCronOrSchedule().equalsIgnoreCase("Scheduled")) {
47              OLEBatchProcessScheduleBo oleBatchProcessScheduleBo = oleBatchJobBo.getOleBatchProcessScheduleBo();
48              oleBatchProcessScheduleBo.setOneTimeOrRecurring(oleBatchJobBo.getOneTimeOrRecurring());
49              oleBatchJobBo.setJobCronExpression(OLESchedulerHelper.getInstance().getCronExpression(oleBatchJobBo.getOleBatchProcessScheduleBo()));
50          }
51          isValid &= validateCronExpression(oleBatchJobBo);
52          return isValid;
53      }
54  
55      /**
56       *  This method  validates duplicate completeness Id and return boolean value.
57       * @param oleBatchJobBo
58       * @return  boolean
59       */
60      private boolean validateCronExpression(OleBatchJobBo oleBatchJobBo) {
61  
62          boolean isJobModified=true;
63          StdScheduler scheduler = (StdScheduler) GlobalResourceLoader.getService("scheduler");
64  
65          if(!oleBatchJobBo.isJobEnableStatus()){
66              JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName());
67              try {
68                  scheduler.deleteJob(jobDetail.getName(),jobDetail.getGroup()) ;
69              } catch (SchedulerException e) {
70                  isJobModified=false;
71                  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
72              }
73          }
74          else {
75              try {
76  
77                  JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName());
78                  CronTriggerBean cronTriggerBean = new CronTriggerBean();
79                  boolean valid=   org.quartz.CronExpression.isValidExpression(oleBatchJobBo.getJobCronExpression());
80                  if(valid){
81                      cronTriggerBean.setName(oleBatchJobBo.getJobTriggerName()+"Trigger");
82                      cronTriggerBean.setCronExpression(oleBatchJobBo.getJobCronExpression());
83                      cronTriggerBean.setJobName(jobDetail.getName());
84                      cronTriggerBean.setJobGroup(jobDetail.getGroup());
85                      cronTriggerBean.setJobDetail(jobDetail);
86                      String [] jobNames=scheduler.getJobNames(jobDetail.getGroup());
87                      for (String jobName:jobNames){
88                          if(jobName.equals(jobDetail.getName())) {
89                              scheduler.deleteJob(jobDetail.getName(),jobDetail.getGroup());
90                          }
91                      }
92  
93                      try{
94                          scheduler.scheduleJob(jobDetail, cronTriggerBean);
95                      }
96                      catch (Exception e){
97                          isJobModified=false;
98                          LOG.error("Batch Job Exception :: "+e);
99                      }
100 
101 
102                 }else {
103                     isJobModified=false;
104                     this.putFieldError(OLEConstants.INVALID_CRON, "error.invalid.cron");
105                 }
106 
107             }
108             catch (Exception e) {
109                 isJobModified=false;
110                 LOG.error("Batch Job Exception :: "+e);
111             }
112 
113 
114 
115         }
116 
117         return isJobModified;
118     }
119 }