package com.thinkbiganalytics.scheduler.quartz;


import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;

import java.util.UUID;

/**
 * Configures Quartz to run a job with one step
 */
public class SimpleSchedulerSetup implements InitializingBean {

    @Autowired
    private QuartzScheduler quartzScheduler;

    @Autowired
    private ApplicationContext applicationContext;

    private Object jobRunner;

    private String cronExpression;

    private String targetMethod;

    /**
     * Configure the quartz scheduler
     * @param jobRunner the target job runner (implementing a run() method)
     * @param cronExpression a cron expression for the schedule
     */
    public SimpleSchedulerSetup(Object jobRunner, String cronExpression) {
        this(jobRunner, "run", cronExpression);
    }

    /**
     * Configure the quartz scheduler
     * @param jobRunner the target job runner
     * @param targetMethod the method to invoke on the job runner
     * @param cronExpression a cron expression for the schedule
     *
     */
    public SimpleSchedulerSetup(Object jobRunner, String targetMethod, String cronExpression) {
        this.jobRunner = jobRunner;
        this.cronExpression = cronExpression;
        this.targetMethod = targetMethod;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        MethodInvokingJobDetailFactoryBean jobDetailFactory = new MethodInvokingJobDetailFactoryBean();
        jobDetailFactory.setTargetObject(this.jobRunner);
        jobDetailFactory.setTargetMethod(this.targetMethod);
        applicationContext.getAutowireCapableBeanFactory().initializeBean(jobDetailFactory, UUID.randomUUID().toString());

        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setCronExpression(cronExpression);
        triggerFactoryBean.setJobDetail(jobDetailFactory.getObject());
        applicationContext.getAutowireCapableBeanFactory().initializeBean(triggerFactoryBean, UUID.randomUUID().toString());

        quartzScheduler.scheduleJob(jobDetailFactory, triggerFactoryBean);
    }
}

@@ -745,16 +745,75 @@
 ethod;%0A%0A
+    private String jobName;%0A    private String groupName;%0A%0A
     /**%0A
@@ -1885,16 +1885,115 @@
 ethod);%0A
+        jobDetailFactory.setName(this.jobName);%0A        jobDetailFactory.setGroup(this.groupName);%0A
         
@@ -2099,32 +2099,32 @@
 ().toString());%0A
-
 %0A        CronTri
@@ -2323,16 +2323,130 @@
 ect());%0A
+        triggerFactoryBean.setGroup(this.groupName);%0A        triggerFactoryBean.setName(%22trigger_%22+this.jobName);%0A
         
@@ -2555,32 +2555,32 @@
 ).toString());%0A%0A
-
         quartzSc
@@ -2643,11 +2643,187 @@
 );%0A    %7D
+%0A%0A    public void setJobName(String jobName) %7B%0A        this.jobName = jobName;%0A    %7D%0A%0A    public void setGroupName(String groupName) %7B%0A        this.groupName = groupName;%0A    %7D
 %0A%7D%0A

fix some issues with JDBC job and quartz stuff