added xml ressources - most notably the LOC marc21-toXYZ stylesheets
[metaproxy-moved-to-github.git] / src / filter_record_transform.cpp
1 /* $Id: filter_record_transform.cpp,v 1.2 2006-10-04 11:21:47 marc Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #include "config.hpp"
8 #include "filter.hpp"
9 #include "filter_record_transform.hpp"
10 #include "package.hpp"
11 #include "util.hpp"
12 #include "gduutil.hpp"
13 #include "xmlutil.hpp"
14
15 #include <yaz/zgdu.h>
16 #include <yaz/retrieval.h>
17
18 //#include <boost/thread/mutex.hpp>
19
20 #include <iostream>
21
22 namespace mp = metaproxy_1;
23 namespace yf = mp::filter;
24
25 namespace metaproxy_1 {
26     namespace filter {
27         class RecordTransform::Impl {
28         public:
29             Impl();
30             ~Impl();
31             void process(metaproxy_1::Package & package) const;
32             void configure(const xmlNode * xml_node);
33         private:
34             yaz_retrieval_t m_retrieval;
35         };
36     }
37 }
38
39 // define Pimpl wrapper forwarding to Impl
40  
41 yf::RecordTransform::RecordTransform() : m_p(new Impl)
42 {
43 }
44
45 yf::RecordTransform::~RecordTransform()
46 {  // must have a destructor because of boost::scoped_ptr
47 }
48
49 void yf::RecordTransform::configure(const xmlNode *xmlnode)
50 {
51     m_p->configure(xmlnode);
52 }
53
54 void yf::RecordTransform::process(mp::Package &package) const
55 {
56     m_p->process(package);
57 }
58
59
60 // define Implementation stuff
61
62
63
64 yf::RecordTransform::Impl::Impl() 
65 {
66     m_retrieval = yaz_retrieval_create();
67     assert(m_retrieval);
68 }
69
70 yf::RecordTransform::Impl::~Impl()
71
72     if (m_retrieval)
73         yaz_retrieval_destroy(m_retrieval);
74 }
75
76 void yf::RecordTransform::Impl::configure(const xmlNode *xml_node)
77 {
78     //const char *srcdir = getenv("srcdir");
79     //if (srcdir)
80     //    yaz_retrieval_set_path(m_retrieval, srcdir);
81
82     if (!xml_node)
83         throw mp::XMLError("RecordTransform filter config: empty XML DOM");
84
85     // parsing down to retrieval node, which can be any of the children nodes
86     xmlNode *retrieval_node;
87     for (retrieval_node = xml_node->children; 
88          retrieval_node; 
89          retrieval_node = retrieval_node->next)
90     {
91         if (retrieval_node->type != XML_ELEMENT_NODE)
92             continue;
93         if (0 == strcmp((const char *) retrieval_node->name, "retrievalinfo"))
94             break;
95     }
96     
97     // read configuration
98     if ( 0 != yaz_retrieval_configure(m_retrieval, retrieval_node)){
99         std::string msg("RecordTransform filter config: ");
100         msg += yaz_retrieval_get_error(m_retrieval);
101         throw mp::XMLError(msg);
102     }
103 }
104
105 void yf::RecordTransform::Impl::process(mp::Package &package) const
106 {
107
108     Z_GDU *gdu_req = package.request().get();
109     
110     // only working on z3950 present packages
111     if (!gdu_req 
112         || !(gdu_req->which == Z_GDU_Z3950) 
113         || !(gdu_req->u.z3950->which == Z_APDU_presentRequest))
114     {
115         package.move();
116         return;
117     }
118     
119     // getting original present request
120     Z_PresentRequest *pr = gdu_req->u.z3950->u.presentRequest;
121
122     // setting up ODR's for memory during encoding/decoding
123     //mp::odr odr_de(ODR_DECODE);  
124     mp::odr odr_en(ODR_ENCODE);
125
126     // now re-insructing the z3950 backend present request
127      
128     // z3950'fy record syntax
129     //Odr_oid odr_oid;
130     if(pr->preferredRecordSyntax){
131         (pr->preferredRecordSyntax)
132             = yaz_str_to_z3950oid(odr_en, CLASS_RECSYN, "xml");
133         
134         // = yaz_oidval_to_z3950oid (odr_en, CLASS_RECSYN, VAL_TEXT_XML);
135     }
136     // Odr_oid *yaz_str_to_z3950oid (ODR o, int oid_class,
137     //                                         const char *str);
138     // const char *yaz_z3950oid_to_str (Odr_oid *oid, int *oid_class);
139
140
141     // z3950'fy record schema
142     //if ()
143     // {
144     //     pr->recordComposition 
145     //         = (Z_RecordComposition *) 
146     //           odr_malloc(odr_en, sizeof(Z_RecordComposition));
147     //     pr->recordComposition->which 
148     //         = Z_RecordComp_simple;
149     //     pr->recordComposition->u.simple 
150     //         = build_esn_from_schema(odr_en, 
151     //                                 (const char *) sr_req->recordSchema); 
152     // }
153
154     // attaching Z3950 package to filter chain
155     package.request() = gdu_req;
156
157     // std::cout << "z3950_present_request " << *apdu << "\n";   
158
159     // sending package
160     package.move();
161
162    //check successful Z3950 present response
163     Z_GDU *gdu_res = package.response().get();
164     if (!gdu_res || gdu_res->which != Z_GDU_Z3950 
165         || gdu_res->u.z3950->which != Z_APDU_presentResponse
166         || !gdu_res->u.z3950->u.presentResponse)
167
168     {
169         std::cout << "record-transform: error back present\n";
170         package.session().close();
171         return;
172     }
173     
174
175     // everything fine, continuing
176     // std::cout << "z3950_present_request OK\n";
177     // std::cout << "back z3950 " << *gdu_res << "\n";
178
179
180     return;
181 }
182
183
184 static mp::filter::Base* filter_creator()
185 {
186     return new mp::filter::RecordTransform;
187 }
188
189 extern "C" {
190     struct metaproxy_1_filter_struct metaproxy_1_filter_record_transform = {
191         0,
192         "record_transform",
193         filter_creator
194     };
195 }
196
197
198 /*
199  * Local variables:
200  * c-basic-offset: 4
201  * indent-tabs-mode: nil
202  * c-file-style: "stroustrup"
203  * End:
204  * vim: shiftwidth=4 tabstop=8 expandtab
205  */