-- introducing stack behaviour for xpath parsing
[bkell-clj.git] / src / opts_handler.clj
blob1f402cf62bea26fde4ed2104106f106aa54a2727
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                            (println "DEBUG > db-query[" db-query "]")
74                            
75                            (println "DEBUG > FINAL http query[" (str db-full-PARENT "/" db-leaf "?" (url-encode db-query) ) "]")
76                            
77                            ;; from DB, get 'token' for 'option' args & value 
78                            (def result-XML (clojure.contrib.http.agent/string (clojure.contrib.http.agent/http-agent (str db-full-PARENT "/" db-leaf "?" (url-encode db-query) ) 
79                                  :method "GET" 
80                                  :header {"Content-Type" "text/xml"} 
81                                  
82                                  ;; TODO - parse results, check for i) null or ii) multiple results 
83                               ) 
84                            ))
85                            
86                            ;; pass built XML sequence to handler
87                            (handler (xml-seq result-XML))
88                         )
89                      )
90                   )