   (:import [org.apache.commons.codec.binary Base64])
   )
 
+; Michal Merka 736 539 653
 (comment ;; hook for emacs
   (add-hook 'after-save-hook 'restart-app nil t)
 )

  "It creates tmpdir and add it to request data"
  [[metadata payload]]
  (let [tmpdir (fs/temp-dir "test-export-to-kramerius-request-")]
    [[metadata payload] tmpdir]
    )
  )

(defn save-request
  "it returns name of a directory where payload is saved"
  [[[metadata payload] workdir]]
  (def msg (json/read-str (String. payload) :key-fn keyword))
  (def marcxml-file (io/file workdir "oai_marc.xml"))
  (def data-file (->> (:filename msg)
                     io/file
                     .getName
                     (io/file workdir)
                     )
    )

  (log/info "save export request to" workdir)  
  (spit (-> marcxml-file .toString (.concat ".b64")) (:b64_marcxml msg))
  (spit (-> data-file .toString (.concat ".b64")) (:b64_data msg))
  (spit (io/file workdir "uuid") (:uuid msg))
  (spit (io/file workdir "filename") (:filename  msg))

  (with-open [out (io/output-stream marcxml-file)]
    (.write out (Base64/decodeBase64 (:b64_marcxml msg))))
  (with-open [out (io/output-stream data-file)]
    (.write out (Base64/decodeBase64 (:b64_data msg))))

  workdir
  )

(defn prepare-marcxml2mods-request
  [workdir]

    (let [ mods_files (-> payload 
         (json/read-str :key-fn keyword) 
         :mods_files)]
      [mods_files workdir])))

(defn parse-mods-files
  "takes strings and returns xml root for each xml string"
  [[mods_files workdir]]
  [(map xml/parse-str mods_files) workdir])

(defn save-img-files
  [[mods_files workdir]]
  (let [uuid (slurp (io/file workdir "uuid"))
        slurp-img-full (comp slurp (partial io/file workdir "img_full"))
        slurp-img-preview (comp slurp (partial io/file workdir "img_preview"))
        ]
    (.mkdir (io/file workdir uuid))
    (.mkdir (io/file workdir uuid "img"))
    (fs/copy (io/file workdir "img_full" (slurp-img-full "filename")) 
             (io/file workdir uuid "img" (slurp-img-full "filename")))
    (fs/copy (io/file workdir "img_preview" (slurp-img-preview "filename")) 
             (io/file workdir uuid "img" (slurp-img-preview "filename")))
    [{:img-type :img_full
      :mime-type (slurp-img-full "mimetype")
      :filename (slurp-img-full "filename")
      }
     {:img-type :img_preview
      :mime-type (slurp-img-preview "mimetype")
      :filename (slurp-img-preview "filename")
      } 
     workdir]))

(defn add-urnnbn-to-first-mods
  [[mods workdir]]
  (let [urnnbn (slurp (io/file workdir "urnnbn"))]
    [(into [(mods/with-urnnbn-identifier (first mods) urnnbn)] (rest mods))  workdir]))

(defn mods->oai_dcs
  [[mods workdir]]
