package de.nava.demo.config;

import de.nava.demo.job.MyJobOne;
import de.nava.demo.job.MyJobTwo;
import org.joda.time.DateTime;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Trigger;
import org.quartz.impl.JobDetailImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.PostConstruct;

import java.util.Arrays;
import java.util.HashSet;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

@Configuration
public class JobConfiguration {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @PostConstruct
    private void initialize() throws Exception {
        schedulerFactoryBean.getScheduler().addJob(jobDetailMyJobOne(), true, true);
        schedulerFactoryBean.getScheduler().scheduleJob(simpleTriggerMyJobOne());

        schedulerFactoryBean.getScheduler().addJob(jobDetailMyJobTwo(), true, true);
        schedulerFactoryBean.getScheduler().scheduleJob(cronTriggerMyJobTwo());
        /*
          schedulerFactoryBean.getScheduler().scheduleJob(jobDetailMyJobTwo(),
                new HashSet<>(Arrays.asList(cronTriggerMyJobTwo())), true);
          throws:
              caused by: java.lang.UnsupportedOperationException: null
	            at com.novemberain.quartz.mongodb.MongoDBJobStore.storeJobsAndTriggers(MongoDBJobStore.java:172) ~[quartz-mongodb-2.0.0-rc1.jar:na]
	            at org.quartz.core.QuartzScheduler.scheduleJobs(QuartzScheduler.java:1056) ~[quartz-2.2.2.jar:na]
        */
    }

    private static JobDetail jobDetailMyJobOne() {
        JobDetailImpl jobDetail = new JobDetailImpl();
        jobDetail.setKey(new JobKey("jobone", "mygroup"));
        jobDetail.setJobClass(MyJobOne.class);
        // remain stored in the job store even if no triggers point to it anymore
        jobDetail.setDurability(true);
        return jobDetail;
    }

    private static Trigger simpleTriggerMyJobOne() {
        return newTrigger()
                .forJob(jobDetailMyJobOne())
                .withIdentity("trigger1", "mygroup")
                .withPriority(50)
                // Job is scheduled for 3+1 times with the interval of 30 seconds
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(30)
                        .withRepeatCount(3))
                .startAt(DateTime.now().plusSeconds(3).toDate())
                .build();
    }

    // ~~

    private static JobDetail jobDetailMyJobTwo() {
        JobDetailImpl jobDetail = new JobDetailImpl();
        jobDetail.setKey(new JobKey("jobtwo", "mygroup"));
        jobDetail.setJobClass(MyJobTwo.class);
        jobDetail.setDurability(true);
        JobDataMap map = new JobDataMap();
        map.put("name", "HaHa");
        map.put(MyJobTwo.COUNT, 1);
        jobDetail.setJobDataMap(map);
        return jobDetail;
    }

    private static Trigger cronTriggerMyJobTwo() {
        return newTrigger()
                .forJob(jobDetailMyJobTwo())
                .withIdentity("trigger2", "mygroup")
                .withPriority(100)
                // Job is scheduled for every 1 minute
                .withSchedule(cronSchedule("0 0/1 * 1/1 * ? *"))
                .startAt(DateTime.now().plusSeconds(3).toDate())
                .build();
    }

}
@@ -144,100 +144,9 @@
 rtz.
-JobDataMap;%0Aimport org.quartz.JobDetail;%0Aimport org.quartz.JobKey;%0Aimport org.quartz.Trigger
+*
 ;%0Aim
@@ -417,60 +417,8 @@
 t;%0A%0A
-import java.util.Arrays;%0Aimport java.util.HashSet;%0A%0A
 impo
@@ -860,32 +860,37 @@
  true);%0A        
+if (!
 schedulerFactory
@@ -913,132 +913,66 @@
 r().
-s
 che
-duleJob(simpleTriggerMyJobOne());%0A%0A        schedulerFactoryBean.getScheduler().addJob(jobDetailMyJobTwo(), true, true);%0A
+ckExists(new TriggerKey(%22trigger1%22, %22mygroup%22))) %7B%0A    
     
@@ -1023,20 +1023,22 @@
 duleJob(
-cron
+simple
 TriggerM
@@ -1041,19 +1041,19 @@
 gerMyJob
-Two
+One
 ());%0A   
@@ -1061,13 +1061,11 @@
     
-/*%0A  
+%7D%0A%0A
     
@@ -1100,32 +1100,27 @@
 Scheduler().
-schedule
+add
 Job(jobDetai
@@ -1135,76 +1135,13 @@
 o(),
-%0A                new HashSet%3C%3E(Arrays.asList(cronTriggerMyJobTwo()))
+ true
 , tr
@@ -1157,272 +1157,139 @@
     
- 
- throws:%0A              caused by: java.lang.UnsupportedOperationException: null%0A%09            at com.novemberain.quartz.mongodb.MongoDBJobStore.storeJobsAndTriggers(MongoDBJobStore.java:172) ~%5Bquartz-mongodb-2.0.0-rc1.jar:na%5D%0A%09            at org.quartz.core.Quartz
+if (!schedulerFactoryBean.getScheduler().checkExists(new TriggerKey(%22trigger2%22, %22mygroup%22))) %7B%0A            schedulerFactoryBean.get
 Sche
@@ -1293,16 +1293,18 @@
 cheduler
+()
 .schedul
@@ -1311,59 +1311,32 @@
 eJob
-s(QuartzScheduler.java:1056) ~%5Bquartz-2.2.2.jar:na%5D
+(cronTriggerMyJobTwo());
 %0A   
@@ -1344,10 +1344,9 @@
     
-*/
+%7D
 %0A   

Verify if trigger is already registered in job store, otherwise skip configuration.