renamed methods and restructured code to follow coding guidelines set in filter-templ...
[metaproxy-moved-to-github.git] / src / filter_cql_to_rpn.cpp
1 /* $Id: filter_cql_to_rpn.cpp,v 1.3 2007-01-15 15:07:59 marc Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #include "config.hpp"
8
9 #include "filter.hpp"
10 #include "package.hpp"
11
12 #include <boost/thread/mutex.hpp>
13 #include <boost/thread/condition.hpp>
14 #include <boost/thread/thread.hpp>
15 #include <boost/thread/xtime.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/format.hpp>
18
19 #include "util.hpp"
20 #include "filter_cql_to_rpn.hpp"
21
22 #include <yaz/log.h>
23 #include <yaz/cql.h>
24 #include <yaz/zgdu.h>
25 #include <yaz/otherinfo.h>
26 #include <yaz/diagbib1.h>
27 #include <yaz/srw.h>
28 #include <yazpp/z-query.h>
29 #include <yazpp/cql2rpn.h>
30 #include <map>
31 #include <iostream>
32 #include <time.h>
33
34 namespace mp = metaproxy_1;
35 namespace yf = metaproxy_1::filter;
36
37 namespace metaproxy_1 {
38     namespace filter {
39         class CQLtoRPN::Impl {
40             //friend class CQLtoRPN;
41         public:
42             Impl();
43             ~Impl();
44             void process(metaproxy_1::Package & package);
45             void configure(const xmlNode * ptr);
46         private:
47             yazpp_1::Yaz_cql2rpn m_cql2rpn;
48         };
49     }
50 }
51
52
53 // define Pimpl wrapper forwarding to Impl
54  
55 yf::CQLtoRPN::CQLtoRPN() : m_p(new Impl)
56 {
57 }
58
59 yf::CQLtoRPN::~CQLtoRPN()
60 {  // must have a destructor because of boost::scoped_ptr
61 }
62
63 void yf::CQLtoRPN::configure(const xmlNode *xmlnode)
64 {
65     m_p->configure(xmlnode);
66 }
67
68 void yf::CQLtoRPN::process(mp::Package &package) const
69 {
70     m_p->process(package);
71 }
72
73
74 // define Implementation stuff
75
76 yf::CQLtoRPN::Impl::Impl()
77 {
78 }
79
80 yf::CQLtoRPN::Impl::~Impl()
81
82 }
83
84 void yf::CQLtoRPN::Impl::configure(const xmlNode *xmlnode)
85 {
86
87     /*
88       <filter type="cql_rpn">
89       <conversion file="pqf.properties"/>
90       </filter>
91     */
92     
93     std::string fname;
94     for (xmlnode = xmlnode->children; xmlnode; xmlnode = xmlnode->next)
95     {
96         if (xmlnode->type != XML_ELEMENT_NODE)
97             continue;
98         if (!strcmp((const char *) xmlnode->name, "conversion"))
99         {
100             const struct _xmlAttr *attr;
101             for (attr = xmlnode->properties; attr; attr = attr->next)
102             {
103                 if (!strcmp((const char *) attr->name, "file"))
104                     fname = mp::xml::get_text(attr);
105                 else
106                     throw mp::filter::FilterException(
107                         "Bad attribute " + std::string((const char *)
108                                                        attr->name));
109             }
110         }
111         else
112         {
113             throw mp::filter::FilterException("Bad element " 
114                                                + std::string((const char *)
115                                                              xmlnode->name));
116         }
117     }
118     if (fname.length() == 0)
119     {
120         throw mp::filter::FilterException("Missing conversion spec for "
121                                           "filter cql_rpn");
122     }
123
124     int error = 0;
125     if (!m_cql2rpn.parse_spec_file(fname.c_str(), &error))
126     {
127         throw mp::filter::FilterException("Bad or missing CQL to RPN spec "
128                                           + fname);
129     }
130 }
131
132 void yf::CQLtoRPN::Impl::process(mp::Package &package)
133 {
134     Z_GDU *gdu = package.request().get();
135
136     if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
137         Z_APDU_searchRequest)
138     {
139         Z_APDU *apdu_req = gdu->u.z3950;
140         Z_SearchRequest *sr = gdu->u.z3950->u.searchRequest;
141         if (sr->query && sr->query->which == Z_Query_type_104 &&
142             sr->query->u.type_104->which == Z_External_CQL)
143         {
144             char *addinfo = 0;
145             Z_RPNQuery *rpnquery = 0;
146             mp::odr odr;
147             
148             int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
149                                                  &rpnquery, odr,
150                                                  &addinfo);
151             if (r == -3)
152             {
153                 yaz_log(YLOG_LOG, "No CQL to RPN table");
154                 Z_APDU *f_apdu = 
155                     odr.create_searchResponse(
156                         apdu_req, 
157                         YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
158                         "Missing CQL to RPN spec");
159                 package.response() = f_apdu;
160                 return;
161             }
162             else if (r)
163             {
164                 int error_code = yaz_diag_srw_to_bib1(r);
165
166                 yaz_log(YLOG_LOG, "CQL Conversion error %d", r);
167                 Z_APDU *f_apdu = 
168                     odr.create_searchResponse(apdu_req, error_code, addinfo);
169                 package.response() = f_apdu;
170                 return;
171             }
172             else
173             {   // conversion OK
174                 
175                 sr->query->which = Z_Query_type_1;
176                 sr->query->u.type_1 = rpnquery;
177                 package.request() = gdu;
178             }
179         }
180     }
181     package.move();
182 }
183
184
185 // yf::CQLtoRPN::Rep::Rep()
186 // {
187
188 // }
189
190 // yf::CQLtoRPN::CQLtoRPN() : m_p(new CQLtoRPN::Rep)
191 // {
192 // }
193
194 // yf::CQLtoRPN::~CQLtoRPN()
195 // {
196
197 // }
198
199 // void yf::CQLtoRPN::process(mp::Package &package) const
200 // {
201 //     Z_GDU *gdu = package.request().get();
202
203 //     if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
204 //         Z_APDU_searchRequest)
205 //     {
206 //         Z_APDU *apdu_req = gdu->u.z3950;
207 //         Z_SearchRequest *sr = gdu->u.z3950->u.searchRequest;
208 //         if (sr->query && sr->query->which == Z_Query_type_104 &&
209 //             sr->query->u.type_104->which == Z_External_CQL)
210 //         {
211 //             char *addinfo = 0;
212 //             Z_RPNQuery *rpnquery = 0;
213 //             mp::odr odr;
214             
215 //             int r = m_p->cql2rpn.query_transform(sr->query->u.type_104->u.cql,
216 //                                                  &rpnquery, odr,
217 //                                                  &addinfo);
218 //             if (r == -3)
219 //             {
220 //                 yaz_log(YLOG_LOG, "No CQL to RPN table");
221 //                 Z_APDU *f_apdu = 
222 //                     odr.create_searchResponse(
223 //                         apdu_req, 
224 //                         YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
225 //                         "Missing CQL to RPN spec");
226 //                 package.response() = f_apdu;
227 //                 return;
228 //             }
229 //             else if (r)
230 //             {
231 //                 int error_code = yaz_diag_srw_to_bib1(r);
232
233 //                 yaz_log(YLOG_LOG, "CQL Conversion error %d", r);
234 //                 Z_APDU *f_apdu = 
235 //                     odr.create_searchResponse(apdu_req, error_code, addinfo);
236 //                 package.response() = f_apdu;
237 //                 return;
238 //             }
239 //             else
240 //             {   // conversion OK
241                 
242 //                 sr->query->which = Z_Query_type_1;
243 //                 sr->query->u.type_1 = rpnquery;
244 //                 package.request() = gdu;
245 //             }
246 //         }
247 //     }
248 //     package.move();
249 // }
250
251 // void yf::CQLtoRPN::configure(const xmlNode *ptr)
252 // {
253
254 //     /*
255 //       <filter type="cql_rpn">
256 //       <conversion file="pqf.properties"/>
257 //       </filter>
258 //     */
259     
260 //     std::string fname;
261 //     for (ptr = ptr->children; ptr; ptr = ptr->next)
262 //     {
263 //         if (ptr->type != XML_ELEMENT_NODE)
264 //             continue;
265 //         if (!strcmp((const char *) ptr->name, "conversion"))
266 //         {
267 //             const struct _xmlAttr *attr;
268 //             for (attr = ptr->properties; attr; attr = attr->next)
269 //             {
270 //                 if (!strcmp((const char *) attr->name, "file"))
271 //                     fname = mp::xml::get_text(attr);
272 //                 else
273 //                     throw mp::filter::FilterException(
274 //                         "Bad attribute " + std::string((const char *)
275 //                                                        attr->name));
276 //             }
277 //         }
278 //         else
279 //         {
280 //             throw mp::filter::FilterException("Bad element " 
281 //                                                + std::string((const char *)
282 //                                                              ptr->name));
283 //         }
284 //     }
285 //     if (fname.length() == 0)
286 //     {
287 //         throw mp::filter::FilterException("Missing conversion spec for "
288 //                                           "filter cql_rpn");
289 //     }
290
291 //     int error = 0;
292 //     if (!m_p->cql2rpn.parse_spec_file(fname.c_str(), &error))
293 //     {
294 //         throw mp::filter::FilterException("Bad or missing CQL to RPN spec "
295 //                                           + fname);
296 //     }
297 // }
298
299
300
301
302 static mp::filter::Base* filter_creator()
303 {
304     return new mp::filter::CQLtoRPN;
305 }
306
307 extern "C" {
308     struct metaproxy_1_filter_struct metaproxy_1_filter_cql_to_rpn = {
309         0,
310         "cql_rpn",
311         filter_creator
312     };
313 }
314
315 /*
316  * Local variables:
317  * c-basic-offset: 4
318  * indent-tabs-mode: nil
319  * c-file-style: "stroustrup"
320  * End:
321  * vim: shiftwidth=4 tabstop=8 expandtab
322  */