removing some unneeded comments
[bkell-clj.git] / src / depth_adapter.clj
blob6df539ad3af31b54f515046a1f9b74be12d1756b
2 (ns depth_adapter
3    (:import com.interrupt.bookkeeping.cc.analysis.DepthFirstAdapter) 
6 (use 'clj-stacktrace.repl)
7 (comment try
8  ("foo")
9  (catch Exception e
10    (clj-stacktrace.repl/pst e)))
12 (require 'clojure.contrib.str-utils2) 
13 (require 'clojure.contrib.http.agent) 
14 (require 'clojure.contrib.io) 
15 (require 'clojure.contrib.string) 
18 (import (java.net URLEncoder))
21 ;; function stolen from http://p.hagelb.org/http-client-send-body 
22 (defn url-encode 
23         " Replacing these characters http encoded ones 
24         <space>         %20
25         '                                       %27
26         ;                                       %3B
27         [                                       %5B
28         @                                       %40
29         =                                       %3D
30         ]                                       %5D "
31   [text]
32   
33   (clojure.contrib.string/replace-str " " "%20"   
34         (clojure.contrib.string/replace-str "'" "%27"   
35                 (clojure.contrib.string/replace-str ";" "%3B"   
36                         (clojure.contrib.string/replace-str "[" "%5B"   
37                                 (clojure.contrib.string/replace-str "@" "%40"   
38                                         (clojure.contrib.string/replace-str "=" "%3D"   
39                                                 (clojure.contrib.string/replace-str "]" "%5D" text 
40                                         )))))   
41   )
45 ;; set get base URL ...TODO - put in config 
46 (def db-base-URL "http://localhost:8080/exist/rest/") 
48 ;; set root/system dir fragment ...TODO - put in config 
49 (def db-system-DIR "rootDir/system.main.system/") 
51 ;; working directory lookup ...TODO - put these lookups into config 
52 (defn working-dir-lookup 
53         [token]
54         
55         (println "DEBUG > 'working-dir-lookup' CALLED > ["(keyword token)"]" )
56         
57         (       {       :group "aauthentication.main.authentication/groups.aauth.groups/"
58                         :user "aauthentication.main.authentication/users.aauth.users/"
59                         :account "groups.main.groups/"
60                         :journal "groups.main.groups/"
61                         :entry "groups.main.groups/"
62                         :debit "groups.main.groups/"
63                         :credit "groups.main.groups/" 
64                 }
65                 (keyword token)
66         )
68 (defn namespace-lookup 
69         [token]
70         
71         (println "DEBUG > 'namespace-lookup' CALLED > ["token"]" )
72         
73         (       {       "group" "com/interrupt/bookkeeping/users"
74                         "user" "com/interrupt/bookkeeping/users"
75                         "account"  "com/interrupt/bookkeeping/account"
76                         "journal"  "com/interrupt/bookkeeping/journal"
77                         "entry"  "com/interrupt/bookkeeping/journal"
78                         "debit"  "com/interrupt/bookkeeping/account"
79                         "credit"  "com/interrupt/bookkeeping/account" 
80                 }
81                 token
82         )
85 (defn operate-dep-inputtype 
86         [node handler_block]    ;; input args ; for now we are going to load by ID 
87         
88         (let [ checks 
89                 
90                                         [       (fn [node handler] 
91                                                         (if (instance? com.interrupt.bookkeeping.cc.node.AXmlCommandInput (. node getCommandInput) )
92                                                                 (do 
93                                                                         (println "XML input[" (.. node getCommandInput toString) "]")
94                                                                         
95                                                                         ;; extract the context 
96                                                                         (let [ result_seq []]
97                                                                                 
98                                                                                 ;; operate with handler
99                                                                                 (handler result_seq)
100                                                                         )
101                                                                 )
102                                                         ) 
103                                                 )
104                                                 
105                                                 (fn [node handler] 
106                                                         (if (instance? com.interrupt.bookkeeping.cc.node.AOptsCommandInput (. node getCommandInput) )
107                                                                 (do 
108                                                                         (println "DEBUG > OPTIONS input > token[" (.. node getCommandInput getInputOption getCommandtoken) "] > options[" (.. node getCommandInput getInputOption getCommandoption) "]")
109                                                                         
110                                                                         ;; get token string (ie user, entry, etc) -> 
111                                                                         (def token (.. node getCommandInput getInputOption getCommandtoken))
112                                                                         
113                                                                         ;; get option args & value -> use a 'CommandOptionVisitor' 
114                                                                         (def options (seq (.. node getCommandInput getInputOption getCommandoption)))
115                                                                         
116                                                                         (def option-id  
117                                                                                 (take 1 (filter 
118                                                                                         (fn [input] 
119                                                                                                 (if (instance? com.interrupt.bookkeeping.cc.node.AIdCommandoption input ) 
120                                                                                                         (true? true)
121                                                                                                         (false? false)
122                                                                                                 )
123                                                                                         )
124                                                                                         options
125                                                                                 ))
126                                                                         )
127                                                                         (def db-id-ID   ;; TODO - chain this to look for other options if 'id' is not there
128                                                                                 
129                                                                                 (clojure.contrib.str-utils2/trim 
130                                                                                         (nth  
131                                                                                                 (clojure.contrib.str-utils2/split (.. (nth option-id 0) ;; class 'com.interrupt.bookkeeping.cc.node.AIdCommandoption' 
132                                                                                                         getIdOpt getText) #"-[a-z]+")
133                                                                                                 1
134                                                                                         )
135                                                                                 )
136                                                                                 
137                                                                         )
138                                                                         
139                                                                         (println "DEBUG > extracted > [" token "] > [" options "] > [" db-id-ID "]")
140                                                                         
141                                                                         ;; from HASH -> find containing folder for token 
142                                                                         (def db-working-DIR (working-dir-lookup (.. token toString trim)))
143                                                                         
144                                                                         ;; build another <my.group> to end of db-working-DIR 
145                                                                         (def db-leaf (str (.. token toString trim) "." db-id-ID )       )
146                                                                         (def db-full-PARENT (str db-base-URL db-system-DIR db-working-DIR db-leaf       ))
147                                                                         
148                                                                         (def db-document-NAME db-leaf) 
149                                                                         
150                                                                         
151                                                                         (println "DEBUG > db-base-URL["db-base-URL"] > db-system-DIR["db-system-DIR"] > db-working-DIR["db-working-DIR"] > leaf["db-leaf"]") 
152                                                                         (println "DEBUG > db-base-URL[" db-full-PARENT "]")
153                                                                         
154                                                                         
155                                                                         ;; this will find all <SPEECH> elements in the collection /db/shakespeare  with "Juliet" as the <SPEAKER> 
156                                                                         ;;              http://localhost:8080/exist/rest/db/shakespeare?_query=//SPEECH[SPEAKER=%22JULIET%22] 
157                                                                         
158                                                                         ;; build XPATH expression to find 'token' based on option 
159                                                                         ;; http://localhost:8080/exist/rest/db/two.xml?_query= 
160                                                                         ;;              declare default element namespace 'com/interrupt/bookkeeping/users' 
161                                                                         ;;              declare namespace aauth='com/interrupt/bookkeeping/cc/bkell/aauth'; 
162                                                                         ;;              //system/aauth:aauthentication 
163                                                                         
164                                                                         ;; TODO - a check if we even need a query 
165                                                                         (def db-query (str "_wrap=no&_query=" 
166                                                                                                 "declare default element namespace '"(namespace-lookup (.. token toString trim)) "';" 
167                                                                                                 ;;"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'; " 
168                                                                                                 
169                                                                                                 ;; TODO - check if we need 'and' conditions 
170                                                                                                 ;; "**/<token>[ @option='option_value' [ and @option='option_value' ] ]" 
171                                                                                                 "//"(.. token toString trim)"[ @" 
172                                                                                                         (. (nth (re-seq #"-[a-z]+" (.. (nth option-id 0) getIdOpt getText)) 0) substring 1)             ;; TODO - put this part into a function (being re-used) 
173                                                                                                 "='"db-id-ID"']"
174                                                                                                 )
175                                                                                                 
176                                                                         )
177                                                                         (println "DEBUG > db-query[" db-query "]")
178                                                                         
179                                                                         (println "DEBUG > FINAL http query[" (str db-full-PARENT "/" db-leaf "?" (url-encode db-query) ) "]")
180                                                                         
181                                                                         ;; from DB, get 'token' for 'option' args & value 
182                                                                         (def result-XML (clojure.contrib.http.agent/string (clojure.contrib.http.agent/http-agent (str db-full-PARENT "/" db-leaf "?" (url-encode db-query) ) 
183                                                                                         :method "GET" 
184                                                                                         :header {"Content-Type" "text/xml"} 
185                                                                                         
186                                                                                         ;; TODO - parse results, check for i) null or ii) multiple results 
187                                                                                 ) 
188                                                                         ))
189                                                                         
190                                                                         ;; pass built XML sequence to handler
191                                                                         (handler (xml-seq result-XML))
192                                                                 )
193                                                         )
194                                                 )
195                                                 
196                                                 (fn [node handler] 
197                                                         (if (instance? com.interrupt.bookkeeping.cc.node.AXpathCommandInput (. node getCommandInput) )
198                                                                 
199                                                                 (do 
200                                                                         (println "XPATH input[" (.. node getCommandInput toString) "]")
201                                                                         
202                                                                         ;; extract the context 
203                                                                         (let [ result_seq []]
204                                                                                 
205                                                                                 ;; operate with handler
206                                                                                 (handler result_seq)
207                                                                         )
208                                                                 )
209                                                         )
210                                                 )
211                                         ]
212                         ]
213                         
214                         (doseq [ each_check checks ] 
215                                 (do
216                                         (println "DEBUG > each... " each_check) 
217                                         (each_check node handler_block)
218                                 )
219                         )
220                         
221         )
222         
225 (defn get-depth-adapter [] 
226          
227    (proxy [DepthFirstAdapter] [] 
228          
229          ;; EXIT commnad 
230          (caseAExitCommand4 [node] 
231             
232             (println (str "DEBUG > caseAExitCommand4: " node))
233             
234             (proxy-super inAExitCommand4 node) 
235             (proxy-super outAExitCommand4 node) 
236                         
237             (. System exit 0) 
238          )
240          
241          ;; LOGIN command 
242          (caseALoginCommand3 [node] 
243             (println "DEBUG > caseALoginCommand3: " node)
244             
245             
246             (proxy-super inALoginCommand3 node) 
247             
248             (if (not= (. node getLogin ) nil) 
249                (.. node getLogin (apply this) ) )
250             
251             (if (not= (. node getLbracket ) nil) 
252                (.. node getLbracket (apply this) ) )
253             
254       (if (not= (. node getCommandInput ) nil) 
255               
256               (do 
257                 
258                 (.. node getCommandInput (apply this) ) 
259                         
260                         ;; execute LOGIN 
261                                         (operate-dep-inputtype node (fn [result_seq] (println "DEBUG > logging in on... " result_seq)))
262                 )
263             )
264             
265       (if (not= (. node getRbracket ) nil) 
266                (.. node getRbracket (apply this) ) )
267             
268       (proxy-super outALoginCommand3 node) 
269             
270          )
271          
272          ;; PRINT command 
273          (caseAPrintCommand6 [node] 
274             (println (str "caseAPrintCommand6: " node)) )
275          
276          
277          ;; LOAD command 
278          (caseALoadCommand3 [node] 
279             (println "DEBUG > caseALoadCommand3 [" (class (. node getCommandInput)) "]: " node) 
280             
281             (comment "replicating java calls in the 'DepthFirstAdapter.caseALoadCommand3'")
282             
283             
284             (proxy-super inALoadCommand3 node) 
285             
286             (if (not= (. node getLoad ) nil) 
287                (.. node getLoad (apply this) ) )
288             
289             (if (not= (. node getLbracket ) nil) 
290                (.. node getLbracket (apply this) ) )
291              
292             (if (not= (. node getCommandInput ) nil) 
293                
294                     (do ;; execute 'if' block 
295                                   (.. node getCommandInput (apply this) ) 
296                                   
297                                   
298                                         (if (not (contains? com.interrupt.bookkeeping/shell :logged-in-user ))  ;; check if there is a 'logged-in-user' 
299                                 
300                                 ;;throw an error if no 'logged-in-user' 
301                                 (println "ERROR - NO logged-in-user") 
302                                 
303                                 ;; execute LOAD 
304                                 (operate-dep-inputtype node (fn [result_seq] (println "loading... " result_seq)))
305                                 
306                                   )
307                                   
308                     )
309             )
310             
311             (if (not= (. node getRbracket ) nil) 
312                (.. node getRbracket (apply this) ) )
313             
314             
315             (proxy-super outALoadCommand3 node) 
316             
317          )
318          
319          ;; ADD command (for registering users too) 
320                   ;; 1. check that there's not an existing user 
321                   ;; 2. add corresponding default group to the new user 
322                   ;; 3. add to aauth.groups 
323                   ;; 4. add to aauth.users 
324                   ;; 5. add Associated Bookkeeping to Group 
325          
326          
327         )