/*
 * CODENVY CONFIDENTIAL
 * __________________
 *
 *  [2012] - [2014] Codenvy, S.A.
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Codenvy S.A. and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Codenvy S.A.
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Codenvy S.A..
 */
package com.codenvy.im;

import com.codenvy.im.restlet.InstallationManager;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Named;
import java.io.IOException;

/** @author Anatoliy Bazko */
@Singleton
public class UpdateManager {
    private static final Logger LOG = LoggerFactory.getLogger(UpdateManager.class);

    private final String              schedule;
    private final InstallationManager manager;

    private Scheduler scheduler;

    @Inject
    public UpdateManager(@Named("installation-manager.daemon.schedule") String schedule,
                         InstallationManager manager) throws IOException {
        this.schedule = schedule;
        this.manager = manager;
        LOG.info("Cron schedule: " + schedule);
    }

    @PostConstruct
    public void init() throws SchedulerException {
        scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();

        JobDetailImpl jobDetail = new JobDetailImpl();
        jobDetail.setKey(new JobKey(IMJob.class.getName()));
        jobDetail.setJobClass(IMJob.class);
        jobDetail.setDurability(true);

        scheduler.scheduleJob(jobDetail, TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule(schedule)).build());

    }

    @PreDestroy
    public void destroy() throws SchedulerException {
        if (scheduler != null) {
            scheduler.shutdown(true);
        }
    }

    /** Job to check and download updates. */
    public class IMJob implements Job {
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            // do nothing yet
        }
    }
}

@@ -796,17 +796,19 @@
 .quartz.
-*
+Job
 ;%0Aimport
@@ -823,70 +823,135 @@
 rtz.
-impl.JobDetailImpl;%0Aimport org.quartz.impl.StdSchedulerFactory
+JobExecutionContext;%0Aimport org.quartz.JobExecutionException;%0Aimport org.quartz.Scheduler;%0Aimport org.quartz.SchedulerException
 ;%0Aim
@@ -1781,32 +1781,34 @@
 ulerException %7B%0A
+//
         schedule
@@ -1849,24 +1849,26 @@
 cheduler();%0A
+//
         sche
@@ -1882,17 +1882,21 @@
 tart();%0A
-%0A
+//%0A//
         
@@ -1942,16 +1942,18 @@
 Impl();%0A
+//
         
@@ -2005,16 +2005,18 @@
 me()));%0A
+//
         
@@ -2051,16 +2051,18 @@
 class);%0A
+//
         
@@ -2092,17 +2092,21 @@
 (true);%0A
-%0A
+//%0A//
         
@@ -2233,17 +2233,16 @@
 ild());%0A
-%0A
     %7D%0A%0A 
@@ -2302,32 +2302,34 @@
 ulerException %7B%0A
+//
         if (sche
@@ -2345,16 +2345,18 @@
 null) %7B%0A
+//
         
@@ -2381,24 +2381,26 @@
 down(true);%0A
+//
         %7D%0A  

Disable scheduler