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