All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.amherst.acdc.orchard.AppleTreeConverter.clj Maven / Gradle / Ivy

There is a newer version: 0.1.3
Show newest version
;; Copyright Amherst College
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;     http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

(ns edu.amherst.acdc.orchard.AppleTreeConverter
  (:import (org.apache.commons.codec.digest DigestUtils))
  (:require [clojure.string :as str])
  (:require [clojure.tools.logging :as log])
  (:gen-class
    :name edu.amherst.acdc.orchard.AppleTreeConverter
    :init init
    :state state
    :constructors {[] []
                   [int int] []}
    :extends org.fcrepo.kernel.api.identifiers.InternalIdentifierConverter))

(defn basePath
  "Remove any URI fragment from the path"
  [id]
  (first (str/split id #"#")))

(defn digest
  "Generate an MD5 digest value for an ID value, ignoring any hash values"
  [id]
  (DigestUtils/md5Hex (basePath id)))

(defn treeFromId
  "Generate an MD5-based tree from an ID value"
  [len segments id]
  (if (str/blank? id) id
    (str/join "/"
        (list (->> (digest id) (partition len) (map str/join) (take segments) (str/join "/")) id))))

(defn expandTree
  "Expand an id into a tree form"
  [len segments id]
  (->> (str/split id #"/") (map (partial treeFromId len segments)) (str/join "/")))

(defn collapseTree
  "Collapse a tree form into a simple id"
  [len segments id]
  (str/join "/" (->> (str/split id #"/") (partition (+ segments 1)) (map last))))

(defn forward
  "Handle forward processing (external to internal)"
  [len segments id]
  (if (str/starts-with? id "/") (str "/" (forward len segments (subs id 1))) (expandTree len segments id)))

(defn backward
  "Handle backward processing (internal to external)"
  [len segments id]
  (if (str/starts-with? id "/") (str "/" (backward len segments (subs id 1))) (collapseTree len segments id)))

(defn -init
  ([] [[] (ref {:len 2 :segments 4})])
  ([len segments] [[] (ref {:len len :segments segments})]))

(defn -doForward [this id]
  (let [internal (forward (@(.state this) :len) (@(.state this) :segments) id)]
    (log/debug "Converting" id "to" internal)
    internal))

(defn -doBackward [this id]
  (let [external (backward (@(.state this) :len) (@(.state this) :segments) id)]
    (log/debug "Converting" id "to" external)
    external))





© 2015 - 2024 Weber Informatics LLC | Privacy Policy