 (ns mdr2.abacus
   (:require [clj-xpath.core :refer [xml->doc $x:text]]
-            [clojure.java.io :as io]
+            [clojure.java.io :refer [file delete-file]]
             [immutant.messaging :as msg])
   (:import javax.xml.XMLConstants
            org.xml.sax.SAXException

   ;; :source_publisher "MetaData/sbs/verlag"
   })

(defn read-file [file]
  (let [xml (-> file slurp xml->doc)]
    (into {} (for [[key path] param-mapping] 
               [key ($x:text (str root path) xml)]))))

(defn valid? [file]
  ;; basically a minimal port of
  ;; http://stackoverflow.com/questions/15732/whats-the-best-way-to-validate-an-xml-file-against-an-xsd-file
  (let [validator (.newValidator

      true
      (catch SAXException e false))))

(defn import-file
  "Import new productions from ABACUS and put them on the create queue"
  []
  (doseq [f (filter #(.isFile %)
                    (file-seq (file import-dir)))]
    (when (valid? f)
      (msg/publish "queue.create" (read-file f))
      (delete-file f))))
