Adds support for target category command - 'categories'
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / Pz2Bean.java
index d7a9235..1c0fc07 100644 (file)
@@ -104,35 +104,49 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     pzreq.getRecord().removeParametersInState();\r
     pzreq.getSearch().removeParametersInState();   \r
   }\r
-\r
     \r
   public void doSearch(String query) {\r
     pzreq.getSearch().setParameter(new CommandParameter("query","=",query));     \r
     doSearch();\r
   }\r
 \r
-  public void doSearch() { \r
-    stateMgr.hasPendingStateChange("search",false);\r
-    pzresp.resetSearchResponses();\r
-    // resets some record and show command parameters without \r
-    // changing state or creating state change feedback\r
-    pzreq.getRecord().removeParametersInState();        \r
-    pzreq.getShow().setParameterInState(new CommandParameter("start","=",0));    \r
-    logger.debug(Utils.objectId(this) + " is searching using "+pzreq.getCommand("search").getUrlEncodedParameterValue("query"));\r
-    doCommand("search");      \r
+  public void doSearch() {\r
+    if (errors.hasConfigurationErrors()) {\r
+      logger.error("Ignoring search request due to configuration errors.");\r
+    } else {\r
+      stateMgr.hasPendingStateChange("search",false);\r
+      pzresp.resetSearchResponses();\r
+      // resets some record and show command parameters without \r
+      // changing state or creating state change feedback\r
+      pzreq.getRecord().removeParametersInState();        \r
+      pzreq.getShow().setParameterInState(new CommandParameter("start","=",0));    \r
+      logger.debug(Utils.objectId(this) + " is searching using "+pzreq.getCommand("search").getUrlEncodedParameterValue("query"));\r
+      doCommand("search");\r
+    }\r
   }\r
   \r
   public String doRecord() {\r
-    ResponseDataObject responseObject = doCommand("record");\r
-    if (pzreq.getRecord().hasParameterValue("offset") ||\r
-          pzreq.getRecord().hasParameterValue("checksum")) {\r
+    if (errors.hasConfigurationErrors()) {\r
+      logger.error("Ignoring record request due to configuration errors.");\r
+      return "";\r
+    } else if (!pzreq.getCommand("record").hasParameterValue("id")) {\r
+      logger.debug("Ignoring record request due to no id parameter.");\r
+      return "";\r
+    } else if (pzresp.getSearch().hasApplicationError()) {\r
+      logger.debug("Ignoring record request due search error.");\r
+      return "";\r
+    } else {\r
+      ResponseDataObject responseObject = doCommand("record");\r
+      if (pzreq.getRecord().hasParameterValue("offset") ||\r
+            pzreq.getRecord().hasParameterValue("checksum")) {\r
         RecordResponse recordResponse = new RecordResponse();\r
         recordResponse.setType("record");\r
         recordResponse.setXml(responseObject.getXml());\r
         recordResponse.setAttribute("activeclients", "0");\r
         pzresp.put("record", recordResponse);\r
-     }\r
-     return pzresp.getRecord().getActiveClients();    \r
+      }\r
+      return pzresp.getRecord().getActiveClients();\r
+    }\r
   }\r
       \r
   /**\r
@@ -142,29 +156,20 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
    */\r
   public String update () {\r
     logger.debug("Updating show,stat,termlist,bytarget from pazpar2");\r
-    return update("show,stat,termlist,bytarget");\r
-  }\r
-  \r
-  public boolean validateUpdateRequest(String commands) {\r
     if (errors.hasConfigurationErrors()) {\r
-      logger.error("The command(s) " + commands + " are cancelled due to configuration errors.");\r
-      return false;\r
-    } else if (!commands.equals("search") && pzresp.getSearch().hasApplicationError()) {\r
-      logger.error("The command(s) " + commands + " are cancelled because the latest search command had an error.");\r
-      return false;\r
-    } else if (!commandsAreValid(commands)) {\r
-      logger.debug("The command(s) " + commands + " are cancelled because the were not found to be ready/valid.");\r
-      return false;\r
-    } else if (!hasQuery() &&  !(commands.equals("record") && pzreq.getCommand("record").hasParameterValue("recordquery"))) {\r
-      logger.debug("The command(s) " + commands + " are held off because there's not yet a query.");\r
-      return false;\r
+      logger.error("Ignoring show,stat,termlist,bytarget commands due to configuration errors.");\r
+      return "";\r
+    } else if (pzresp.getSearch().hasApplicationError()) {\r
+      logger.error("Ignoring show,stat,termlist,bytarget commands due to problem with most recent search.");\r
+      return "";\r
+    } else if (!hasQuery()) {\r
+      logger.error("Ignoring show,stat,termlist,bytarget commands because there is not yet a query.");\r
+      return "";\r
     } else {\r
-      return true;\r
+      return update("show,stat,termlist,bytarget");\r
     }\r
-    \r
-    \r
   }\r
-   \r
+     \r
   /**\r
    * Refreshes the data objects listed in 'commands' from pazpar2\r
    * \r
@@ -174,9 +179,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
   public String update (String commands) {\r
     logger.info("Request to update: " + commands);\r
     try {\r
-      if (!validateUpdateRequest(commands)) {\r
-        return "0";\r
-      } else if (commands.equals("search")) {\r
+      if (commands.equals("search")) {\r
         doSearch();\r
         return "";\r
       } else if (commands.equals("record")) {\r
@@ -234,17 +237,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     }\r
     \r
   }\r
-  \r
-  public boolean commandsAreValid(String commands) {\r
-    if (commands.equals("record")) {\r
-      if (!pzreq.getCommand("record").hasParameterValue("id")) {\r
-        logger.debug("Skips sending record command due to lacking id parameter");\r
-        return false;\r
-      }\r
-    }\r
-    return true;\r
-  }\r
-                                \r
+                                  \r
   public String toggleRecord (String recId) {\r
     if (hasRecord(recId)) {\r
       pzreq.getRecord().removeParameters();  \r
@@ -313,20 +306,18 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
    * @return An XML response parsed to form a response data object\r
    */\r
   protected ResponseDataObject doCommand(String commandName) {\r
-    ResponseDataObject responseObject = null; \r
-    if (validateUpdateRequest(commandName)) {\r
-      logger.debug(pzreq.getCommand(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommand("search").getEncodedQueryString());\r
-      Pazpar2Command command = pzreq.getCommand(commandName);\r
-      long start = System.currentTimeMillis();\r
-      HttpResponseWrapper commandResponse = searchClient.executeCommand(command);\r
-      long end = System.currentTimeMillis();\r
-      logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." );\r
-      responseLogger.debug("Response was: " + commandResponse.getResponseString());\r
-      responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse);\r
-      if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
-        pzresp.put(commandName, responseObject);\r
-      }      \r
-    }\r
+    ResponseDataObject responseObject = null;     \r
+    // logger.debug(pzreq.getCommand(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommand("search").getEncodedQueryString());\r
+    Pazpar2Command command = pzreq.getCommand(commandName);\r
+    long start = System.currentTimeMillis();\r
+    HttpResponseWrapper commandResponse = searchClient.executeCommand(command);\r
+    long end = System.currentTimeMillis();\r
+    logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." );\r
+    responseLogger.debug("Response was: " + commandResponse.getResponseString());\r
+    responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse);\r
+    if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
+      pzresp.put(commandName, responseObject);\r
+    }          \r
     return responseObject;\r
   }\r
     \r