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