 (ns reports.scheduler
-  (:require [clojurewerkz.quartzite.scheduler :as qs]
-            [clojurewerkz.quartzite.triggers :as t]
-            [clojurewerkz.quartzite.jobs :as j]
-            [clojurewerkz.quartzite.conversion :as qc]
-            [reports.exporter :as exp]
-            [clojure.string :as s :only (split join)]))
+  (:refer-clojure :exclude [key])
+  (:import java.io.File
+           [org.quartz JobDetail JobExecutionContext JobKey])
+  (:require [clojure.string :as string :only (split join)]
+            [clojurewerkz.quartzite [conversion :as conversion]
+                                    [jobs :as jobs]
+                                    [scheduler :as scheduler]
+                                    [triggers :as triggers]]
+            [reports.exporter :as exporter]))
 
 (def cache (ref {}))
 
-(j/defjob ExportJob [context]
-  (let [{:strs [baseURL exportType surveyId opts reportId]} (qc/from-job-data context)
-        report (exp/doexport exportType baseURL surveyId opts)
-        path (s/join "/" (take-last 2 (s/split (.getAbsolutePath report) #"/")))]
+(jobs/defjob ExportJob [context]
+  (let [{:strs [baseURL exportType surveyId opts reportId]} (conversion/from-job-data context)
+        ^File report (exporter/doexport exportType baseURL surveyId opts)
+        path (string/join "/" (take-last 2 (string/split (.getAbsolutePath report) #"/")))]
     (dosync
       (alter cache conj {{:id reportId
                           :surveyId surveyId
                           :baseURL baseURL} path}))
-    (qs/delete-job (j/key reportId))))
+    (scheduler/delete-job (jobs/key reportId))))
 
-(defn- get-executing-jobs []
-  "Returns a list of executing jobs in form of JobExecutionContext"
-    (.getCurrentlyExecutingJobs @qs/*scheduler*))
+(defn- ^JobExecutionContext get-executing-jobs []
+  "Returns a list of executing jobs in the form of ^org.quartz.JobExecutionContext"
+  (.getCurrentlyExecutingJobs @scheduler/*scheduler*))
 
-(defn- filter-executing-jobs [k]
-  "Filter the list of executing jobs by key (usually just 1 occurrence)"
-  (let [jkey (j/key k)]
-    (filter #(= (.getKey (.getJobDetail %)) jkey) (get-executing-jobs))))
+(defn- filter-executing-jobs [key]
+  "Filter the list of executing jobs by key (usually returns just 1 job)"
+  (filter #(= (.. % ^JobDetail (getJobDetail) ^JobKey (getKey)) (jobs/key key))
+          (get-executing-jobs)))
 
-(defn- job-executing? [k]
+(defn- job-executing? [key]
   "Returns true if there is a running job for that particular key"
-  (if (empty? (filter-executing-jobs k))
-      false true))
+  (if (empty? (filter-executing-jobs key)) false true))
 
 (defn- report-id [m]
   "Generates a unique identifier based on the map"

  (let [baseURL (params "baseURL")]
    (doseq [sid (params "surveyIds")]
      (dosync
        (doseq [k (keys @cache) :when (and (= (k :baseURL) baseURL) (= (str sid) (k :surveyId)))]
        (alter cache dissoc k))))
    "OK"))

  (let [id (report-id params)
        job (get-job ExportJob id params)
        trigger (get-trigger id)]
    (scheduler/maybe-schedule job trigger)
    {"status" "OK"
     "message" "PROCESSING"}))

