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