+bool
+yf::SRUtoZ3950::Impl::z3950_close_request(mp::Package &package) const
+{
+ // prepare and close Z3950 package
+ Package z3950_package(package.session(), package.origin());
+ z3950_package.copy_filter(package);
+ z3950_package.session().close();
+
+ // set close APDU
+ //mp::odr odr_en(ODR_ENCODE);
+ //Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_close);
+ //z3950_package.request() = apdu;
+
+ z3950_package.move();
+
+ // check successful close response
+ //Z_GDU *z3950_gdu = z3950_package.response().get();
+ //if (z3950_gdu && z3950_gdu->which == Z_GDU_Z3950
+ // && z3950_gdu->u.z3950->which == Z_APDU_close)
+ // return true;
+
+ if (z3950_package.session().is_closed())
+ {
+ return true;
+ }
+ return false;
+}
+
+bool yf::SRUtoZ3950::Impl::z3950_search_request(mp::Package &package,
+ mp::odr &odr_en,
+ Z_SRW_PDU *sru_pdu_res,
+ Z_SRW_searchRetrieveRequest
+ const *sr_req,
+ std::string zurl) const
+{
+
+ assert(sru_pdu_res->u.response);
+
+ Package z3950_package(package.session(), package.origin());
+ z3950_package.copy_filter(package);
+
+ //add stuff in z3950 apdu
+ Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_searchRequest);
+ Z_SearchRequest *z_searchRequest = apdu->u.searchRequest;
+
+
+ if (!mp_util::set_databases_from_zurl(odr_en, zurl,
+ &z_searchRequest->num_databaseNames,
+ &z_searchRequest->databaseNames))
+ {
+ z_searchRequest->num_databaseNames = 1;
+ z_searchRequest->databaseNames = (char**)
+ odr_malloc(odr_en, sizeof(char *));
+
+ if (sr_req->database)
+ z_searchRequest->databaseNames[0]
+ = odr_strdup(odr_en, const_cast<char *>(sr_req->database));
+ else
+ z_searchRequest->databaseNames[0]
+ = odr_strdup(odr_en, "Default");
+ }
+
+ // z3950'fy query
+ Z_Query *z_query = (Z_Query *) odr_malloc(odr_en, sizeof(Z_Query));
+ z_searchRequest->query = z_query;
+
+ if (!z3950_build_query(odr_en, z_query, sr_req))
+ {
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED,
+ "query");
+ return false;
+ }
+
+ z3950_package.request() = apdu;
+
+ // send Z39.50 package off to backend
+ z3950_package.move();
+
+
+ Z_GDU *z3950_gdu = z3950_package.response().get();
+
+ //TODO: check success condition
+ //int yaz_diag_bib1_to_srw (int bib1_code);
+ //int yaz_diag_srw_to_bib1(int srw_code);
+ //Se kode i src/seshigh.c (srw_bend_search, srw_bend_init).
+
+ if (!z3950_gdu || z3950_gdu->which != Z_GDU_Z3950
+ || z3950_gdu->u.z3950->which != Z_APDU_searchResponse
+ || !z3950_gdu->u.z3950->u.searchResponse
+ || !z3950_gdu->u.z3950->u.searchResponse->searchStatus)
+ {
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ YAZ_SRW_SYSTEM_TEMPORARILY_UNAVAILABLE, 0);
+ return false;
+ }
+
+ // everything fine, continuing
+ Z_SearchResponse *sr = z3950_gdu->u.z3950->u.searchResponse;
+
+ // checking non surrogate diagnostics in Z3950 search response package
+ if (!z3950_to_srw_diagnostics_ok(odr_en, sru_pdu_res->u.response,
+ sr->records))
+ {
+ return false;
+ }
+
+ // Finally, roll on and srw'fy number of records
+ sru_pdu_res->u.response->numberOfRecords
+ = odr_intdup(odr_en, *sr->resultCount);
+
+ // srw'fy nextRecordPosition
+ //sru_pdu_res->u.response->nextRecordPosition
+ // = (int *) odr_malloc(odr_en, sizeof(int *));
+ //*(sru_pdu_res->u.response->nextRecordPosition) = 1;
+
+ return true;
+}
+
+bool
+yf::SRUtoZ3950::Impl::z3950_present_request(
+ mp::Package &package,
+ mp::odr &odr_en,
+ Z_SRW_PDU *sru_pdu_res,
+ const Z_SRW_searchRetrieveRequest *sr_req)
+ const
+{
+ assert(sru_pdu_res->u.response);
+ int start = 1;
+ int max_recs = 0;
+
+ if (!sr_req)
+ return false;
+
+ if (sr_req->maximumRecords)
+ max_recs = *sr_req->maximumRecords;
+ if (sr_req->startRecord)
+ start = *sr_req->startRecord;
+
+ // no need to work if nobody wants record ..
+ if (max_recs == 0)
+ return true;
+
+ bool send_z3950_present = true;
+
+ // recordXPath unsupported.
+ if (sr_req->recordXPath)
+ {
+ send_z3950_present = false;
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ YAZ_SRW_XPATH_RETRIEVAL_UNSUPP, 0);
+ }
+
+ // resultSetTTL unsupported.
+ // resultSetIdleTime in response
+ if (sr_req->resultSetTTL)
+ {
+ send_z3950_present = false;
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ YAZ_SRW_RESULT_SETS_UNSUPP, 0);
+ }
+
+ // sort unsupported
+ if (sr_req->sort_type != Z_SRW_sort_type_none)
+ {
+ send_z3950_present = false;
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ YAZ_SRW_SORT_UNSUPP, 0);
+ }
+
+ // start record requested negative, or larger than number of records
+ if (start < 0 || start > *sru_pdu_res->u.response->numberOfRecords)
+ {
+ send_z3950_present = false;
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ YAZ_SRW_FIRST_RECORD_POSITION_OUT_OF_RANGE, 0);
+ }
+
+ // maximumRecords requested negative
+ if (max_recs < 0)
+ {
+ send_z3950_present = false;
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ YAZ_SRW_UNSUPP_PARAMETER_VALUE,
+ "maximumRecords");
+ }
+
+ // exit on all these above diagnostics
+ if (!send_z3950_present)
+ return false;
+
+ if (max_recs > *sru_pdu_res->u.response->numberOfRecords - start)
+ max_recs = *sru_pdu_res->u.response->numberOfRecords - start + 1;
+
+ Z_SRW_searchRetrieveResponse *sru_res = sru_pdu_res->u.response;
+ sru_res->records = (Z_SRW_record *)
+ odr_malloc(odr_en, max_recs * sizeof(Z_SRW_record));
+ int num = 0;
+ while (num < max_recs)
+ {
+ // now packaging the z3950 present request
+ Package z3950_package(package.session(), package.origin());
+ z3950_package.copy_filter(package);
+ Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_presentRequest);
+
+ assert(apdu->u.presentRequest);
+
+ *apdu->u.presentRequest->resultSetStartPoint = start + num;
+ *apdu->u.presentRequest->numberOfRecordsRequested = max_recs - num;
+
+ // set response packing to be same as "request" packing..
+ int record_packing = Z_SRW_recordPacking_XML;
+ if (sr_req->recordPacking && 's' == *(sr_req->recordPacking))
+ record_packing = Z_SRW_recordPacking_string;
+
+ // RecordSyntax will always be XML
+ apdu->u.presentRequest->preferredRecordSyntax
+ = odr_oiddup(odr_en, yaz_oid_recsyn_xml);
+
+ // z3950'fy record schema
+ if (sr_req->recordSchema)
+ {
+ apdu->u.presentRequest->recordComposition
+ = (Z_RecordComposition *)
+ odr_malloc(odr_en, sizeof(Z_RecordComposition));
+ apdu->u.presentRequest->recordComposition->which
+ = Z_RecordComp_simple;
+ apdu->u.presentRequest->recordComposition->u.simple
+ = mp_util::build_esn_from_schema(odr_en,
+ (const char *)
+ sr_req->recordSchema);
+ }
+
+ // attaching Z3950 package to filter chain
+ z3950_package.request() = apdu;
+
+ // sending Z30.50 present request
+ z3950_package.move();
+
+ //check successful Z3950 present response
+ Z_GDU *z3950_gdu = z3950_package.response().get();
+ if (!z3950_gdu || z3950_gdu->which != Z_GDU_Z3950
+ || z3950_gdu->u.z3950->which != Z_APDU_presentResponse
+ || !z3950_gdu->u.z3950->u.presentResponse)
+
+ {
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ YAZ_SRW_SYSTEM_TEMPORARILY_UNAVAILABLE, 0);
+ return false;
+ }
+ // everything fine, continuing
+
+ Z_PresentResponse *pr = z3950_gdu->u.z3950->u.presentResponse;
+
+ // checking non surrogate diagnostics in Z3950 present response package
+ if (!z3950_to_srw_diagnostics_ok(odr_en, sru_pdu_res->u.response,
+ pr->records))
+ return false;
+
+ // if anything but database or surrogate diagnostics, stop
+ if (!pr->records || pr->records->which != Z_Records_DBOSD)
+ break;
+ else
+ {
+ // inserting all records
+ int returned_recs =
+ pr->records->u.databaseOrSurDiagnostics->num_records;
+ for (int i = 0; i < returned_recs; i++)
+ {
+ int position = i + *apdu->u.presentRequest->resultSetStartPoint;
+ Z_NamePlusRecord *npr
+ = pr->records->u.databaseOrSurDiagnostics->records[i];
+
+ sru_res->records[i + num].recordPacking = record_packing;
+
+ if (npr->which == Z_NamePlusRecord_databaseRecord &&
+ npr->u.databaseRecord->direct_reference
+ && !oid_oidcmp(npr->u.databaseRecord->direct_reference,
+ yaz_oid_recsyn_xml))
+ {
+ // got XML record back
+ Z_External *r = npr->u.databaseRecord;
+ sru_res->records[i + num].recordPosition =
+ odr_intdup(odr_en, position);
+ sru_res->records[i + num].recordSchema = sr_req->recordSchema;
+ sru_res->records[i + num].recordData_buf
+ = odr_strdupn(odr_en,
+ (const char *)r->u.octet_aligned->buf,
+ r->u.octet_aligned->len);
+ sru_res->records[i + num].recordData_len
+ = r->u.octet_aligned->len;
+ }
+ else
+ {
+ // not XML or no database record at all
+ yaz_mk_sru_surrogate(
+ odr_en, sru_res->records + i + num, position,
+ YAZ_SRW_RECORD_NOT_AVAILABLE_IN_THIS_SCHEMA, 0);
+ }
+ }
+ num += returned_recs;
+ }
+ }
+ sru_res->num_records = num;
+ if (start - 1 + num < *sru_pdu_res->u.response->numberOfRecords)
+ sru_res->nextRecordPosition =
+ odr_intdup(odr_en, start + num);
+ return true;
+}
+
+bool yf::SRUtoZ3950::Impl::z3950_build_query(mp::odr &odr_en, Z_Query *z_query,
+ const Z_SRW_searchRetrieveRequest *req
+ ) const
+{
+ if (req->query_type == Z_SRW_query_type_cql)
+ {
+ Z_External *ext = (Z_External *)
+ odr_malloc(odr_en, sizeof(*ext));
+ ext->direct_reference =
+ odr_getoidbystr(odr_en, "1.2.840.10003.16.2");
+ ext->indirect_reference = 0;
+ ext->descriptor = 0;
+ ext->which = Z_External_CQL;
+ ext->u.cql = odr_strdup(odr_en, req->query.cql);
+
+ z_query->which = Z_Query_type_104;
+ z_query->u.type_104 = ext;
+ return true;
+ }
+
+ if (req->query_type == Z_SRW_query_type_pqf)
+ {
+ Z_RPNQuery *RPNquery;
+ YAZ_PQF_Parser pqf_parser;
+
+ pqf_parser = yaz_pqf_create ();
+
+ RPNquery = yaz_pqf_parse (pqf_parser, odr_en, req->query.pqf);
+ if (!RPNquery)
+ {
+ std::cout << "TODO: Handeling of bad PQF\n";
+ std::cout << "TODO: Diagnostic to be send\n";
+ }
+ z_query->which = Z_Query_type_1;
+ z_query->u.type_1 = RPNquery;
+
+ yaz_pqf_destroy(pqf_parser);
+ return true;
+ }
+ return false;
+}
+
+
+bool
+yf::SRUtoZ3950::Impl::z3950_to_srw_diagnostics_ok(mp::odr &odr_en,
+ Z_SRW_searchRetrieveResponse
+ *sru_res,
+ Z_Records *records) const
+{
+ // checking non surrogate diagnostics in Z3950 present response package
+ if (records
+ && records->which == Z_Records_NSD
+ && records->u.nonSurrogateDiagnostic)
+ {
+ z3950_to_srw_diag(odr_en, sru_res,
+ records->u.nonSurrogateDiagnostic);
+ return false;
+ }
+ return true;
+}
+
+
+int
+yf::SRUtoZ3950::Impl::z3950_to_srw_diag(mp::odr &odr_en,
+ Z_SRW_searchRetrieveResponse *sru_res,
+ Z_DefaultDiagFormat *ddf) const
+{
+ int bib1_code = *ddf->condition;
+ sru_res->num_diagnostics = 1;
+ sru_res->diagnostics = (Z_SRW_diagnostic *)
+ odr_malloc(odr_en, sizeof(*sru_res->diagnostics));
+ yaz_mk_std_diagnostic(odr_en, sru_res->diagnostics,
+ yaz_diag_bib1_to_srw(bib1_code),
+ ddf->u.v2Addinfo);
+ return 0;
+}
+
+
+