zoom: may read cf-proxy's config settings
[metaproxy-moved-to-github.git] / src / filter_record_transform.cpp
index d3709bc..a7cb28c 100644 (file)
@@ -26,6 +26,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/zgdu.h>
 #include <yaz/retrieval.h>
 
+#include <boost/thread/mutex.hpp>
+
+#if HAVE_USEMARCON
+#include <usemarconlib.h>
+#include <defines.h>
+#endif
+
 #include <iostream>
 
 namespace mp = metaproxy_1;
@@ -46,6 +53,122 @@ namespace metaproxy_1 {
     }
 }
 
+#if HAVE_USEMARCON
+struct info_usemarcon {
+    boost::mutex m_mutex;
+
+    char *stage1;
+    char *stage2;
+
+    Usemarcon *usemarcon1;
+    Usemarcon *usemarcon2;
+};
+
+static int convert_usemarcon(void *info, WRBUF record, WRBUF wr_error)
+{
+    struct info_usemarcon *p = (struct info_usemarcon *) info;
+
+    boost::mutex::scoped_lock lock(p->m_mutex);
+
+    if (p->usemarcon1)
+    {
+        char *converted;
+        size_t convlen;
+        int res;
+
+        p->usemarcon1->SetMarcRecord(wrbuf_buf(record), wrbuf_len(record));
+        res = p->usemarcon1->Convert();
+        if (res != 0)
+        {
+            wrbuf_printf(wr_error, "usemarcon stage1 failed res=%d", res);
+            return -1;
+        }
+        p->usemarcon1->GetMarcRecord(converted, convlen);
+        
+        if (p->usemarcon2)
+        {
+            p->usemarcon2->SetMarcRecord(converted, convlen);
+            
+            res = p->usemarcon2->Convert();
+            free(converted);
+            if (res != 0)
+            {
+                wrbuf_printf(wr_error, "usemarcon stage2 failed res=%d",
+                             res);
+                return -1;
+            }
+            p->usemarcon2->GetMarcRecord(converted, convlen);
+        }
+        wrbuf_rewind(record);
+        wrbuf_write(record, converted, convlen);
+        free(converted);
+    }
+    return 0;
+}
+
+static void destroy_usemarcon(void *info)
+{
+    struct info_usemarcon *p = (struct info_usemarcon *) info;
+
+    delete p->usemarcon1;
+    delete p->usemarcon2;
+    xfree(p->stage1);
+    xfree(p->stage2);
+    delete p;
+}
+
+static void *construct_usemarcon(const xmlNode *ptr, const char *path,
+                                 WRBUF wr_error)
+{
+    struct _xmlAttr *attr;
+    if (strcmp((const char *) ptr->name, "usemarcon"))
+        return 0;
+
+    struct info_usemarcon *p = new(struct info_usemarcon);
+    p->stage1 = 0;
+    p->stage2 = 0;
+    p->usemarcon1 = 0;
+    p->usemarcon2 = 0;
+
+    for (attr = ptr->properties; attr; attr = attr->next)
+    {
+        if (!xmlStrcmp(attr->name, BAD_CAST "stage1") &&
+            attr->children && attr->children->type == XML_TEXT_NODE)
+            p->stage1 = xstrdup((const char *) attr->children->content);
+        else if (!xmlStrcmp(attr->name, BAD_CAST "stage2") &&
+            attr->children && attr->children->type == XML_TEXT_NODE)
+            p->stage2 = xstrdup((const char *) attr->children->content);
+        else
+        {
+            wrbuf_printf(wr_error, "Bad attribute '%s'"
+                         "Expected stage1 or stage2.", attr->name);
+            destroy_usemarcon(p);
+            return 0;
+        }
+    }
+
+    if (p->stage1)
+    {
+        p->usemarcon1 = new Usemarcon();
+        p->usemarcon1->SetIniFileName(p->stage1);
+    }
+    if (p->stage2)
+    {
+        p->usemarcon2 = new Usemarcon();
+        p->usemarcon2->SetIniFileName(p->stage2);
+    }
+    return p;
+}
+
+static void type_usemarcon(struct yaz_record_conv_type *t)
+{
+    t->next = 0;
+    t->construct = construct_usemarcon;
+    t->convert = convert_usemarcon;
+    t->destroy = destroy_usemarcon;
+}
+#endif
+
 // define Pimpl wrapper forwarding to Impl
  
 yf::RecordTransform::RecordTransform() : m_p(new Impl)
@@ -100,8 +223,16 @@ void yf::RecordTransform::Impl::configure(const xmlNode *xml_node,
             break;
     }
 
+#if HAVE_USEMARCON
+    struct yaz_record_conv_type mt;
+    type_usemarcon(&mt);
+    struct yaz_record_conv_type *t = &mt;
+#else
+    struct yaz_record_conv_type *t = 0;
+#endif
+
     // read configuration
-    if (0 != yaz_retrieval_configure(m_retrieval, retrieval_node))
+    if (0 != yaz_retrieval_configure_t(m_retrieval, retrieval_node, t))
     {
         std::string msg("RecordTransform filter config: ");
         msg += yaz_retrieval_get_error(m_retrieval);
@@ -330,7 +461,7 @@ void yf::RecordTransform::Impl::process(mp::Package &package) const
             Z_NamePlusRecord *npr = records->records[i];
             if (npr->which == Z_NamePlusRecord_databaseRecord)
             {
-                WRBUF output_record = wrbuf_alloc();
+                mp::wrbuf output_record;
                 Z_External *r = npr->u.databaseRecord;
                 int ret_trans = 0;
                 if (r->which == Z_External_OPAC)
@@ -351,8 +482,8 @@ void yf::RecordTransform::Impl::process(mp::Package &package) const
                 {
                     npr->u.databaseRecord =
                         z_ext_record_oid(odr_en, match_syntax,
-                                         wrbuf_buf(output_record),
-                                         wrbuf_len(output_record));
+                                         output_record.buf(),
+                                         output_record.len());
                 }
                 else
                 {
@@ -362,7 +493,6 @@ void yf::RecordTransform::Impl::process(mp::Package &package) const
                             YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
                             yaz_record_conv_get_error(rc));
                 }
-                wrbuf_destroy(output_record);
             }
         }
         package.response() = gdu_res;