package org.ovirt.engine.core.utils.timer;

import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Singleton;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.impl.matchers.GroupMatcher.jobGroupEquals;

@Singleton
public class SchedulerUtilQuartzImpl extends SchedulerUtilBaseImpl {

    /**
     * This method is called upon the bean creation as part
     * of the management Service bean lifecycle.
     */
    @Override
    @PostConstruct
    public void create() {
        setup();
    }

    /*
     * retrieving the quartz scheduler from the factory.
     */
    public void setup() {
        try {
            SchedulerFactory sf = new StdSchedulerFactory();
            sched = sf.getScheduler();
            sched.start();
            sched.getListenerManager()
                    .addJobListener(new FixedDelayJobListener(this), jobGroupEquals(Scheduler.DEFAULT_GROUP));
        } catch (SchedulerException se) {
            log.error("there is a problem with the underlying Scheduler: {}", se.getMessage());
            log.debug("Exception", se);
        }
    }

    @PreDestroy
    public void teardown() {
        try {
            if (sched != null) {
                sched.shutdown();
            }
        } catch (SchedulerException e) {
            log.error("Failed to shutdown Quartz service: {}", e.getMessage());
            log.debug("Exception", e);
        }
    }

    @Override
    protected JobDetail createJobWithBasicMapValues(Object instance,
            String methodName,
            Class<?>[] inputTypes,
            Object[] inputParams) {
        String jobName = generateUniqueNameForInstance(instance, methodName);
        JobDetail job = newJob()
                .withIdentity(jobName, Scheduler.DEFAULT_GROUP)
                .ofType(JobWrapper.class)
                .build();
        setBasicMapValues(job.getJobDataMap(), instance, methodName, inputTypes, inputParams);
        return job;
    }

    private void setBasicMapValues(JobDataMap data,
            Object instance,
            String methodName,
            Class<?>[] inputTypes,
            Object[] inputParams) {
        data.put(RUNNABLE_INSTANCE, instance);
        data.put(RUN_METHOD_NAME, methodName);
        data.put(RUN_METHOD_PARAM, inputParams);
        data.put(RUN_METHOD_PARAM_TYPE, inputTypes);
    }

}

@@ -48,37 +48,14 @@
 ort 
-org.quartz.JobDataMap;%0Aimport
+static
  org
@@ -65,22 +65,30 @@
 artz.Job
-Detail
+Builder.newJob
 ;%0Aimport
@@ -80,32 +80,39 @@
 .newJob;%0Aimport 
+static 
 org.quartz.Sched
@@ -110,135 +110,49 @@
 rtz.
-Scheduler;%0Aimport org.quartz.SchedulerException;%0Aimport org.quartz.SchedulerFactory;%0Aimport org.quartz.impl.StdSchedulerFactory
+impl.matchers.GroupMatcher.jobGroupEquals
 ;%0A%0Ai
@@ -256,38 +256,61 @@
 gleton;%0A%0Aimport 
-static
+org.quartz.JobDataMap;%0Aimport
  org.quartz.JobB
@@ -304,38 +304,30 @@
 g.quartz.Job
-Builder.newJob
+Detail
 ;%0Aimport sta
@@ -315,39 +315,32 @@
 bDetail;%0Aimport 
-static 
 org.quartz.impl.
@@ -338,49 +338,135 @@
 rtz.
-impl.matchers.GroupMatcher.jobGroupEquals
+Scheduler;%0Aimport org.quartz.SchedulerException;%0Aimport org.quartz.SchedulerFactory;%0Aimport org.quartz.impl.StdSchedulerFactory
 ;%0A%0A@

Fix imports order to fit project standards in scheduler