 
 (defn generate-report [params]
   "Returns the cached report for the given parameters, or schedules the report for generation"
-  (if-let [f (get-report-by-id (report-id params))]
+  (if-let [file (get-report-by-id (report-id params))]
    {"status" "OK"
-    "file" f}
+    "file" file}
    (schedule-job params)))
 
 (defn invalidate-cache [params]

(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)]))

(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) #"/")))]
    (dosync
      (alter cache conj {{:id reportId
                          :surveyId surveyId
                          :baseURL baseURL} path}))
    (qs/delete-job (j/key reportId))))

(defn- get-executing-jobs []
  "Returns a list of executing jobs in form of JobExecutionContext"
    (.getCurrentlyExecutingJobs @qs/*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- job-executing? [k]
  "Returns true if there is a running job for that particular key"
  (if (empty? (filter-executing-jobs k))
      false true))

(defn- report-id [m]
  "Generates a unique identifier based on the map"


(defn- get-job [jtype id params]
  "Returns a Job specification for the given parameters"
  (jobs/build
    (jobs/of-type jtype)
    (jobs/using-job-data (conj params {"reportId" id} ))
    (jobs/with-identity (jobs/key id))))

(defn- get-trigger [id]
  "Returns a Trigger to be executed now"
  (triggers/build
    (triggers/with-identity (triggers/key id))
    (triggers/start-now)))

(defn- schedule-job [params]
  "Schedule a report for generation. For concurrent requests only schedules the report
