 (defn save-request
   "it returns name of a directory where payload is saved"
   [[[metadata payload] workdir]]
-  (spit (io/file workdir "payload.bin") payload)
-  (.mkdir (io/file workdir "payload"))
-  (spit (io/file workdir "metadata.clj") metadata)
+  (fs/mkdirs (io/file workdir "request" "payload"))
+  (spit (io/file workdir "request" "payload.bin") payload)
+  (spit (io/file workdir "request" "metadata.clj") metadata)
   (let [msg (json/read-str (String. payload) :key-fn keyword) 
-        marcxml-file (io/file  workdir "payload" "oai_marc.xml")]
+        marcxml-file (io/file  workdir "request" "payload" "oai_marc.xml")]
     (log/info "save export request to" workdir)  
     (spit (-> marcxml-file .toString (.concat ".b64")) (:b64_marcxml msg))
-    (spit (io/file workdir "payload" "uuid") (:uuid msg))
-    (spit (io/file workdir "payload" "urnnbn") (:urnnbn msg))
-    (spit (io/file workdir "payload" "aleph_id") (:aleph_id msg))
-    (spit (io/file workdir "payload" "isbn") (:isbn msg))
-    (spit (io/file workdir "payload" "location-at-kramerius") (:location_at_kramerius msg))
-    (spit (io/file workdir "payload" "is-private") (:is_private msg))
-    (spit (io/file workdir "payload" "edeposit-url.txt") (:edeposit_url msg))
+    (spit (io/file workdir "request" "payload" "uuid") (:uuid msg))
+    (spit (io/file workdir "request" "payload" "urnnbn") (:urnnbn msg))
+    (spit (io/file workdir "request" "payload" "aleph_id") (:aleph_id msg))
+    (spit (io/file workdir "request" "payload" "isbn") (:isbn msg))
+    (spit (io/file workdir "request" "payload" "location-at-kramerius") (:location_at_kramerius msg))
+    (spit (io/file workdir "request" "payload" "is-private") (:is_private msg))
+    (spit (io/file workdir "request" "payload" "edeposit-url.txt") (:edeposit_url msg))
     (with-open [out (io/output-stream marcxml-file)]
       (.write out (Base64/decodeBase64 (:b64_marcxml msg))))
     
-    (let [out-dir (io/file workdir "payload" "first-page")
+    (let [out-dir (io/file workdir "request" "payload" "first-page")
           first-page (-> msg :first_page)
           ]
       (.mkdir out-dir)

(ns edeposit.amqp.kramerius.handlers
  (:require [me.raynes.fs :as fs]
            [langohr.basic     :as lb]
            [clojure.data.json :as json]
            [clojure.java.io :as io]
            [clojure.tools.logging :as log]
            [clojure.pprint :as pp]
            [clojure.data.json :as json]
            [clojure.data.xml :as xml]
            [clj-time.core :as t]
            [edeposit.amqp.kramerius.xml.mods :as m]
            [edeposit.amqp.kramerius.xml.foxml :as f]
            [edeposit.amqp.kramerius.xml.utils :as u]
            [clojure.java.shell :as shell]
            [clojurewerkz.serialism.core :as s]
            )
  (:import [org.apache.commons.codec.binary Base64])
  )



(defn add-urnnbn-to-mods
  [[mods workdir]]
  (let [urnnbn (slurp (io/file workdir "request" "payload" "urnnbn"))]
    [(map (fn [one-mods] (m/with-urnnbn-identifier one-mods urnnbn)) mods) workdir]))

(defn mods->oai_dcs
  [[mods workdir]]
  [(map m/mods->oai_dc mods) workdir])

(defn make-package-with-foxml
  [[mods mods-workdir] [oai_dcs oai-workdir] workdir]
  {:pre [(= workdir mods-workdir oai-workdir)]}
  (let [payload-dir (io/file workdir "request" "payload")
        uuid (slurp (io/file payload-dir "uuid"))
        result-dir (io/file workdir uuid)
        full-file {:mimetype (slurp (io/file payload-dir "original" "mimetype"))
