From 29b71aea14bbf58a39a265d2388fe30bb67e9d13 Mon Sep 17 00:00:00 2001 From: Timothy Washington Date: Sat, 10 Jul 2010 13:09:18 -0400 Subject: [PATCH] -- abstrcting out code for an HTTP call -- making successful RESTful call for bkeeping 'load' command --- src/helpers.clj | 17 +++++ src/opts_handler.clj | 168 +++++++++++++++++++++++--------------------------- src/xpath_handler.clj | 28 ++++++--- 3 files changed, 114 insertions(+), 99 deletions(-) rewrite src/opts_handler.clj (96%) diff --git a/src/helpers.clj b/src/helpers.clj index 5c15540..435d16b 100644 --- a/src/helpers.clj +++ b/src/helpers.clj @@ -55,6 +55,7 @@ "account" "com/interrupt/bookkeeping/account" "journal" "com/interrupt/bookkeeping/journal" "entry" "com/interrupt/bookkeeping/journal" + "entries" "com/interrupt/bookkeeping/journal" "debit" "com/interrupt/bookkeeping/account" "credit" "com/interrupt/bookkeeping/account" } @@ -62,3 +63,19 @@ ) ) +(defn execute-http-call [ db-full-PARENT db-leaf db-query ] + + (println "DEBUG > db-query[" db-query "]") + (println "DEBUG > FINAL http query[" (str db-full-PARENT "/" db-leaf "?" (url-encode db-query) ) "]") + + ;; from DB, get 'token' for 'option' args & value + (def result-XML (clojure.contrib.http.agent/string (clojure.contrib.http.agent/http-agent (str db-full-PARENT "/" db-leaf "?" (url-encode db-query) ) + :method "GET" + :header {"Content-Type" "text/xml"} + + ;; TODO - parse results, check for i) null or ii) multiple results + ) + )) +) + + diff --git a/src/opts_handler.clj b/src/opts_handler.clj dissimilarity index 96% index 1f402cf..4e71066 100644 --- a/src/opts_handler.clj +++ b/src/opts_handler.clj @@ -1,90 +1,78 @@ - - (defn option_handler [node handler] - (if (instance? com.interrupt.bookkeeping.cc.node.AOptsCommandInput (. node getCommandInput) ) - (do - (println "DEBUG > OPTIONS input > token[" (.. node getCommandInput getInputOption getCommandtoken) "] > options[" (.. node getCommandInput getInputOption getCommandoption) "]") - - ;; get token string (ie user, entry, etc) -> - (def token (.. node getCommandInput getInputOption getCommandtoken)) - - ;; get option args & value -> use a 'CommandOptionVisitor' - (def options (seq (.. node getCommandInput getInputOption getCommandoption))) - - (def option-id - (take 1 (filter - (fn [input] - (if (instance? com.interrupt.bookkeeping.cc.node.AIdCommandoption input ) - (true? true) - (false? false) - ) - ) - options - )) - ) - (def db-id-ID ;; TODO - chain this to look for other options if 'id' is not there - - (clojure.contrib.str-utils2/trim - (nth - (clojure.contrib.str-utils2/split (.. (nth option-id 0) ;; class 'com.interrupt.bookkeeping.cc.node.AIdCommandoption' - getIdOpt getText) #"-[a-z]+") - 1 - ) - ) - ) - - (println "DEBUG > extracted > [" token "] > [" options "] > [" db-id-ID "]") - - ;; from HASH -> find containing folder for token - (def db-working-DIR (working-dir-lookup (.. token toString trim))) - - ;; build another to end of db-working-DIR - (def db-leaf (str (.. token toString trim) "." db-id-ID ) ) - (def db-full-PARENT (str db-base-URL db-system-DIR db-working-DIR "/" db-leaf )) - - (def db-document-NAME db-leaf) - - - (println "DEBUG > db-base-URL["db-base-URL"] > db-system-DIR["db-system-DIR"] > db-working-DIR["db-working-DIR"] > leaf["db-leaf"]") - (println "DEBUG > db-base-URL[" db-full-PARENT "]") - - - ;; this will find all elements in the collection /db/shakespeare with "Juliet" as the - ;; http://localhost:8080/exist/rest/db/shakespeare?_query=//SPEECH[SPEAKER=%22JULIET%22] - - ;; build XPATH expression to find 'token' based on option - ;; http://localhost:8080/exist/rest/db/two.xml?_query= - ;; declare default element namespace 'com/interrupt/bookkeeping/users' - ;; declare namespace aauth='com/interrupt/bookkeeping/cc/bkell/aauth'; - ;; //system/aauth:aauthentication - - ;; TODO - a check if we even need a query - (def db-query (str "_wrap=no&_query=" - "declare default element namespace '"(namespace-lookup (.. token toString trim)) "';" - ;;"declare namespace users='com/interrupt/bookkeeping/users'; declare namespace bkell='com/interrupt/bookkeeping/cc/bkell'; declare namespace command='com/interrupt/bookkeeping/cc/bkell/command'; declare namespace interpret='com/interrupt/bookkeeping/interpret'; declare namespace aauth='com/interrupt/bookkeeping/cc/bkell/aauth'; " - - ;; TODO - check if we need 'and' conditions - ;; "**/[ @option='option_value' [ and @option='option_value' ] ]" - "//"(.. token toString trim)"[ @" - (. (nth (re-seq #"-[a-z]+" (.. (nth option-id 0) getIdOpt getText)) 0) substring 1) ;; TODO - put this part into a function (being re-used) - "='"db-id-ID"']" - ) - - ) - (println "DEBUG > db-query[" db-query "]") - - (println "DEBUG > FINAL http query[" (str db-full-PARENT "/" db-leaf "?" (url-encode db-query) ) "]") - - ;; from DB, get 'token' for 'option' args & value - (def result-XML (clojure.contrib.http.agent/string (clojure.contrib.http.agent/http-agent (str db-full-PARENT "/" db-leaf "?" (url-encode db-query) ) - :method "GET" - :header {"Content-Type" "text/xml"} - - ;; TODO - parse results, check for i) null or ii) multiple results - ) - )) - - ;; pass built XML sequence to handler - (handler (xml-seq result-XML)) - ) - ) - ) + + (defn option_handler [node handler] + (if (instance? com.interrupt.bookkeeping.cc.node.AOptsCommandInput (. node getCommandInput) ) + (do + (println "DEBUG > OPTIONS input > token[" (.. node getCommandInput getInputOption getCommandtoken) "] > options[" (.. node getCommandInput getInputOption getCommandoption) "]") + + ;; get token string (ie user, entry, etc) -> + (def token (.. node getCommandInput getInputOption getCommandtoken)) + + ;; get option args & value -> use a 'CommandOptionVisitor' + (def options (seq (.. node getCommandInput getInputOption getCommandoption))) + + (def option-id + (take 1 (filter + (fn [input] + (if (instance? com.interrupt.bookkeeping.cc.node.AIdCommandoption input ) + (true? true) + (false? false) + ) + ) + options + )) + ) + (def db-id-ID ;; TODO - chain this to look for other options if 'id' is not there + + (clojure.contrib.str-utils2/trim + (nth + (clojure.contrib.str-utils2/split (.. (nth option-id 0) ;; class 'com.interrupt.bookkeeping.cc.node.AIdCommandoption' + getIdOpt getText) #"-[a-z]+") + 1 + ) + ) + ) + + (println "DEBUG > extracted > [" token "] > [" options "] > [" db-id-ID "]") + + ;; from HASH -> find containing folder for token + (def db-working-DIR (working-dir-lookup (.. token toString trim))) + + ;; build another to end of db-working-DIR + (def db-leaf (str (.. token toString trim) "." db-id-ID ) ) + (def db-full-PARENT (str db-base-URL db-system-DIR db-working-DIR "/" db-leaf )) + + (def db-document-NAME db-leaf) + + + (println "DEBUG > db-base-URL["db-base-URL"] > db-system-DIR["db-system-DIR"] > db-working-DIR["db-working-DIR"] > leaf["db-leaf"]") + (println "DEBUG > db-base-URL[" db-full-PARENT "]") + + + ;; this will find all elements in the collection /db/shakespeare with "Juliet" as the + ;; http://localhost:8080/exist/rest/db/shakespeare?_query=//SPEECH[SPEAKER=%22JULIET%22] + + ;; build XPATH expression to find 'token' based on option + ;; http://localhost:8080/exist/rest/db/two.xml?_query= + ;; declare default element namespace 'com/interrupt/bookkeeping/users' + ;; declare namespace aauth='com/interrupt/bookkeeping/cc/bkell/aauth'; + ;; //system/aauth:aauthentication + + ;; TODO - a check if we even need a query + (def db-query (str "_wrap=no&_query=" + "declare default element namespace '"(namespace-lookup (.. token toString trim)) "';" + ;;"declare namespace users='com/interrupt/bookkeeping/users'; declare namespace bkell='com/interrupt/bookkeeping/cc/bkell'; declare namespace command='com/interrupt/bookkeeping/cc/bkell/command'; declare namespace interpret='com/interrupt/bookkeeping/interpret'; declare namespace aauth='com/interrupt/bookkeeping/cc/bkell/aauth'; " + + ;; TODO - check if we need 'and' conditions + ;; "**/[ @option='option_value' [ and @option='option_value' ] ]" + "//"(.. token toString trim)"[ @" + (. (nth (re-seq #"-[a-z]+" (.. (nth option-id 0) getIdOpt getText)) 0) substring 1) ;; TODO - put this part into a function (being re-used) + "='"db-id-ID"']" + ) + + ) + + (handler (xml-seq (execute-http-call db-full-PARENT db-leaf db-query))) + + ) + ) + ) diff --git a/src/xpath_handler.clj b/src/xpath_handler.clj index 6beeecc..3fe2f2c 100644 --- a/src/xpath_handler.clj +++ b/src/xpath_handler.clj @@ -243,19 +243,29 @@ ;; 1.2 build an xpath parser (def tree (.parse (get-pushback-parser input-string))) - (. tree apply (get-adapter-proxy ) ) ;; 2. token - find i) leaf document to search ii) root & xpath expression to feed to RESTful exist - ;; 3. build RESTful call - ;; 4. make RESTful call - ;; 5. pass result sequece to handler + (. tree apply (get-adapter-proxy ) ) + + ;; 3. build RESTful call + (def db-query (str "_wrap=no&_query=" + "declare default element namespace '"(namespace-lookup (:leaf-node (deref xpath-data))) "';" + + ;; TODO - check if we need 'and' conditions + ;; "**/[ @option='option_value' [ and @option='option_value' ] ]" + "//"(:leaf-node (deref xpath-data))"[ @" predicate-name "='" predicate-value "']" + ) + + ) + - ;; extract the context - (let [ result_seq []] - - ;; operate with handler - (handler result_seq) + (def db-full-PARENT (str db-base-URL "rootDir/" (:context-dir (deref xpath-data)))) + + ;; 4. make RESTful call & 5. pass result sequece to handler + (handler (xml-seq + (execute-http-call db-full-PARENT (:leaf-document-name (deref xpath-data)) db-query)) ) + ) ) ) -- 2.11.4.GIT