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.setScheduleType(oleBatchJobBo.getScheduleType());
49              oleBatchProcessScheduleBo.setOneTimeOrRecurring(oleBatchJobBo.getOneTimeOrRecurring());
50              oleBatchJobBo.setJobCronExpression(OLESchedulerHelper.getInstance().getCronExpression(oleBatchJobBo.getOleBatchProcessScheduleBo()));
51          }
52          isValid &= validateCronExpression(oleBatchJobBo);
53          return isValid;
54      }
55  
56      /**
57       *  This method  validates duplicate completeness Id and return boolean value.
58       * @param oleBatchJobBo
59       * @return  boolean
60       */
61      private boolean validateCronExpression(OleBatchJobBo oleBatchJobBo) {
62  
63          boolean isJobModified=true;
64          StdScheduler scheduler = (StdScheduler) GlobalResourceLoader.getService("scheduler");
65  
66          if(!oleBatchJobBo.isJobEnableStatus()){
67              JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName());
68              try {
69                  scheduler.deleteJob(jobDetail.getName(),jobDetail.getGroup()) ;
70              } catch (SchedulerException e) {
71                  isJobModified=false;
72                  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
73              }
74          }
75          else {
76              try {
77  
78                  JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName());
79                  CronTriggerBean cronTriggerBean = new CronTriggerBean();
80                  boolean valid=   org.quartz.CronExpression.isValidExpression(oleBatchJobBo.getJobCronExpression());
81                  if(valid){
82                      cronTriggerBean.setName(oleBatchJobBo.getJobTriggerName()+"Trigger");
83                      cronTriggerBean.setCronExpression(oleBatchJobBo.getJobCronExpression());
84                      cronTriggerBean.setJobName(jobDetail.getName());
85                      cronTriggerBean.setJobGroup(jobDetail.getGroup());
86                      cronTriggerBean.setJobDetail(jobDetail);
87                      String [] jobNames=scheduler.getJobNames(jobDetail.getGroup());
88                      for (String jobName:jobNames){
89                          if(jobName.equals(jobDetail.getName())) {
90                              scheduler.deleteJob(jobDetail.getName(),jobDetail.getGroup());
91                          }
92                      }
93  
94                      try{
95                          scheduler.scheduleJob(jobDetail, cronTriggerBean);
96                      }
97                      catch (Exception e){
98                          isJobModified=false;
99                          LOG.error("Batch Job Exception :: "+e);
100                     }
101 
102 
103                 }else {
104                     isJobModified=false;
105                     this.putFieldError(OLEConstants.INVALID_CRON, "error.invalid.cron");
106                 }
107 
108             }
109             catch (Exception e) {
110                 isJobModified=false;
111                 LOG.error("Batch Job Exception :: "+e);
112             }
113 
114 
115 
116         }
117 
118         return isJobModified;
119     }
120 }