001package org.kuali.ole.deliver.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
025    @Override
026    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
027        boolean isValid = true;
028        OleBatchJobBo oleBatchJobBo = (OleBatchJobBo) document.getNewMaintainableObject().getDataObject();
029
030        isValid &= validateCronExpression(oleBatchJobBo);
031        return isValid;
032    }
033
034    /**
035     * This method  validates duplicate completeness Id and return boolean value.
036     *
037     * @param oleBatchJobBo
038     * @return boolean
039     */
040    private boolean validateCronExpression(OleBatchJobBo oleBatchJobBo) {
041
042        boolean isJobModified = true;
043        StdScheduler scheduler = (StdScheduler) GlobalResourceLoader.getService("scheduler");
044
045        if (!oleBatchJobBo.isJobEnableStatus()) {
046            JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName());
047            try {
048                scheduler.deleteJob(jobDetail.getName(), jobDetail.getGroup());
049            } catch (SchedulerException e) {
050                isJobModified = false;
051                LOG.error(e, e);  //To change body of catch statement use File | Settings | File Templates.
052            }
053        } else {
054            try {
055
056                JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleBatchJobBo.getJobTriggerName());
057                CronTriggerBean cronTriggerBean = new CronTriggerBean();
058                boolean valid = org.quartz.CronExpression.isValidExpression(oleBatchJobBo.getJobCronExpression());
059                if (valid) {
060                    cronTriggerBean.setName(oleBatchJobBo.getJobTriggerName() + "Trigger");
061                    cronTriggerBean.setCronExpression(oleBatchJobBo.getJobCronExpression());
062                    cronTriggerBean.setJobName(jobDetail.getName());
063                    cronTriggerBean.setJobGroup(jobDetail.getGroup());
064                    cronTriggerBean.setJobDetail(jobDetail);
065                    String[] jobNames = scheduler.getJobNames(jobDetail.getGroup());
066                    for (String jobName : jobNames) {
067                        if (jobName.equals(jobDetail.getName())) {
068                            scheduler.deleteJob(jobDetail.getName(), jobDetail.getGroup());
069                        }
070                    }
071
072                    try {
073                        scheduler.scheduleJob(jobDetail, cronTriggerBean);
074                    } catch (Exception e) {
075                        isJobModified = false;
076                        LOG.error("Batch Job Exception :: " + e, e);
077                    }
078
079
080                } else {
081                    isJobModified = false;
082                    this.putFieldError(OLEConstants.INVALID_CRON, "error.invalid.cron");
083                }
084
085            } catch (Exception e) {
086                isJobModified = false;
087                LOG.error("Batch Job Exception :: " + e, e);
088            }
089        }
090
091        return isJobModified;
092    }
093}