View Javadoc
1   package org.kuali.ole.batch.rule;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.deliver.bo.OleBatchJobBo;
6   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
7   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
8   import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
9   import org.quartz.JobDetail;
10  import org.quartz.SchedulerException;
11  import org.quartz.impl.StdScheduler;
12  import org.springframework.scheduling.quartz.CronTriggerBean;
13  
14  /**
15   * Created with IntelliJ IDEA.
16   * User: ?
17   * Date: 3/1/13
18   * Time: 8:18 PM
19   * To change this template use File | Settings | File Templates.
20   */
21  public class OleBatchJobRule extends MaintenanceDocumentRuleBase {
22  
23      private static final Logger LOG = Logger.getLogger(OleBatchJobRule.class);
24      @Override
25      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
26          boolean isValid = true;
27          OleBatchJobBo oleBatchJobBo = (OleBatchJobBo) document.getNewMaintainableObject().getDataObject();
28  
29          isValid &= validateCronExpression(oleBatchJobBo);
30          return isValid;
31      }
32  
33      /**
34       *  This method  validates duplicate completeness Id and return boolean value.
35       * @param oleBatchJobBo
36       * @return  boolean
37       */
38      private boolean validateCronExpression(OleBatchJobBo oleBatchJobBo) {
39  
40          boolean isJobModified=true;
41          StdScheduler scheduler = (StdScheduler) GlobalResourceLoader.getService("scheduler");
42  
43          if(!oleBatchJobBo.isJobEnableStatus()){
44              JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName());
45              try {
46                  scheduler.deleteJob(jobDetail.getName(),jobDetail.getGroup()) ;
47              } catch (SchedulerException e) {
48                  isJobModified=false;
49                  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
50              }
51          }
52          else {
53              try {
54  
55                  JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName());
56                  CronTriggerBean cronTriggerBean = new CronTriggerBean();
57                  boolean valid=   org.quartz.CronExpression.isValidExpression(oleBatchJobBo.getJobCronExpression());
58                  if(valid){
59                      cronTriggerBean.setName(oleBatchJobBo.getJobTriggerName()+"Trigger");
60                      cronTriggerBean.setCronExpression(oleBatchJobBo.getJobCronExpression());
61                      cronTriggerBean.setJobName(jobDetail.getName());
62                      cronTriggerBean.setJobGroup(jobDetail.getGroup());
63                      cronTriggerBean.setJobDetail(jobDetail);
64                      String [] jobNames=scheduler.getJobNames(jobDetail.getGroup());
65                      for (String jobName:jobNames){
66                          if(jobName.equals(jobDetail.getName())) {
67                              scheduler.deleteJob(jobDetail.getName(),jobDetail.getGroup());
68                          }
69                      }
70  
71                      try{
72                          scheduler.scheduleJob(jobDetail, cronTriggerBean);
73                      }
74                      catch (Exception e){
75                          isJobModified=false;
76                          LOG.error("Batch Job Exception :: "+e);
77                      }
78  
79  
80                  }else {
81                      isJobModified=false;
82                      this.putFieldError(OLEConstants.INVALID_CRON, "error.invalid.cron");
83                  }
84  
85              }
86              catch (Exception e) {
87                  isJobModified=false;
88                  LOG.error("Batch Job Exception :: "+e);
89              }
90  
91  
92  
93          }
94  
95          return isJobModified;
96      }
97  }