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