View Javadoc
1   package org.kuali.ole.deliver.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  
25      @Override
26      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
27          boolean isValid = true;
28          OleBatchJobBo oleBatchJobBo = (OleBatchJobBo) document.getNewMaintainableObject().getDataObject();
29  
30          isValid &= validateCronExpression(oleBatchJobBo);
31          return isValid;
32      }
33  
34      /**
35       * This method  validates duplicate completeness Id and return boolean value.
36       *
37       * @param oleBatchJobBo
38       * @return boolean
39       */
40      private boolean validateCronExpression(OleBatchJobBo oleBatchJobBo) {
41  
42          boolean isJobModified = true;
43          StdScheduler scheduler = (StdScheduler) GlobalResourceLoader.getService("scheduler");
44  
45          if (!oleBatchJobBo.isJobEnableStatus()) {
46              JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName());
47              try {
48                  scheduler.deleteJob(jobDetail.getName(), jobDetail.getGroup());
49              } catch (SchedulerException e) {
50                  isJobModified = false;
51                  LOG.error(e, e);  //To change body of catch statement use File | Settings | File Templates.
52              }
53          } else {
54              try {
55  
56                  JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName());
57                  CronTriggerBean cronTriggerBean = new CronTriggerBean();
58                  boolean valid = org.quartz.CronExpression.isValidExpression(oleBatchJobBo.getJobCronExpression());
59                  if (valid) {
60                      cronTriggerBean.setName(oleBatchJobBo.getJobTriggerName() + "Trigger");
61                      cronTriggerBean.setCronExpression(oleBatchJobBo.getJobCronExpression());
62                      cronTriggerBean.setJobName(jobDetail.getName());
63                      cronTriggerBean.setJobGroup(jobDetail.getGroup());
64                      cronTriggerBean.setJobDetail(jobDetail);
65                      String[] jobNames = scheduler.getJobNames(jobDetail.getGroup());
66                      for (String jobName : jobNames) {
67                          if (jobName.equals(jobDetail.getName())) {
68                              scheduler.deleteJob(jobDetail.getName(), jobDetail.getGroup());
69                          }
70                      }
71  
72                      try {
73                          scheduler.scheduleJob(jobDetail, cronTriggerBean);
74                      } catch (Exception e) {
75                          isJobModified = false;
76                          LOG.error("Batch Job Exception :: " + e, e);
77                      }
78  
79  
80                  } else {
81                      isJobModified = false;
82                      this.putFieldError(OLEConstants.INVALID_CRON, "error.invalid.cron");
83                  }
84  
85              } catch (Exception e) {
86                  isJobModified = false;
87                  LOG.error("Batch Job Exception :: " + e, e);
88              }
89          }
90  
91          return isJobModified;
92      }
93  }