Bugfixes for browser history management. Documentation.
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / Pz2Bean.java
index 1c0fc07..30ac455 100644 (file)
@@ -113,6 +113,9 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
   public void doSearch() {\r
     if (errors.hasConfigurationErrors()) {\r
       logger.error("Ignoring search request due to configuration errors.");\r
+    } else if (searchClient == null){\r
+      logger.error("No search client defined. A client must either be pre-configured or selected before searching.");\r
+      errors.addConfigurationError(new ConfigurationError("No client defined","Client is null","No search client defined. A client must be pre-configured or selected runtime, prior to searching."));\r
     } else {\r
       stateMgr.hasPendingStateChange("search",false);\r
       pzresp.resetSearchResponses();\r
@@ -121,6 +124,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
       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
+      searchClient.setSearchCommand(pzreq.getCommand("search"));\r
       doCommand("search");\r
     }\r
   }\r
@@ -136,9 +140,12 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
       logger.debug("Ignoring record request due search error.");\r
       return "";\r
     } else {\r
+      logger.debug("Executing record command");\r
       ResponseDataObject responseObject = doCommand("record");\r
-      if (pzreq.getRecord().hasParameterValue("offset") ||\r
-            pzreq.getRecord().hasParameterValue("checksum")) {\r
+      if ((pzreq.getRecord().hasParameterValue("offset") ||\r
+            pzreq.getRecord().hasParameterValue("checksum")) &&\r
+            !responseObject.getType().equals("record")) {\r
+        logger.debug("Storing record offset response as 'record'");\r
         RecordResponse recordResponse = new RecordResponse();\r
         recordResponse.setType("record");\r
         recordResponse.setXml(responseObject.getXml());\r
@@ -191,7 +198,10 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
       } else {\r
         handleQueryStateChanges(commands);\r
         if (pzresp.getSearch().hasApplicationError()) {\r
-          logger.error("The command(s) " + commands + " are cancelled because the latest search command had an error.");\r
+          logger.error("The command(s) " + commands + " cancelled because the latest search command had an error.");\r
+          return "0";\r
+        } else if (errors.hasConfigurationErrors()) {\r
+          logger.error("The command(s) " + commands + " cancelled due to configuration errors.");\r
           return "0";\r
         } else {\r
           logger.debug("Processing request for " + commands); \r
@@ -281,10 +291,22 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     pager =  new ResultsPager(pzresp,pageRange,pzreq);\r
     return pager;\r
   }\r
-    \r
+   \r
+  /**\r
+   * This methods main purpose is to support browser history.\r
+   *  \r
+   * When the browsers back or forward buttons are pressed, a  \r
+   * re-search /might/ be required - namely if the query changes.\r
+   * So, as the UI requests updates of the page (show,facets,\r
+   * etc) this method checks if a search must be executed\r
+   * before those updates are performed.\r
+   *  \r
+   * @see {@link com.indexdata.mkjsf.pazpar2.state.StateManager#setCurrentStateKey} \r
+   * @param commands\r
+   */\r
   protected void handleQueryStateChanges (String commands) {\r
     if (stateMgr.hasPendingStateChange("search") && hasQuery()) { \r
-      logger.info("Triggered search: Found pending search change, doing search before updating " + commands);      \r
+      logger.info("Triggered search: Found pending search change [" + pzreq.getCommand("search").toString() + "], doing search before updating " + commands);      \r
       doSearch();\r
     } \r
     if (stateMgr.hasPendingStateChange("record") && ! commands.equals("record")) {        \r
@@ -307,7 +329,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
    */\r
   protected ResponseDataObject doCommand(String commandName) {\r
     ResponseDataObject responseObject = null;     \r
-    // logger.debug(pzreq.getCommand(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommand("search").getEncodedQueryString());\r
+    logger.info("Request "+commandName + ": "+ pzreq.getCommand("search").toString());\r
     Pazpar2Command command = pzreq.getCommand(commandName);\r
     long start = System.currentTimeMillis();\r
     HttpResponseWrapper commandResponse = searchClient.executeCommand(command);\r
@@ -316,8 +338,11 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
     responseLogger.debug("Response was: " + commandResponse.getResponseString());\r
     responseObject = ResponseParser.getParser().getDataObject((ClientCommandResponse)commandResponse);\r
     if (ResponseParser.docTypes.contains(responseObject.getType())) {\r
+      logger.debug("Storing " + responseObject.getType() + " in pzresp. ");\r
       pzresp.put(commandName, responseObject);\r
-    }          \r
+    } else {\r
+      logger.info("Unrecognized response object type not cached in pzresp: " + responseObject.getType());\r
+    }\r
     return responseObject;\r
   }\r
     \r
@@ -451,6 +476,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Configurable, Seria
       serviceProxyUrls = config.getMultiProperty(SERVICE_PROXY_URL_LIST,",");\r
       pazpar2Urls = config.getMultiProperty(PAZPAR2_URL_LIST, ",");\r
     }\r
+    logger.info(reader.document());\r
     logger.info("Service Type is configured to " + serviceType);\r
     \r
   }\r