-- putting auto-commit to 'update' command as well
[bkell-clj.git] / src / helpers.clj
blob1454f3f9c5665d6853628120fb945caeb1dd06f2
2 (ns helpers 
3         (:use clj-stacktrace.repl)
4         (:require clojure.contrib.string)
5         (:require clojure.contrib.http.agent)
6         (:require clojure-http.resourcefully)
7         (:require clojure.pprint)
8   
9     (:import java.io.ByteArrayInputStream)
10
12 (defn url-encode 
13         " Replacing these characters http encoded ones 
14         <space>         %20
15         '                                       %27
16         ;                                       %3B
17         [                                       %5B
18         @                                       %40
19         =                                       %3D
20         ]                                       %5D "
21   [text]
22   
23   (clojure.contrib.string/replace-str " " "%20"   
24         (clojure.contrib.string/replace-str "'" "%27"   
25                 (clojure.contrib.string/replace-str ";" "%3B"   
26                         (clojure.contrib.string/replace-str "[" "%5B"   
27                                 (clojure.contrib.string/replace-str "@" "%40"   
28                                         (clojure.contrib.string/replace-str "=" "%3D"   
29                                                 (clojure.contrib.string/replace-str "]" "%5D" text 
30                                         )))))   
31   )
33 (defn url-encode-spaces 
34         " Replacing just spaces  
35         <space>         %20 "
36   [text]
37   
38   (clojure.contrib.string/replace-str " " "%20" text )  
40 (defn url-encode-newlines 
41         " Replacing just newlines"
42   [text]
43   
44   (clojure.contrib.string/replace-str (str \newline) "" text )  
46 (defn strip-xml-header [text]
47   (clojure.contrib.string/replace-str "<?xml version='1.0' encoding='UTF-8'?>" "" text )        
51 (defn filterSpacesFromXML [text]
52   
53   (clojure.contrib.string/replace-str "< " "<"   
54         (clojure.contrib.string/replace-str " : " ":"   
55                 (clojure.contrib.string/replace-str " / >" " />"   
56                         (clojure.contrib.string/replace-str "< /" "</"   
57                                 (clojure.contrib.string/replace-str "</ " "</"   
58                                         (clojure.contrib.string/replace-str " / " "/"   
59                                                 (clojure.contrib.string/replace-re #"=\"\s" "=\""
60                                                         (clojure.contrib.string/replace-re #"\s\"" "\"" 
61                                                                 (clojure.contrib.string/replace-str " = " "=" 
62                                         text 
63                                         )))))))))
64   )
65                 
67 ;; set get base URL ...TODO - put in config 
68 ;; TODO - replace URL with a config value 
69 (def db-base-URL "http://localhost:8080/exist/rest/") 
72 ;; set root/system dir fragment ...TODO - put in config 
73 (def db-system-DIR "rootDir/system.main.system/") 
75 ;; working directory lookup ...TODO - put these lookups into config 
76 (defn working-dir-lookup [token]
77    
78    ;;(println "DEBUG > 'working-dir-lookup' CALLED > ["(keyword token)"]" )
79    (  {  :group "aauthentication.main.authentication/groups.aauth.groups"
80                                  :user "aauthentication.main.authentication/users.aauth.users"
81                                  :users "aauthentication.main.authentication/users.aauth.users"
82                                  :account "groups.main.groups"
83                                  :journal "groups.main.groups"
84                                  :entry "groups.main.groups" 
85                                  :entries "groups.main.groups" 
86                                  :debit "groups.main.groups"
87                                  :credit "groups.main.groups" 
88                                  :bookkeeping "groups.main.groups" 
89                                  
90       }
91       (keyword token)
92    )
94 (defn namespace-lookup 
95    [token]
96    
97    ;;(println "DEBUG > 'namespace-lookup' CALLED > ["token"]" )
98    (  {  "group" "com/interrupt/bookkeeping/users" 
99          "user" "com/interrupt/bookkeeping/users" 
100          "users" "com/interrupt/bookkeeping/users" 
101          "account"  "com/interrupt/bookkeeping/account" 
102          "journal"  "com/interrupt/bookkeeping/journal" 
103          "entry"  "com/interrupt/bookkeeping/journal" 
104          "entries"  "com/interrupt/bookkeeping/journal" 
105          "debit"  "com/interrupt/bookkeeping/account" 
106          "credit"  "com/interrupt/bookkeeping/account" 
107       }
108       token
109    )
112 (defn execute-http-call [ full-URL http-method header-hash xml-content ] 
113                 
114                 ;; from DB, get 'token' for 'option' args & value 
115                 (println "DEBUG > FINAL http query[" full-URL "] > http-method[" http-method "] > header-hash[" header-hash "] > xml-content[" xml-content "]")
116                 
117                 (cond 
118                         (. "GET" equals http-method) 
119                                 (try 
120                                         (clojure-http.resourcefully/get full-URL header-hash xml-content)
121                                         (catch Exception e { :msg "Error" :dmsg (. e getMessage ) } )
122                                 )
123                         
124                         (. "PUT" equals http-method)
125                                 (try 
126                                         (clojure-http.resourcefully/put full-URL header-hash xml-content)
127                                         (catch Exception e { :msg "Error" :dmsg (. e getMessage ) } )
128                                 )
129                         (. "POST" equals http-method)
130                                 (try 
131                                         (clojure-http.resourcefully/post full-URL header-hash xml-content)
132                                         (catch Exception e { :msg "Error" :dmsg (. e getMessage ) } )
133                                 )
134                         (. "DELETE" equals http-method)
135                                 (try 
136                                         (clojure-http.resourcefully/delete full-URL header-hash nil)
137                                         (catch Exception e { :msg "Error" :dmsg (. e getMessage ) } )
138                                 )
139                         
140                 )
144 (defn parse-xml-to-hash [x-input]
146   (clojure.xml/parse (ByteArrayInputStream. (.getBytes 
147         (clojure.contrib.str-utils/str-join nil x-input)                ;; get the XML string  
148         "UTF-8"))) 
149 )       
151 (defn get-user [db-base-URL db-system-DIR working-USER] 
153     (let 
154       [result-hash 
155             (execute-http-call  ;; TODO - put in 404 check 
156                   (str 
157                         db-base-URL 
158                         db-system-DIR 
159                         (working-dir-lookup (:tag working-USER))        ;; stringing together lookup URL leaf 
160                         "/" 
161                         (str 
162                                 (name (:tag working-USER)) 
163                                 "." 
164                                 (:id (:attrs working-USER)))
165                         "/"
166                         (str                    ;; repeating user name as leaf document 
167                                 (name (:tag working-USER)) 
168                                 "." 
169                                 (:id (:attrs working-USER))))
170                         "GET" 
171                         {"Content-Type" "text/xml"} 
172                         nil
173       ) ]
174           
175       (println "RESULT USER > result-hash... " result-hash)
176       (parse-xml-to-hash (:body-seq result-hash))
177     )