Loose the query parsing so that Pazpar2 only returns error if _all_
[pazpar2-moved-to-github.git] / src / logic.c
index d0a92a8..e7f2941 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: logic.c,v 1.43 2007-06-13 21:29:04 adam Exp $
+/* $Id: logic.c,v 1.48 2007-07-04 12:07:49 adam Exp $
    Copyright (c) 2006-2007, Index Data.
 
 This file is part of Pazpar2.
@@ -45,9 +45,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <yaz/nmem.h>
 #include <yaz/query-charset.h>
 #include <yaz/querytowrbuf.h>
-#if YAZ_VERSIONL >= 0x020163
 #include <yaz/oid_db.h>
-#endif
 
 #if HAVE_CONFIG_H
 #include "cconfig.h"
@@ -152,9 +150,8 @@ static void add_facet(struct session *s, const char *type, const char *value)
     termlist_insert(s->termlists[i].termlist, value);
 }
 
-xmlDoc *normalize_record(struct session_database *sdb, Z_External *rec)
+xmlDoc *record_to_xml(struct session_database *sdb, Z_External *rec)
 {
-    struct database_retrievalmap *m;
     struct database *db = sdb->database;
     xmlDoc *rdoc = 0;
     const Odr_oid *oid = rec->direct_reference;
@@ -233,36 +230,45 @@ xmlDoc *normalize_record(struct session_database *sdb, Z_External *rec)
         xmlDocDump(stderr, rdoc);
 #endif
     }
+    return rdoc;
+}
 
-    for (m = sdb->map; m; m = m->next){
-        xmlDoc *new = 0;
-
-        {
-            xmlNodePtr root = 0;
-            new = xsltApplyStylesheet(m->stylesheet, rdoc, 0);
-            root= xmlDocGetRootElement(new);
-        if (!new || !root || !(root->children))
-        {
-            yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
-                    sdb->database->url);
-            xmlFreeDoc(new);
+xmlDoc *normalize_record(struct session_database *sdb, Z_External *rec)
+{
+    struct database_retrievalmap *m;
+    xmlDoc *rdoc = record_to_xml(sdb, rec);
+    if (rdoc)
+    {
+        for (m = sdb->map; m; m = m->next){
+            xmlDoc *new = 0;
+            
+            {
+                xmlNodePtr root = 0;
+                new = xsltApplyStylesheet(m->stylesheet, rdoc, 0);
+                root= xmlDocGetRootElement(new);
+                if (!new || !root || !(root->children))
+                {
+                    yaz_log(YLOG_WARN, "XSLT transformation failed from %s",
+                            sdb->database->url);
+                    xmlFreeDoc(new);
+                    xmlFreeDoc(rdoc);
+                    return 0;
+                }
+            }
+            
             xmlFreeDoc(rdoc);
-            return 0;
+            rdoc = new;
         }
-        }
-   
-        xmlFreeDoc(rdoc);
-        rdoc = new;
-    }
-    if (global_parameters.dump_records)
-    {
-        fprintf(stderr, "Record from %s\n----------------\n", 
-                sdb->database->url);
+        if (global_parameters.dump_records)
+        {
+            fprintf(stderr, "Record from %s\n----------------\n", 
+                    sdb->database->url);
 #if LIBXML_VERSION >= 20600
-        xmlDocFormatDump(stderr, rdoc, 1);
+            xmlDocFormatDump(stderr, rdoc, 1);
 #else
-        xmlDocDump(stderr, rdoc);
+            xmlDocDump(stderr, rdoc);
 #endif
+        }
     }
     return rdoc;
 }
@@ -341,7 +347,7 @@ static int prepare_map(struct session *se, struct session_database *sdb)
             (*m)->next = 0;
             if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
             {
-                yaz_log(YLOG_FATAL, "Unable to load stylesheet: %s",
+                yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
                         stylesheets[i]);
                 return -1;
             }
@@ -378,21 +384,39 @@ static int prepare_session_database(struct session *se,
     return 0;
 }
 
+// called if watch should be removed because http_channel is to be destroyed
+static void session_watch_cancel(void *data, struct http_channel *c)
+{
+    struct session_watchentry *ent = data;
+
+    ent->fun = 0;
+    ent->data = 0;
+    ent->obs = 0;
+}
 
-void session_set_watch(struct session *s, int what, 
-                       session_watchfun fun, void *data)
+// set watch. Returns 0=OK, -1 if watch is already set
+int session_set_watch(struct session *s, int what, 
+                      session_watchfun fun, void *data,
+                      struct http_channel *chan)
 {
+    if (s->watchlist[what].fun)
+        return -1;
     s->watchlist[what].fun = fun;
     s->watchlist[what].data = data;
+    s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
+                                               session_watch_cancel);
+    return 0;
 }
 
 void session_alert_watch(struct session *s, int what)
 {
     if (!s->watchlist[what].fun)
         return;
+    http_remove_observer(s->watchlist[what].obs);
     (*s->watchlist[what].fun)(s->watchlist[what].data);
     s->watchlist[what].fun = 0;
     s->watchlist[what].data = 0;
+    s->watchlist[what].obs = 0;
 }
 
 //callback for grep_databases
@@ -473,6 +497,8 @@ enum pazpar2_error_code search(struct session *se,
                                const char **addinfo)
 {
     int live_channels = 0;
+    int no_working = 0;
+    int no_failed = 0;
     struct client *cl;
     struct database_criterion *criteria;
 
@@ -504,17 +530,22 @@ enum pazpar2_error_code search(struct session *se,
             *addinfo = client_get_database(cl)->database->url;
             return PAZPAR2_CONFIG_TARGET;
         }
-        // Query must parse for all targets
+        // Parse query for target
         if (client_parse_query(cl, query) < 0)
+            no_failed++;
+        else
         {
-            *addinfo = "query";
-            return PAZPAR2_MALFORMED_PARAMETER_VALUE;
+            no_working++;
+            client_prep_connection(cl);
         }
     }
 
-    for (cl = se->clients; cl; cl = client_next_in_session(cl))
-        client_prep_connection(cl);
-
+    // If no queries could be mapped, we signal an error
+    if (no_working == 0)
+    {
+        *addinfo = "query";
+        return PAZPAR2_MALFORMED_PARAMETER_VALUE;
+    }
     return PAZPAR2_NO_ERROR;
 }
 
@@ -570,7 +601,7 @@ static void session_database_destroy(struct session_database *sdb)
 void session_init_databases(struct session *se)
 {
     se->databases = 0;
-    grep_databases(se, 0, session_init_databases_fun);
+    predef_grep_databases(se, 0, session_init_databases_fun);
 }
 
 // Probably session_init_databases_fun should be refactored instead of
@@ -677,7 +708,7 @@ struct session *new_session(NMEM nmem)
     session->session_nmem = nmem;
     session->nmem = nmem_create();
     session->wrbuf = wrbuf_alloc();
-    session_init_databases(session);
+    session->databases = 0;
     for (i = 0; i <= SESSION_WATCH_MAX; i++)
     {
         session->watchlist[i].data = 0;