Ammends some fringe scenarios with state keys and states
authorNiels Erik G. Nielsen <nielserik@indexdata.com>
Mon, 15 Apr 2013 01:37:14 +0000 (21:37 -0400)
committerNiels Erik G. Nielsen <nielserik@indexdata.com>
Mon, 15 Apr 2013 01:37:14 +0000 (21:37 -0400)
.. also makes state key a hashcode - to shorten it and because the
   textual key would often not seem quite accurate - even though it
   would accurately fetch the correct state -- and thus could cause
   confusion for one inspecting it

src/META-INF/resources/pz2utils/listeners.js
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/Pazpar2Commands.java
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/Pazpar2State.java
src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/StateManager.java

index 84cd19b..8c1f466 100644 (file)
@@ -50,7 +50,7 @@
              if (window.location.hash != stateKey.value) {\r
                //console.log("updating stateKey with new browser hash: " + window.location.hash);\r
                stateKey.value = window.location.hash;\r
-               if (! stateKey.value) window.location.hash = '#initial';\r
+               if (! stateKey.value) window.location.hash = '#1';\r
                stateKey.onchange();\r
              } else {\r
                //console.log("State hash already has the value of the new browser hash - not updating state hash");\r
index d948152..97f0880 100644 (file)
@@ -121,48 +121,63 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
    */\r
   public String update (String commands) {\r
     if (! hasConfigurationErrors()) {\r
-      if (hasQuery()) {\r
-        handleQueryStateChanges(commands);\r
-        logger.debug("Processing request for " + commands); \r
-        List<CommandThread> threadList = new ArrayList<CommandThread>();\r
-        StringTokenizer tokens = new StringTokenizer(commands,",");\r
-        while (tokens.hasMoreElements()) {          \r
-          threadList.add(new CommandThread(pzreq.getCommandReadOnly(tokens.nextToken()),searchClient));            \r
-        }\r
-        for (CommandThread thread : threadList) {\r
-          thread.start();\r
-        }\r
-        for (CommandThread thread : threadList) {\r
-          try {\r
-            thread.join();\r
-          } catch (InterruptedException e) {\r
-            e.printStackTrace();\r
+      if (commandsAreValid(commands)) {\r
+        if (hasQuery()) {\r
+          handleQueryStateChanges(commands);\r
+          logger.debug("Processing request for " + commands); \r
+          List<CommandThread> threadList = new ArrayList<CommandThread>();\r
+          StringTokenizer tokens = new StringTokenizer(commands,",");\r
+          while (tokens.hasMoreElements()) {          \r
+            threadList.add(new CommandThread(pzreq.getCommandReadOnly(tokens.nextToken()),searchClient));            \r
           }\r
-        }\r
-        for (CommandThread thread : threadList) {\r
-           String commandName = thread.getCommand().getName();\r
-           String response = thread.getResponse();\r
-           responseLogger.debug("Response was: " + response);\r
-           Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response);\r
-           pzresp.put(commandName, responseObject);        \r
-        }\r
-        if (commands.equals("record")) {\r
-          logger.debug("Record: Active clients: "+pzresp.getRecord().getActiveClients());\r
-          return pzresp.getRecord().getActiveClients();\r
+          for (CommandThread thread : threadList) {\r
+            thread.start();\r
+          }\r
+          for (CommandThread thread : threadList) {\r
+            try {\r
+              thread.join();\r
+            } catch (InterruptedException e) {\r
+              e.printStackTrace();\r
+            }\r
+          }\r
+          for (CommandThread thread : threadList) {\r
+             String commandName = thread.getCommand().getName();\r
+             String response = thread.getResponse();\r
+             responseLogger.debug("Response was: " + response);\r
+             Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response);\r
+             pzresp.put(commandName, responseObject);        \r
+          }\r
+          if (commands.equals("record")) {\r
+            logger.debug("Record: Active clients: "+pzresp.getRecord().getActiveClients());\r
+            return pzresp.getRecord().getActiveClients();\r
+          } else {\r
+            return pzresp.getActiveClients();\r
+          }  \r
         } else {\r
-          return pzresp.getActiveClients();\r
-        }  \r
+          logger.debug("Skipped requests for " + commands + " as there's not yet a query."); \r
+          pzresp.reset();\r
+          return "0";\r
+        }\r
       } else {\r
-        logger.debug("Skipped requests for " + commands + " as there's not yet a query."); \r
-        pzresp.reset();\r
+        logger.error("Did not attemt to run command(s) due to a validation error.");\r
         return "0";\r
       }\r
-    } else {\r
+    } else {      \r
       logger.error("Did not attempt to execute query since there are configuration errors.");\r
       return "0";\r
     }\r
     \r
   }\r
+  \r
+  public boolean commandsAreValid(String commands) {\r
+    if (commands.equals("record")) {\r
+      if (!pzreq.getCommandReadOnly("record").hasParameterSet("id")) {\r
+        logger.error("Attempt to send record command without the id parameter");\r
+        return false;\r
+      }\r
+    }\r
+    return true;\r
+  }\r
                                 \r
   public String toggleRecord (String recId) {\r
     if (hasRecord(recId)) {\r
@@ -237,7 +252,7 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable {
     if (stateMgr.hasPendingStateChange("record") && ! commands.equals("record")) {        \r
       logger.debug("Found pending record ID change. Doing record before updating " + commands);\r
       stateMgr.hasPendingStateChange("record",false);\r
-      if (pzreq.getCommandReadOnly("record").hasParameters()) {\r
+      if (pzreq.getCommandReadOnly("record").hasParameterSet("id")) {\r
         update("record");\r
       } else {         \r
         pzresp.put("record", new RecordResponse());\r
index 4795905..bac6787 100644 (file)
@@ -81,6 +81,11 @@ public class Pazpar2Commands implements Serializable {
     return (ShowCommand) (stateMgr.getCurrentState().getCommand(SHOW));\r
   }\r
   \r
+  /**\r
+   * Gets a detached (copied) record command from the current state\r
+   * \r
+   * @return\r
+   */\r
   public RecordCommand getRecord() {\r
     return (RecordCommand) (stateMgr.checkOut(RECORD));\r
   }\r
@@ -94,10 +99,19 @@ public class Pazpar2Commands implements Serializable {
     return (RecordCommand)stateMgr.getCurrentState().getCommand(RECORD);\r
   }\r
   \r
+  /**\r
+   * Gets a detached (copied) termlist command from the current state\r
+   * \r
+   * @return Mutable termlist command\r
+   */\r
   public TermlistCommand getTermlist() {\r
     return (TermlistCommand) (stateMgr.checkOut(TERMLIST));\r
   }\r
   \r
+  /**\r
+   * \r
+   * @return\r
+   */\r
   public BytargetCommand getBytarget() {\r
     return (BytargetCommand) (stateMgr.checkOut(BYTARGET));\r
   }\r
index 9850ef9..39ac006 100644 (file)
@@ -37,7 +37,7 @@ public class Pazpar2State {
     commands.put(Pazpar2Commands.RECORD,   new RecordCommand(mgr));\r
     commands.put(Pazpar2Commands.TERMLIST, new TermlistCommand(mgr));\r
     commands.put(Pazpar2Commands.BYTARGET, new BytargetCommand(mgr));    \r
-    key = "#initial";\r
+    key = "#1";\r
   }\r
     \r
   /**\r
@@ -63,14 +63,14 @@ public class Pazpar2State {
    */\r
   public String getKey() {\r
     if (key == null) {\r
-      StringBuilder querystatebuilder = new StringBuilder("#");\r
+      StringBuilder querystatebuilder = new StringBuilder("");\r
       for (Pazpar2Command command : commands.values()) {\r
         if (command.hasParameters()) {\r
           querystatebuilder.append("||"+command.getName()+"::");\r
           querystatebuilder.append(command.getValueWithExpressions());\r
         }      \r
       }            \r
-      key = querystatebuilder.toString();\r
+      key = "#"+querystatebuilder.toString().hashCode();\r
       return key;\r
     } else {      \r
       return key;\r
index 3c09920..23d59d1 100644 (file)
@@ -70,7 +70,7 @@ public class StateManager implements Serializable {
       states.put(state.getKey(), state);\r
       currentKey = state.getKey();\r
       hasPendingStateChange(command.getName(),new Boolean(true));      \r
-      logger.debug("Updating listeners with state change from " + command);\r
+      logger.debug("Updating " + listeners.size() + " listener(s) with state change from " + command);\r
       updateListeners(command.getName());      \r
     } else {\r
       logger.debug("Command " + command.getName() + " not found to change the state [" + command.getEncodedQueryString() + "]");\r
@@ -109,17 +109,21 @@ public class StateManager implements Serializable {
       logger.debug("setCurrentStateKey: no key change detected");\r
     } else {\r
       logger.debug("State key change. Was: [" + currentKey + "]. Will be ["+key+"]");\r
-      if (states.get(key).getCommand("search").equals(states.get(currentKey).getCommand("search"))) {\r
-        logger.debug("No search change detected");\r
+      if (states.get(key)==null) {\r
+        logger.error("The back-end received an unknow state key.");        \r
       } else {\r
-        hasPendingStateChange("search",true);\r
-      }\r
-      if (states.get(key).getCommand("record").equals(states.get(currentKey).getCommand("record"))) {\r
-        logger.debug("No record change detected");\r
-      } else {\r
-        hasPendingStateChange("record",true);\r
-      }\r
-      currentKey = key;\r
+        if (states.get(key).getCommand("search").equals(states.get(currentKey).getCommand("search"))) {\r
+          logger.debug("No search change detected");\r
+        } else {\r
+          hasPendingStateChange("search",true);\r
+        }\r
+        if (states.get(key).getCommand("record").equals(states.get(currentKey).getCommand("record"))) {\r
+          logger.debug("No record change detected");\r
+        } else {\r
+          hasPendingStateChange("record",true);\r
+        }\r
+        currentKey = key;\r
+      }      \r
     }\r
   }\r
 \r