21 #include <metaproxy/package.hpp>
22 #include <metaproxy/util.hpp>
25 #include <yaz/diagbib1.h>
27 #include <yaz/retrieval.h>
28 #include <yaz/oid_db.h>
30 #include <boost/thread/mutex.hpp>
33 #include <usemarconlib.h>
40 namespace yf = mp::filter;
49 void process(metaproxy_1::Package & package)
const;
50 void configure(
const xmlNode * xml_node,
const char *path);
58 struct info_usemarcon {
64 Usemarcon *usemarcon1;
65 Usemarcon *usemarcon2;
68 static int convert_usemarcon(
void *info, WRBUF record, WRBUF wr_error)
70 struct info_usemarcon *p = (
struct info_usemarcon *) info;
72 boost::mutex::scoped_lock lock(p->m_mutex);
80 p->usemarcon1->SetMarcRecord(wrbuf_buf(record), wrbuf_len(record));
81 res = p->usemarcon1->Convert();
84 wrbuf_printf(wr_error,
"usemarcon stage1 failed res=%d", res);
87 p->usemarcon1->GetMarcRecord(converted, convlen);
91 p->usemarcon2->SetMarcRecord(converted, convlen);
93 res = p->usemarcon2->Convert();
97 wrbuf_printf(wr_error,
"usemarcon stage2 failed res=%d",
101 p->usemarcon2->GetMarcRecord(converted, convlen);
103 wrbuf_rewind(record);
104 wrbuf_write(record, converted, convlen);
110 static void destroy_usemarcon(
void *info)
112 struct info_usemarcon *p = (
struct info_usemarcon *) info;
114 delete p->usemarcon1;
115 delete p->usemarcon2;
121 static void *construct_usemarcon(
const xmlNode *ptr,
const char *path,
124 struct _xmlAttr *attr;
125 if (strcmp((
const char *) ptr->name,
"usemarcon"))
128 struct info_usemarcon *p =
new(
struct info_usemarcon);
134 for (attr = ptr->properties; attr; attr = attr->next)
136 if (!xmlStrcmp(attr->name, BAD_CAST
"stage1") &&
137 attr->children && attr->children->type == XML_TEXT_NODE)
138 p->stage1 = xstrdup((
const char *) attr->children->content);
139 else if (!xmlStrcmp(attr->name, BAD_CAST
"stage2") &&
140 attr->children && attr->children->type == XML_TEXT_NODE)
141 p->stage2 = xstrdup((
const char *) attr->children->content);
144 wrbuf_printf(wr_error,
"Bad attribute '%s'"
145 "Expected stage1 or stage2.", attr->name);
146 destroy_usemarcon(p);
153 p->usemarcon1 =
new Usemarcon();
154 p->usemarcon1->SetIniFileName(p->stage1);
158 p->usemarcon2 =
new Usemarcon();
159 p->usemarcon2->SetIniFileName(p->stage2);
164 static void type_usemarcon(
struct yaz_record_conv_type *t)
167 t->construct = construct_usemarcon;
168 t->convert = convert_usemarcon;
169 t->destroy = destroy_usemarcon;
175 yf::RecordTransform::RecordTransform() : m_p(new
Impl)
179 yf::RecordTransform::~RecordTransform()
183 void yf::RecordTransform::configure(
const xmlNode *xmlnode,
bool test_only,
186 m_p->configure(xmlnode, path);
189 void yf::RecordTransform::process(mp::Package &package)
const
191 m_p->process(package);
195 yf::RecordTransform::Impl::Impl()
197 m_retrieval = yaz_retrieval_create();
201 yf::RecordTransform::Impl::~Impl()
204 yaz_retrieval_destroy(m_retrieval);
207 void yf::RecordTransform::Impl::configure(
const xmlNode *xml_node,
210 yaz_retrieval_set_path(m_retrieval, path);
213 throw mp::XMLError(
"RecordTransform filter config: empty XML DOM");
216 xmlNode *retrieval_node;
217 for (retrieval_node = xml_node->children;
219 retrieval_node = retrieval_node->next)
221 if (retrieval_node->type != XML_ELEMENT_NODE)
223 if (0 == strcmp((
const char *) retrieval_node->name,
"retrievalinfo"))
228 struct yaz_record_conv_type mt;
230 struct yaz_record_conv_type *t = &mt;
232 struct yaz_record_conv_type *t = 0;
236 if (0 != yaz_retrieval_configure_t(m_retrieval, retrieval_node, t))
238 std::string msg(
"RecordTransform filter config: ");
239 msg += yaz_retrieval_get_error(m_retrieval);
240 throw mp::XMLError(msg);
245 Odr_oid *match_syntax,
247 Z_NamePlusRecordList *records,
int i)
249 Z_NamePlusRecord *npr = records->records[i];
250 const char *details = 0;
251 mp::wrbuf output_record;
252 Z_External *r = npr->u.databaseRecord;
254 if (npr->which != Z_NamePlusRecord_databaseRecord)
256 r = npr->u.databaseRecord;
257 if (r->which == Z_External_OPAC)
259 ret_trans = yaz_record_conv_opac_record(rc, r->u.opac,
261 details = yaz_record_conv_get_error(rc);
263 else if (r->which == Z_External_octet)
266 yaz_record_conv_record(
267 rc, (
const char *) r->u.octet_aligned->buf,
268 r->u.octet_aligned->len, output_record);
269 details = yaz_record_conv_get_error(rc);
273 details =
"unsupported record type for record_conv";
277 records->records[i] =
278 zget_surrogateDiagRec(
279 odr_en, npr->databaseName,
280 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
285 if (!oid_oidcmp(match_syntax, yaz_oid_recsyn_opac))
288 yaz_marc_t mt = yaz_marc_create();
289 Z_OPACRecord *opac = 0;
290 const char *output_charset =
291 yaz_record_get_output_charset(rc);
293 cd = yaz_iconv_open(output_charset,
"utf-8");
294 if (yaz_xml_to_opac(mt, output_record.buf(),
297 ((ODR )odr_en)->mem, 0)
300 npr->u.databaseRecord =
301 z_ext_record_oid(odr_en, match_syntax,
302 (
const char *) opac, -1);
306 records->records[i] =
307 zget_surrogateDiagRec(
308 odr_en, npr->databaseName,
309 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
310 "XML to OPAC conversion failed");
312 yaz_marc_destroy(mt);
316 npr->u.databaseRecord = z_ext_record_oid(odr_en, match_syntax,
318 output_record.len());
323 void yf::RecordTransform::Impl::process(mp::Package &package)
const
326 Z_GDU *gdu_req = package.request().get();
327 Z_PresentRequest *pr_req = 0;
328 Z_SearchRequest *sr_req = 0;
330 const char *input_schema = 0;
331 Odr_oid *input_syntax = 0;
333 if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
334 gdu_req->u.z3950->which == Z_APDU_presentRequest)
336 pr_req = gdu_req->u.z3950->u.presentRequest;
339 mp_util::record_composition_to_esn(pr_req->recordComposition);
340 input_syntax = pr_req->preferredRecordSyntax;
342 else if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
343 gdu_req->u.z3950->which == Z_APDU_searchRequest)
345 sr_req = gdu_req->u.z3950->u.searchRequest;
347 input_syntax = sr_req->preferredRecordSyntax;
353 if (sr_req->smallSetElementSetNames
355 sr_req->mediumSetElementSetNames
357 sr_req->smallSetElementSetNames->which == Z_ElementSetNames_generic
359 sr_req->mediumSetElementSetNames->which == Z_ElementSetNames_generic
361 !strcmp(sr_req->smallSetElementSetNames->u.generic,
362 sr_req->mediumSetElementSetNames->u.generic))
364 input_schema = sr_req->smallSetElementSetNames->u.generic;
366 else if (*sr_req->largeSetLowerBound > 1
367 && !sr_req->smallSetElementSetNames && !sr_req->mediumSetElementSetNames)
372 *sr_req->smallSetUpperBound = 0;
373 *sr_req->largeSetLowerBound = 1;
374 *sr_req->mediumSetPresentNumber = 0;
386 mp::odr odr_en(ODR_ENCODE);
389 yaz_record_conv_t rc = 0;
391 const char *match_schema = 0;
392 Odr_oid *match_syntax = 0;
394 const char *backend_schema = 0;
395 Odr_oid *backend_syntax = 0;
398 = yaz_retrieval_request(m_retrieval,
399 input_schema, input_syntax,
400 &match_schema, &match_syntax,
402 &backend_schema, &backend_syntax);
407 const char *details = 0;
411 details = yaz_retrieval_get_error(m_retrieval);
412 error_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
414 else if (ret_code == 1 || ret_code == 3)
416 details = input_schema;
417 error_code = YAZ_BIB1_ELEMENT_SET_NAMES_UNSUPP;
419 else if (ret_code == 2)
421 char oidbuf[OID_STR_MAX];
422 oid_oid_to_dotstring(input_syntax, oidbuf);
423 details = odr_strdup(odr_en, oidbuf);
424 error_code = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
428 char *tmp = (
char*) odr_malloc(odr_en, 80);
430 "record_transform: yaz_retrieval_get_error returned %d",
433 error_code = YAZ_BIB1_UNSPECIFIED_ERROR;
438 apdu = odr_en.create_searchResponse(
439 gdu_req->u.z3950, error_code, details);
443 apdu = odr_en.create_presentResponse(
444 gdu_req->u.z3950, error_code, details);
446 package.response() = apdu;
453 sr_req->preferredRecordSyntax = odr_oiddup(odr_en, backend_syntax);
455 sr_req->preferredRecordSyntax = 0;
459 sr_req->smallSetElementSetNames
460 = (Z_ElementSetNames *)
461 odr_malloc(odr_en,
sizeof(Z_ElementSetNames));
462 sr_req->smallSetElementSetNames->which = Z_ElementSetNames_generic;
463 sr_req->smallSetElementSetNames->u.generic
464 = odr_strdup(odr_en, backend_schema);
465 sr_req->mediumSetElementSetNames = sr_req->smallSetElementSetNames;
469 sr_req->smallSetElementSetNames = 0;
470 sr_req->mediumSetElementSetNames = 0;
476 pr_req->preferredRecordSyntax = odr_oiddup(odr_en, backend_syntax);
478 pr_req->preferredRecordSyntax = 0;
482 pr_req->recordComposition
483 = (Z_RecordComposition *)
484 odr_malloc(odr_en,
sizeof(Z_RecordComposition));
485 pr_req->recordComposition->which
486 = Z_RecordComp_simple;
487 pr_req->recordComposition->u.simple
488 = (Z_ElementSetNames *)
489 odr_malloc(odr_en,
sizeof(Z_ElementSetNames));
490 pr_req->recordComposition->u.simple->which = Z_ElementSetNames_generic;
491 pr_req->recordComposition->u.simple->u.generic
492 = odr_strdup(odr_en, backend_schema);
495 pr_req->recordComposition = 0;
499 package.request() = gdu_req;
503 Z_GDU *gdu_res = package.response().get();
506 Z_NamePlusRecordList *records = 0;
507 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
508 gdu_res->u.z3950->which == Z_APDU_presentResponse)
510 Z_PresentResponse * pr_res = gdu_res->u.z3950->u.presentResponse;
513 && pr_res->numberOfRecordsReturned
514 && *(pr_res->numberOfRecordsReturned) > 0
516 && pr_res->records->which == Z_Records_DBOSD)
518 records = pr_res->records->u.databaseOrSurDiagnostics;
521 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
522 gdu_res->u.z3950->which == Z_APDU_searchResponse)
524 Z_SearchResponse *sr_res = gdu_res->u.z3950->u.searchResponse;
527 && sr_res->numberOfRecordsReturned
528 && *(sr_res->numberOfRecordsReturned) > 0
530 && sr_res->records->which == Z_Records_DBOSD)
532 records = sr_res->records->u.databaseOrSurDiagnostics;
539 for (i = 0; i < records->num_records; i++)
541 package.response() = gdu_res;
548 return new mp::filter::RecordTransform;