-- abstrcting out code for an HTTP call
[bkell-clj.git] / src / depth_adapter.clj
blob97a061252f01f3a8da2b8a6c2f1ace0e8a492eb3
2 (ns depth_adapter
3   (:import com.interrupt.bookkeeping.cc.analysis.DepthFirstAdapter) 
4    
5         (:require clojure.contrib.str-utils2) 
6         (:require clojure.contrib.http.agent) 
7         (:require clojure.contrib.io) 
8         (:require clojure.contrib.string) 
9   
10   (:use helpers) 
11   
12   (:require xml_handler)
13   (:require opts_handler)
14   (:require xpath_handler)
15   
19 (use 'clj-stacktrace.repl)
20 (comment try
21  ("foo")
22  (catch Exception e
23    (clj-stacktrace.repl/pst e)))
26 (defn operate-dep-inputtype 
27         [node handler_block]    ;; input args ; for now we are going to load by ID 
28         
29         (let [ checks [ xml_handler option_handler xpath_handler/xpath_handler ] ]
30                         
31                         (doseq [ each_check checks ] 
32                                 (do
33                                         (println "DEBUG > each... " each_check) 
34                                         (each_check node handler_block)
35                                 )
36                         )
37                         
38         )
39         
42 (defn get-depth-adapter [] 
43          
44    (proxy [DepthFirstAdapter] [] 
45          
46          ;; EXIT command 
47          (caseAExitCommand4 [node] 
48             
49             (println (str "DEBUG > caseAExitCommand4: " node))
50             
51             (proxy-super inAExitCommand4 node) 
52             (proxy-super outAExitCommand4 node) 
53                         
54             (. System exit 0) 
55          )
57          
58          ;; LOGIN command 
59          (caseALoginCommand3 [node] 
60             (println "DEBUG > caseALoginCommand3: " node)
61             
62             
63             (proxy-super inALoginCommand3 node) 
64             
65             (if (not= (. node getLogin ) nil) 
66                (.. node getLogin (apply this) ) )
67             
68             (if (not= (. node getLbracket ) nil) 
69                (.. node getLbracket (apply this) ) )
70             
71       (if (not= (. node getCommandInput ) nil) 
72               
73               (do 
74                 
75                 (.. node getCommandInput (apply this) ) 
76                         
77                         ;; execute LOGIN 
78                                         (operate-dep-inputtype node 
79                                                                                                                                 (fn [result_seq] 
80                                                                                                                                         
81                                                                                                                                         (dosync 
82                                                                                                                                                 (alter com.interrupt.bookkeeping/shell conj 
83                                                                                                                                                                                 { :logged-in-user result_seq } )
84                                                                                                                                                 (alter com.interrupt.bookkeeping/shell conj 
85                                                                                                                                                                                 {       :previous result_seq })) 
86                                                                                                                                         (println "DEBUG > logged-in-user > " ((deref com.interrupt.bookkeeping/shell) :logged-in-user))
87                                                                                                                                 ))
88                 )
89             )
90             
91       (if (not= (. node getRbracket ) nil) 
92                (.. node getRbracket (apply this) ) )
93             
94       (proxy-super outALoginCommand3 node) 
95             
96          )
97          
98          ;; PRINT command 
99          (caseAPrintCommand6 [node] 
100             (println (str "caseAPrintCommand6: " node)) )
101          
102          
103          ;; LOAD command 
104          (caseALoadCommand3 [node] 
105             (println "DEBUG > caseALoadCommand3 [" (class (. node getCommandInput)) "]: " node) 
106             
107             (comment "replicating java calls in the 'DepthFirstAdapter.caseALoadCommand3'")
108             
109             
110             (proxy-super inALoadCommand3 node) 
111             
112             (if (not= (. node getLoad ) nil) 
113                (.. node getLoad (apply this) ) )
114             
115             (if (not= (. node getLbracket ) nil) 
116                (.. node getLbracket (apply this) ) )
117              
118             (if (not= (. node getCommandInput ) nil) 
119                
120                     (do ;; execute 'if' block 
121                                   (.. node getCommandInput (apply this) ) 
122                                   
123                                   
124                                         (if (not (contains? (deref com.interrupt.bookkeeping/shell) :logged-in-user ))  ;; check if there is a 'logged-in-user' 
125                                 
126                                 ;;throw an error if no 'logged-in-user' 
127                                 (println "ERROR - NO logged-in-user") 
128                                 
129                                 ;; execute LOAD 
130                                 (operate-dep-inputtype node (fn [result_seq] 
131                                                                                                                                                         
132                                                                                                                                                         (println "loading... " result_seq)
133                                                                                                                                                         (dosync (alter com.interrupt.bookkeeping/shell conj 
134                                                                                                                                                                                         {       :previous result_seq }))
135                                                                                                                                                 ))
136                                 
137                                   )
138                                   
139                     )
140             )
141             
142             (if (not= (. node getRbracket ) nil) 
143                (.. node getRbracket (apply this) ) )
144             
145             
146             (proxy-super outALoadCommand3 node) 
147             
148          )
149         
150         (caseStart [node] 
151                         
152                         (println "DEBUG > caseStart CALLED > com.interrupt.bookkeeping/shell[" (deref com.interrupt.bookkeeping/shell) "]")
153                         
154                         (proxy-super inStart node) 
155       
156       (.. node getPExpr (apply this) )
157       (.. node getEOF (apply this) )
158       
159       (proxy-super outStart node) 
160       
161         ) 
162         
163         (caseAAddCommand1 [node]                ;; public void caseAAddCommand1(AAddCommand1 node)
164                                 
165                                 (println "DEBUG > caseAAddCommand1 [" (class (. node getCommandInput)) "]: " node) 
166                 
167                 
168         (proxy-super inAAddCommand1 node) 
169                 
170                     (if (not= (. node getAdd ) nil) 
171                        (.. node getAdd (apply this) ) ) 
172                     
173         (if (not= (. node getLbdepth1 ) nil) 
174                        (.. node getLbdepth1 (apply this) ) ) 
175                     
176                     (if (not= (. node getLbdepth2 ) nil) 
177                        (.. node getLbdepth2 (apply this) ) ) 
178                     
179         (if (not= (. node getCommandInput ) nil) 
180                        (.. node getCommandInput (apply this) )          ;; any i) 'load' ii) direct XML or iii) variable should be in the shell's :previous 
181                        
182                     ) 
183                     
184                     (if (not= (. node getRbdepth2 ) nil) 
185                        (.. node getRbdepth2 (apply this) ) ) 
186                     
187                                 (let [ copy (. node getIlist) ]
188                                                 
189                                                 (doseq [ each_copy copy ] 
190                                                         (do 
191                                                                 
192                                                                 ;; apply each element in the list 
193                                                                 (. each_copy apply this)
194                                                                 
195                                                                 ;; check if we are dealing with a 'User'
196                                                                 
197                                                                 
198                                                         )
199                                                 )
200                                                 
201                                 )
202         (if (not= (. node getRbdepth1 ) nil) 
203                        (.. node getRbdepth1 (apply this) ) ) 
204                     
205                     
206         
207         (proxy-super outAAddCommand1 node) 
208                 
209         )
211          ;; TODO - handle variables 
212          
213          ;; TODO - ADD command (for registering users too) 
214                   ;; 1. check that there's not an existing user 
215                   ;; 2. add corresponding default group to the new user 
216                   ;; 3. add to aauth.groups 
217                   ;; 4. add to aauth.users 
218                   ;; 5. add Associated Bookkeeping to Group 
219          
220          ;; TODO - finish 'print' command 
221          
222          
223         )