-- upgrading Clojure to 1.2 RC3; code changes to reflect
[bkell-clj.git] / src / helpers.clj
blob1727074a93ed9cb88c4d83111aac97f29b6b4494
2 (ns helpers 
3         (:use clj-stacktrace.repl)
4         (:require clojure.contrib.string)
5         (:require clojure.contrib.http.agent)) 
7 (defn url-encode 
8         " Replacing these characters http encoded ones 
9         <space>         %20
10         '                                       %27
11         ;                                       %3B
12         [                                       %5B
13         @                                       %40
14         =                                       %3D
15         ]                                       %5D "
16   [text]
17   
18   (clojure.contrib.string/replace-str " " "%20"   
19         (clojure.contrib.string/replace-str "'" "%27"   
20                 (clojure.contrib.string/replace-str ";" "%3B"   
21                         (clojure.contrib.string/replace-str "[" "%5B"   
22                                 (clojure.contrib.string/replace-str "@" "%40"   
23                                         (clojure.contrib.string/replace-str "=" "%3D"   
24                                                 (clojure.contrib.string/replace-str "]" "%5D" text 
25                                         )))))   
26   )
29 (defn filterSpacesFromXML [text]
30   
31   (clojure.contrib.string/replace-str "< " "<"   
32         (clojure.contrib.string/replace-str " : " ":"   
33                 (clojure.contrib.string/replace-str " / >" " />"   
34                         (clojure.contrib.string/replace-str "< /" "</"   
35                                 (clojure.contrib.string/replace-str "</ " "</"   
36                                         (clojure.contrib.string/replace-str " / " "/"   
37                                                 (clojure.contrib.string/replace-re #"=\"\s" "=\""
38                                                         (clojure.contrib.string/replace-re #"\s\"" "\"" 
39                                                                 (clojure.contrib.string/replace-str " = " "=" 
40                                         text 
41                                         )))))))))
42   )
43                 
44                 
45 ;; set get base URL ...TODO - put in config 
46 (def db-base-URL "http://localhost:8080/exist/rest/") 
49 ;; set root/system dir fragment ...TODO - put in config 
50 (def db-system-DIR "rootDir/system.main.system/") 
52 ;; working directory lookup ...TODO - put these lookups into config 
53 (defn working-dir-lookup [token]
54    
55    ;;(println "DEBUG > 'working-dir-lookup' CALLED > ["(keyword token)"]" )
56    (  {  :group "aauthentication.main.authentication/groups.aauth.groups"
57                                  :user "aauthentication.main.authentication/users.aauth.users"
58                                  :users "aauthentication.main.authentication/users.aauth.users"
59                                  :account "groups.main.groups"
60                                  :journal "groups.main.groups"
61                                  :entry "groups.main.groups" 
62                                  :entries "groups.main.groups" 
63                                  :debit "groups.main.groups"
64                                  :credit "groups.main.groups" 
65                                  :bookkeeping "groups.main.groups" 
66                                  
67       }
68       (keyword token)
69    )
71 (defn namespace-lookup 
72    [token]
73    
74    ;;(println "DEBUG > 'namespace-lookup' CALLED > ["token"]" )
75    (  {  "group" "com/interrupt/bookkeeping/users"
76          "user" "com/interrupt/bookkeeping/users"
77          "account"  "com/interrupt/bookkeeping/account"
78          "journal"  "com/interrupt/bookkeeping/journal"
79          "entry"  "com/interrupt/bookkeeping/journal"
80          "entries"  "com/interrupt/bookkeeping/journal"
81          "debit"  "com/interrupt/bookkeeping/account"
82          "credit"  "com/interrupt/bookkeeping/account" 
83       }
84       token
85    )
88 (defn execute-http-call [ full-URL http-method header-hash xml-content ] 
89                 
90                 ;; from DB, get 'token' for 'option' args & value 
91                 (println "DEBUG > FINAL http query[" full-URL "] > http-method[" http-method "] > header-hash[" header-hash "] > xml-content[" xml-content "]")
92                 
93                 (let [agt (clojure.contrib.http.agent/http-agent   
94                                                                 
95                                                                 ;; TODO - parse results, check for i) null or ii) multiple results 
96                                                         full-URL 
97                                                         :method http-method 
98                                                                 :header header-hash 
99                                                                 :body xml-content 
100                                                                 )]
101                                 (if (clojure.contrib.http.agent/error? agt)
102                                         (str "<error method='GET' query='" full-URL "' errors='" (agent-errors agt) "' />")
103                                         (clojure.contrib.http.agent/string agt))
104                 )