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