-- fixed a small bug where xpath parsing algorithm data gets mixed in between calls
[bkell-clj.git] / src / helpers.clj
blobfc10b0b0cfe2c3fff6f0aebbb15dea8b4ba374c3
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)) 
9 (defn url-encode 
10         " Replacing these characters http encoded ones 
11         <space>         %20
12         '                                       %27
13         ;                                       %3B
14         [                                       %5B
15         @                                       %40
16         =                                       %3D
17         ]                                       %5D "
18   [text]
19   
20   (clojure.contrib.string/replace-str " " "%20"   
21         (clojure.contrib.string/replace-str "'" "%27"   
22                 (clojure.contrib.string/replace-str ";" "%3B"   
23                         (clojure.contrib.string/replace-str "[" "%5B"   
24                                 (clojure.contrib.string/replace-str "@" "%40"   
25                                         (clojure.contrib.string/replace-str "=" "%3D"   
26                                                 (clojure.contrib.string/replace-str "]" "%5D" text 
27                                         )))))   
28   )
30 (defn url-encode-spaces 
31         " Replacing just spaces  
32         <space>         %20 "
33   [text]
34   
35   (clojure.contrib.string/replace-str " " "%20" text )  
38 (defn filterSpacesFromXML [text]
39   
40   (clojure.contrib.string/replace-str "< " "<"   
41         (clojure.contrib.string/replace-str " : " ":"   
42                 (clojure.contrib.string/replace-str " / >" " />"   
43                         (clojure.contrib.string/replace-str "< /" "</"   
44                                 (clojure.contrib.string/replace-str "</ " "</"   
45                                         (clojure.contrib.string/replace-str " / " "/"   
46                                                 (clojure.contrib.string/replace-re #"=\"\s" "=\""
47                                                         (clojure.contrib.string/replace-re #"\s\"" "\"" 
48                                                                 (clojure.contrib.string/replace-str " = " "=" 
49                                         text 
50                                         )))))))))
51   )
52                 
54 ;; set get base URL ...TODO - put in config 
55 ;; TODO - replace URL with a config value 
56 (def db-base-URL "http://localhost:8080/exist/rest/") 
59 ;; set root/system dir fragment ...TODO - put in config 
60 (def db-system-DIR "rootDir/system.main.system/") 
62 ;; working directory lookup ...TODO - put these lookups into config 
63 (defn working-dir-lookup [token]
64    
65    ;;(println "DEBUG > 'working-dir-lookup' CALLED > ["(keyword token)"]" )
66    (  {  :group "aauthentication.main.authentication/groups.aauth.groups"
67                                  :user "aauthentication.main.authentication/users.aauth.users"
68                                  :users "aauthentication.main.authentication/users.aauth.users"
69                                  :account "groups.main.groups"
70                                  :journal "groups.main.groups"
71                                  :entry "groups.main.groups" 
72                                  :entries "groups.main.groups" 
73                                  :debit "groups.main.groups"
74                                  :credit "groups.main.groups" 
75                                  :bookkeeping "groups.main.groups" 
76                                  
77       }
78       (keyword token)
79    )
81 (defn namespace-lookup 
82    [token]
83    
84    ;;(println "DEBUG > 'namespace-lookup' CALLED > ["token"]" )
85    (  {  "group" "com/interrupt/bookkeeping/users" 
86          "user" "com/interrupt/bookkeeping/users" 
87          "users" "com/interrupt/bookkeeping/users" 
88          "account"  "com/interrupt/bookkeeping/account" 
89          "journal"  "com/interrupt/bookkeeping/journal" 
90          "entry"  "com/interrupt/bookkeeping/journal" 
91          "entries"  "com/interrupt/bookkeeping/journal" 
92          "debit"  "com/interrupt/bookkeeping/account" 
93          "credit"  "com/interrupt/bookkeeping/account" 
94       }
95       token
96    )
99 (defn execute-http-call [ full-URL http-method header-hash xml-content ] 
100                 
101                 ;; from DB, get 'token' for 'option' args & value 
102                 (println "DEBUG > FINAL http query[" full-URL "] > http-method[" http-method "] > header-hash[" header-hash "] > xml-content[" xml-content "]")
103                 
104                 (cond 
105                         (. "GET" equals http-method) 
106                                 (try 
107                                         (clojure-http.resourcefully/get full-URL header-hash xml-content)
108                                         (catch Exception e { :msg "Error" :code 500 :dmsg (. e getMessage ) } )
109                                 )
110                         
111                         (. "PUT" equals http-method)
112                                 (try 
113                                         (clojure-http.resourcefully/put full-URL header-hash xml-content)
114                                         (catch Exception e { :msg "Error" :code 500 :dmsg (. e getMessage ) } )
115                                 )
116                 )