record_transform: allow OPACXML to OPAC conversion
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 31 Jan 2013 10:32:36 +0000 (11:32 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 31 Jan 2013 10:32:36 +0000 (11:32 +0100)
src/filter_record_transform.cpp

index 9b14282..4218030 100644 (file)
@@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/diagbib1.h>
 #include <yaz/zgdu.h>
 #include <yaz/retrieval.h>
+#include <yaz/oid_db.h>
 
 #include <boost/thread/mutex.hpp>
 
@@ -461,14 +462,16 @@ void yf::RecordTransform::Impl::process(mp::Package &package) const
             Z_NamePlusRecord *npr = records->records[i];
             if (npr->which == Z_NamePlusRecord_databaseRecord)
             {
+                const char *details = 0;
                 mp::wrbuf output_record;
                 Z_External *r = npr->u.databaseRecord;
-                int ret_trans = 0;
+                int ret_trans = -1;
                 if (r->which == Z_External_OPAC)
                 {
                     ret_trans =
                         yaz_record_conv_opac_record(rc, r->u.opac,
                                                     output_record);
+                    details = yaz_record_conv_get_error(rc);
                 }
                 else if (r->which == Z_External_octet)
                 {
@@ -477,21 +480,53 @@ void yf::RecordTransform::Impl::process(mp::Package &package) const
                                                r->u.octet_aligned->buf,
                                                r->u.octet_aligned->len,
                                                output_record);
+                    details = yaz_record_conv_get_error(rc);
                 }
-                if (ret_trans == 0)
+                else
                 {
-                    npr->u.databaseRecord =
-                        z_ext_record_oid(odr_en, match_syntax,
-                                         output_record.buf(),
-                                         output_record.len());
+                    details = "unsupported record type for record_conv";
                 }
-                else
+                if (ret_trans)
                 {
                     records->records[i] =
                         zget_surrogateDiagRec(
                             odr_en, npr->databaseName,
                             YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
-                            yaz_record_conv_get_error(rc));
+                            details);
+                }
+                else
+                {
+                    if (!oid_oidcmp(match_syntax, yaz_oid_recsyn_opac))
+                    {
+                        yaz_marc_t mt = yaz_marc_create();
+                        Z_OPACRecord *opac = 0;
+                        if (yaz_xml_to_opac(mt, output_record.buf(),
+                                            output_record.len(),
+                                            &opac, 0 /* iconv */,
+                                            ((ODR )odr_en)->mem, 0)
+                            && opac)
+                        {
+                            npr->u.databaseRecord =
+                                z_ext_record_oid(odr_en, match_syntax,
+                                                 (const char *) opac, -1);
+                        }
+                        else
+                        {
+                            records->records[i] =
+                                zget_surrogateDiagRec(
+                                    odr_en, npr->databaseName,
+                                    YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
+                                    "XML to OPAC conversion failed");
+                        }
+                        yaz_marc_destroy(mt);
+                    }
+                    else
+                    {
+                        npr->u.databaseRecord =
+                            z_ext_record_oid(odr_en, match_syntax,
+                                             output_record.buf(),
+                                             output_record.len());
+                    }
                 }
             }
         }