001package org.kuali.ole.batch.rule; 002 003import org.apache.log4j.Logger; 004import org.kuali.ole.OLEConstants; 005import org.kuali.ole.deliver.bo.OleBatchJobBo; 006import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 007import org.kuali.rice.krad.maintenance.MaintenanceDocument; 008import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase; 009import org.quartz.JobDetail; 010import org.quartz.SchedulerException; 011import org.quartz.impl.StdScheduler; 012import org.springframework.scheduling.quartz.CronTriggerBean; 013 014/** 015 * Created with IntelliJ IDEA. 016 * User: ? 017 * Date: 3/1/13 018 * Time: 8:18 PM 019 * To change this template use File | Settings | File Templates. 020 */ 021public class OleBatchJobRule extends MaintenanceDocumentRuleBase { 022 023 private static final Logger LOG = Logger.getLogger(OleBatchJobRule.class); 024 @Override 025 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) { 026 boolean isValid = true; 027 OleBatchJobBo oleBatchJobBo = (OleBatchJobBo) document.getNewMaintainableObject().getDataObject(); 028 029 isValid &= validateCronExpression(oleBatchJobBo); 030 return isValid; 031 } 032 033 /** 034 * This method validates duplicate completeness Id and return boolean value. 035 * @param oleBatchJobBo 036 * @return boolean 037 */ 038 private boolean validateCronExpression(OleBatchJobBo oleBatchJobBo) { 039 040 boolean isJobModified=true; 041 StdScheduler scheduler = (StdScheduler) GlobalResourceLoader.getService("scheduler"); 042 043 if(!oleBatchJobBo.isJobEnableStatus()){ 044 JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName()); 045 try { 046 scheduler.deleteJob(jobDetail.getName(),jobDetail.getGroup()) ; 047 } catch (SchedulerException e) { 048 isJobModified=false; 049 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 050 } 051 } 052 else { 053 try { 054 055 JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName()); 056 CronTriggerBean cronTriggerBean = new CronTriggerBean(); 057 boolean valid= org.quartz.CronExpression.isValidExpression(oleBatchJobBo.getJobCronExpression()); 058 if(valid){ 059 cronTriggerBean.setName(oleBatchJobBo.getJobTriggerName()+"Trigger"); 060 cronTriggerBean.setCronExpression(oleBatchJobBo.getJobCronExpression()); 061 cronTriggerBean.setJobName(jobDetail.getName()); 062 cronTriggerBean.setJobGroup(jobDetail.getGroup()); 063 cronTriggerBean.setJobDetail(jobDetail); 064 String [] jobNames=scheduler.getJobNames(jobDetail.getGroup()); 065 for (String jobName:jobNames){ 066 if(jobName.equals(jobDetail.getName())) { 067 scheduler.deleteJob(jobDetail.getName(),jobDetail.getGroup()); 068 } 069 } 070 071 try{ 072 scheduler.scheduleJob(jobDetail, cronTriggerBean); 073 } 074 catch (Exception e){ 075 isJobModified=false; 076 LOG.error("Batch Job Exception :: "+e); 077 } 078 079 080 }else { 081 isJobModified=false; 082 this.putFieldError(OLEConstants.INVALID_CRON, "error.invalid.cron"); 083 } 084 085 } 086 catch (Exception e) { 087 isJobModified=false; 088 LOG.error("Batch Job Exception :: "+e); 089 } 090 091 092 093 } 094 095 return isJobModified; 096 } 097}