-- abstrcting out code for an HTTP call
[bkell-clj.git] / src / opts_handler.clj
blob4e710664456a12048b45d3a85cc507891ea25fde
2                   (defn option_handler [node handler] 
3                      (if (instance? com.interrupt.bookkeeping.cc.node.AOptsCommandInput (. node getCommandInput) )
4                                         (do 
5                                            (println "DEBUG > OPTIONS input > token[" (.. node getCommandInput getInputOption getCommandtoken) "] > options[" (.. node getCommandInput getInputOption getCommandoption) "]")
6                                            
7                                            ;; get token string (ie user, entry, etc) -> 
8                                            (def token (.. node getCommandInput getInputOption getCommandtoken))
9                                            
10                                            ;; get option args & value -> use a 'CommandOptionVisitor' 
11                                            (def options (seq (.. node getCommandInput getInputOption getCommandoption)))
12                                            
13                                            (def option-id  
14                                               (take 1 (filter 
15                                                  (fn [input] 
16                                                     (if (instance? com.interrupt.bookkeeping.cc.node.AIdCommandoption input ) 
17                                                        (true? true)
18                                                        (false? false)
19                                                     )
20                                                  )
21                                                  options
22                                               ))
23                                            )
24                                            (def db-id-ID  ;; TODO - chain this to look for other options if 'id' is not there
25                                               
26                                                                 (clojure.contrib.str-utils2/trim 
27                                                                         (nth  
28                                                                                 (clojure.contrib.str-utils2/split (.. (nth option-id 0)  ;; class 'com.interrupt.bookkeeping.cc.node.AIdCommandoption' 
29                                                                                         getIdOpt getText) #"-[a-z]+")
30                                                                                         1
31                                                                         )
32                                                                 )
33                                            )
34                                            
35                                            (println "DEBUG > extracted > [" token "] > [" options "] > [" db-id-ID "]")
36                                            
37                                            ;; from HASH -> find containing folder for token 
38                                            (def db-working-DIR (working-dir-lookup (.. token toString trim)))
39                                            
40                                            ;; build another <my.group> to end of db-working-DIR 
41                                            (def db-leaf (str (.. token toString trim) "." db-id-ID )   )
42                                            (def db-full-PARENT (str db-base-URL db-system-DIR db-working-DIR "/" db-leaf  ))
43                                            
44                                            (def db-document-NAME db-leaf) 
45                                            
46                                            
47                                            (println "DEBUG > db-base-URL["db-base-URL"] > db-system-DIR["db-system-DIR"] > db-working-DIR["db-working-DIR"] > leaf["db-leaf"]") 
48                                            (println "DEBUG > db-base-URL[" db-full-PARENT "]")
49                                            
50                                            
51                                            ;; this will find all <SPEECH> elements in the collection /db/shakespeare  with "Juliet" as the <SPEAKER> 
52                                            ;;       http://localhost:8080/exist/rest/db/shakespeare?_query=//SPEECH[SPEAKER=%22JULIET%22] 
53                                            
54                                            ;; build XPATH expression to find 'token' based on option 
55                                            ;; http://localhost:8080/exist/rest/db/two.xml?_query= 
56                                            ;;    declare default element namespace 'com/interrupt/bookkeeping/users' 
57                                            ;;    declare namespace aauth='com/interrupt/bookkeeping/cc/bkell/aauth'; 
58                                            ;;    //system/aauth:aauthentication 
59                                            
60                                            ;; TODO - a check if we even need a query 
61                                            (def db-query (str "_wrap=no&_query=" 
62                                                     "declare default element namespace '"(namespace-lookup (.. token toString trim)) "';" 
63                                                     ;;"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'; " 
64                                                     
65                                                     ;; TODO - check if we need 'and' conditions 
66                                                     ;; "**/<token>[ @option='option_value' [ and @option='option_value' ] ]" 
67                                                     "//"(.. token toString trim)"[ @" 
68                                                        (. (nth (re-seq #"-[a-z]+" (.. (nth option-id 0) getIdOpt getText)) 0) substring 1)    ;; TODO - put this part into a function (being re-used) 
69                                                     "='"db-id-ID"']"
70                                                     )
71                                                     
72                                            )
73                                            
74                                            (handler (xml-seq (execute-http-call db-full-PARENT db-leaf db-query)))
75                                            
76                                         )
77                      )
78                   )