From 0f71d8e1a79d3d007cc52b58bfe33517a69224b7 Mon Sep 17 00:00:00 2001 From: "Niels Erik G. Nielsen" Date: Wed, 10 Apr 2013 13:05:59 -0400 Subject: [PATCH] Refactors state management .. with the intend to decouple the complexities of setting up commands and parameters from the maintenance of state (before the state manager was involved in every setting of parameters, in order to detect and record the change of state. Now it just recieves a copy of the complete command after the fact, and doesn't need to know about command parameters. --- src/META-INF/resources/pz2utils/fieldlisteners.js | 2 +- .../pz2utils4jsf/pazpar2/CommandParameter.java | 1 - .../indexdata/pz2utils4jsf/pazpar2/Pz2Session.java | 54 +++++--- .../pz2utils4jsf/pazpar2/state/Pazpar2State.java | 73 +++++++++++ .../pz2utils4jsf/pazpar2/state/QueryState.java | 134 -------------------- .../pz2utils4jsf/pazpar2/state/QueryStates.java | 76 ----------- .../pz2utils4jsf/pazpar2/state/StateManager.java | 86 +++++++++++++ 7 files changed, 198 insertions(+), 228 deletions(-) create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/Pazpar2State.java delete mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryState.java delete mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryStates.java create mode 100644 src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/StateManager.java diff --git a/src/META-INF/resources/pz2utils/fieldlisteners.js b/src/META-INF/resources/pz2utils/fieldlisteners.js index d9cec5d..b0a3e6a 100644 --- a/src/META-INF/resources/pz2utils/fieldlisteners.js +++ b/src/META-INF/resources/pz2utils/fieldlisteners.js @@ -82,7 +82,7 @@ var stateKeyDoc = StringtoXML(field.textContent || field.text); var stateKeyValue = stateKeyDoc.childNodes[0].getAttribute("value"); //console.log('Application hash update detected. New value: ' + stateKeyValue); - if (stateKeyValue != window.location.hash) { + if (stateKeyValue !== window.location.hash) { window.location.hash = stateKeyValue; //console.log("Browsers hash updated accordingly."); } else { diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandParameter.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandParameter.java index d589160..212175b 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandParameter.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandParameter.java @@ -9,7 +9,6 @@ import java.util.Map; import org.apache.log4j.Logger; import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter; -import com.indexdata.pz2utils4jsf.pazpar2.Expression; public class CommandParameter implements Serializable { diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java index 66063c3..4d8f8c2 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -27,7 +27,7 @@ import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.StatResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.TermListsResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.TermResponse; -import com.indexdata.pz2utils4jsf.pazpar2.state.QueryStates; +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; import com.indexdata.pz2utils4jsf.utils.Utils; @Named @SessionScoped @@ -37,7 +37,7 @@ public class Pz2Session implements Pz2Interface { private static Logger logger = Logger.getLogger(Pz2Session.class); protected Map dataObjects = new ConcurrentHashMap(); - protected QueryStates queryStates = new QueryStates(); + protected StateManager stateManager = new StateManager(); protected ErrorHelper errorHelper = null; protected List configurationErrors = null; @@ -76,7 +76,7 @@ public class Pz2Session implements Pz2Interface { } public void doSearch() { - queryStates.hasPendingStateChange("search",false); + stateManager.hasPendingStateChange("search",false); resetDataObjects(); removeCommand("record"); setCommandParameter("show",new CommandParameter("start","=",0)); @@ -155,8 +155,10 @@ public class Pz2Session implements Pz2Interface { } public void setFacet (String facetKey, String term) { - if (term != null && term.length()>0) { - queryStates.getCurrentState().setCommandParameterExpression("search","query",new Expression(facetKey,"=",term),queryStates); + if (term != null && term.length()>0) { + Pazpar2Command command = getCommand("search"); + command.getParameter("query").addExpression(new Expression(facetKey,"=",term)); + stateManager.checkIn(command); doSearch(); } } @@ -170,7 +172,9 @@ public class Pz2Session implements Pz2Interface { } public void removeFacet(String facetKey, String term) { - queryStates.getCurrentState().removeCommandParameterExpression("search","query",new Expression(facetKey,"=",term),queryStates); + Pazpar2Command command = getCommand("search"); + command.getParameter("query").removeExpression(new Expression(facetKey,"=",term)); + stateManager.checkIn(command); doSearch(); } @@ -290,11 +294,11 @@ public class Pz2Session implements Pz2Interface { public String getCurrentStateKey () { - return queryStates.getCurrentStateKey(); + return stateManager.getCurrentState().getKey(); } public void setCurrentStateKey(String key) { - queryStates.setCurrentStateKey(key); + stateManager.setCurrentStateKey(key); } public boolean hasConfigurationErrors () { @@ -379,13 +383,13 @@ public class Pz2Session implements Pz2Interface { } protected void handleQueryStateChanges (String commands) { - if (queryStates.hasPendingStateChange("search") && hasQuery()) { + if (stateManager.hasPendingStateChange("search") && hasQuery()) { logger.debug("Found pending search change. Doing search before updating " + commands); doSearch(); } - if (queryStates.hasPendingStateChange("record") && ! commands.equals("record")) { + if (stateManager.hasPendingStateChange("record") && ! commands.equals("record")) { logger.debug("Found pending record ID change. Doing record before updating " + commands); - queryStates.hasPendingStateChange("record",false); + stateManager.hasPendingStateChange("record",false); if (getCommand("record").hasParameters()) { update("record"); } else { @@ -404,22 +408,40 @@ public class Pz2Session implements Pz2Interface { } } + /** + * Returns a Pazpar2 command 'detached' from the current Pazpar2 state. + * + * 'Detached' is meant to imply that this is a copy of a command in the + * current state, detached so as to NOT change the current state if + * modified. It can be viewed and executed, however. + * + * In order to modify the command with effect for subsequent searches, + * it must be checked back into the StateManager, which will + * then create a new current Pazpar2 state as needed. + * + * @param name + * @return + */ protected Pazpar2Command getCommand(String name) { - return queryStates.getCurrentState().getCommand(name); + return stateManager.checkOut(name); } protected void setCommandParameter(String commandName, CommandParameter parameter) { logger.debug("Setting parameter for " + commandName + ": " + parameter); - queryStates.getCurrentState().setCommandParameter(commandName, parameter, queryStates); + Pazpar2Command command = getCommand(commandName); + command.setParameter(parameter); + stateManager.checkIn(command); } protected void removeCommandParameter(String commandName, String parameterName) { - queryStates.getCurrentState().removeCommandParameter(commandName,parameterName,queryStates); + Pazpar2Command command = getCommand(commandName); + command.removeParameter(parameterName); + stateManager.checkIn(command); } protected void removeCommand (String commandName) { - queryStates.getCurrentState().removeCommand(commandName, queryStates); + stateManager.checkIn(new Pazpar2Command(commandName)); } protected String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { @@ -472,7 +494,7 @@ public class Pz2Session implements Pz2Interface { dataObjects.put("record", new RecordResponse()); dataObjects.put("search", new SearchResponse()); } - + @Override public void setFilter(String filterExpression) { logger.debug("Setting filter to " + filterExpression); diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/Pazpar2State.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/Pazpar2State.java new file mode 100644 index 0000000..fba685e --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/Pazpar2State.java @@ -0,0 +1,73 @@ +package com.indexdata.pz2utils4jsf.pazpar2.state; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; + + +public class Pazpar2State { + + String key = null; + Map commands = new HashMap();; + + public Pazpar2State () { + for (String command : Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget")) { + commands.put(command, new Pazpar2Command(command)); + } + key = "#initial"; + } + + public Pazpar2State (Pazpar2State previousState, Pazpar2Command newCommand) { + for (String commandName : previousState.commands.keySet()) { + this.commands.put(commandName, previousState.commands.get(commandName).copy()); + } + this.commands.put(newCommand.getName(),newCommand); + this.key = getKey(); + } + + /** + * Generates a state key that can be used by the browser to pick + * up this state again at a later point in time. + * + * @return + */ + public String getKey() { + if (key == null) { + StringBuilder querystatebuilder = new StringBuilder("#"); + for (Pazpar2Command command : commands.values()) { + if (command.hasParameters()) { + querystatebuilder.append("||"+command.getName()+"::"); + querystatebuilder.append(command.getValueWithExpressions()); + } + } + key = querystatebuilder.toString(); + return key; + } else { + return key; + } + } + + /** + * Checks if the provided command represents a state change + * + * @param command + * @return true if the command causes a change of state + */ + public boolean stateMutating (Pazpar2Command command) { + if (command == null) { + return true; + } else if (commands.get(command.getName()) == null) { + return true; + } else if ((command.equals(commands.get(command.getName())))) { + return false; + } else { + return true; + } + } + + public Pazpar2Command getCommand(String name) { + return commands.get(name); + } +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryState.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryState.java deleted file mode 100644 index 89c8888..0000000 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryState.java +++ /dev/null @@ -1,134 +0,0 @@ -package com.indexdata.pz2utils4jsf.pazpar2.state; - -import java.io.Serializable; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -import org.apache.log4j.Logger; - -import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter; -import com.indexdata.pz2utils4jsf.pazpar2.Expression; -import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; -import com.indexdata.pz2utils4jsf.pazpar2.state.QueryState; -import com.indexdata.pz2utils4jsf.pazpar2.state.QueryStates; - -public class QueryState implements Serializable { - - private static final long serialVersionUID = -1465676392610954168L; - private static Logger logger = Logger.getLogger(QueryState.class); - private Map pz2commands = new HashMap(); - private String key = null; - - public QueryState () { - for (String command : Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget")) { - pz2commands.put(command, new Pazpar2Command(command)); - } - } - - private QueryState copy() { - QueryState newState = new QueryState(); - for (String commandName : pz2commands.keySet()) { - newState.setCommand(pz2commands.get(commandName).copy()); - } - return newState; - } - - public void setCommandParameter(String commandName, CommandParameter parameter, QueryStates queryStates) { - CommandParameter current = getCommand(commandName).getParameter(parameter.getName()); - if (current != null && current.equals(parameter)) { - logger.debug("Recieved parameter but already have " + parameter.getValueWithExpressions() + " in this state. No state change."); - } else { - logger.debug("New command parameter received: " + parameter.getValueWithExpressions() + ". Initiating new state."); - QueryState newState = this.copy(); - newState._setCommandParameter(commandName,parameter); - logger.debug("Old state: " + this); - logger.debug("New state: " + newState); - queryStates.setCurrentState(newState); - } - } - - public void removeCommandParameter(String commandName, String parameterName, QueryStates queryStates) { - QueryState newState = this.copy(); - newState._removeCommandParameter(commandName, parameterName); - queryStates.setCurrentState(newState); - } - - public void setCommandParameterExpression (String commandName, String parameterName, Expression expression, QueryStates queryStates) { - QueryState newState = this.copy(); - newState._setCommandParameterExpression(commandName, parameterName, expression); - queryStates.setCurrentState(newState); - } - - public void _setCommandParameterExpression (String commandName, String parameterName, Expression expression) { - getCommand(commandName).getParameter(parameterName).addExpression(expression); - } - - public void removeCommandParameterExpression (String commandName, String parameterName, Expression expression, QueryStates queryStates) { - QueryState newState = this.copy(); - newState._removeCommandParameterExpression(commandName, parameterName, expression); - queryStates.setCurrentState(newState); - } - - public void _removeCommandParameterExpression (String commandName, String parameterName, Expression expression) { - getCommand(commandName).getParameter(parameterName).removeExpression(expression); - - } - - public void _setCommandParameter(String commandName, CommandParameter parameter) { - getCommand(commandName).setParameter(parameter); - } - - public void _removeCommandParameter(String commandName, String parameterName) { - getCommand(commandName).removeParameter(parameterName); - } - - public void _removeCommand(String commandName) { - getCommand(commandName).removeParameters(); - } - - - public void removeCommand (String commandName, QueryStates queryStates) { - QueryState newState = this.copy(); - newState._removeCommand(commandName); - queryStates.setCurrentState(newState); - - } - - public Pazpar2Command getCommand(String name) { - return pz2commands.get(name); - } - - private void setCommand(Pazpar2Command command) { - pz2commands.put(command.getName(), command); - } - - public String getKey() { - if (key == null) { - StringBuilder querystatebuilder = new StringBuilder("#"); - for (Pazpar2Command command : pz2commands.values()) { - if (command.hasParameters()) { - querystatebuilder.append("||"+command.getName()+"::"); - querystatebuilder.append(command.getValueWithExpressions()); - } - } - key = querystatebuilder.toString(); - return key; - } else { - return key; - } - } - - public String toString () { - return pz2commands.toString(); - } - - public boolean equals (Object otherQueryState) { - if (otherQueryState instanceof QueryState) { - return this.toString().equals(otherQueryState.toString()); - } else { - return false; - } - } - -} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryStates.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryStates.java deleted file mode 100644 index ea1264d..0000000 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/QueryStates.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.indexdata.pz2utils4jsf.pazpar2.state; - -import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; - -import org.apache.log4j.Logger; - -import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; -import com.indexdata.pz2utils4jsf.pazpar2.state.QueryState; -import com.indexdata.pz2utils4jsf.pazpar2.state.QueryStates; - -public class QueryStates implements Serializable { - - private static final long serialVersionUID = 6131720167974584659L; - private static Logger logger = Logger.getLogger(QueryStates.class); - - Map queryStates = new HashMap(); - String currentStateKey = ""; - Map pendingStateChanges = new HashMap(); - - public QueryStates () { - queryStates.put("#initial", new QueryState()); - currentStateKey = "#initial"; - for (String command : Pazpar2Command.allCommands) { - pendingStateChanges.put(command, new Boolean(false)); - } - } - - public String getCurrentStateKey() { - return currentStateKey; - } - - public void setCurrentStateKey(String key) { - - if (currentStateKey.equals(key)) { - logger.debug("setCurrentStateKey: no key change detected"); - } else { - logger.debug("State key change. Was: [" + currentStateKey + "]. Will be ["+key+"]"); - if (queryStates.get(key).getCommand("search").equals(getCurrentState().getCommand("search"))) { - logger.debug("No search change detected"); - } else { - hasPendingStateChange("search",true); - } - if (queryStates.get(key).getCommand("record").equals(getCurrentState().getCommand("record"))) { - logger.debug("No record change detected"); - } else { - hasPendingStateChange("record",true); - } - currentStateKey = key; - } - } - - public QueryState getCurrentState() { - if (queryStates.get(currentStateKey) == null) { - return new QueryState(); - } else { - return queryStates.get(currentStateKey); - } - } - - public void setCurrentState(QueryState queryState) { - logger.debug("Setting current state: " + queryState.getKey()); - queryStates.put(queryState.getKey(), queryState); - setCurrentStateKey(queryState.getKey()); - } - - public void hasPendingStateChange(String command, boolean bool) { - pendingStateChanges.put(command, new Boolean(bool)); - } - - public boolean hasPendingStateChange (String command) { - return pendingStateChanges.get(command).booleanValue(); - } - -} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/StateManager.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/StateManager.java new file mode 100644 index 0000000..638effd --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/StateManager.java @@ -0,0 +1,86 @@ +package com.indexdata.pz2utils4jsf.pazpar2.state; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; + +public class StateManager { + + Map states = new HashMap(); + String currentKey = ""; + Map pendingStateChanges = new HashMap(); + private static Logger logger = Logger.getLogger(StateManager.class); + + public StateManager () { + Pazpar2State initialState = new Pazpar2State(); + states.put(initialState.getKey(), initialState); + currentKey = initialState.getKey(); + for (String command : Pazpar2Command.allCommands) { + pendingStateChanges.put(command, new Boolean(false)); + } + + } + + /** + * Registers a Pazpar2 command for execution. + * + * The state manager will update current state and flag that + * a request change was made but that it was not yet carried + * out against Pazpar2. + * + * Any command that is created or modified must be checked in + * like this to come into effect. + * + * @param command + */ + public void checkIn(Pazpar2Command command) { + if (getCurrentState().stateMutating(command)) { + Pazpar2State state = new Pazpar2State(getCurrentState(),command); + states.put(state.getKey(), state); + currentKey = state.getKey(); + hasPendingStateChange(command.getName(),new Boolean(true)); + } else { + logger.debug("Command " + command.getName() + " not found to change the state [" + command.getEncodedQueryString() + "]"); + } + } + + public Pazpar2Command checkOut (String commandName) { + return getCurrentState().getCommand(commandName).copy(); + } + + public Pazpar2State getCurrentState () { + return states.get(currentKey); + } + + public void setCurrentStateKey(String key) { + if (currentKey.equals(key)) { + 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"); + } 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; + } + } + + + public void hasPendingStateChange(String command, boolean bool) { + pendingStateChanges.put(command, new Boolean(bool)); + } + + public boolean hasPendingStateChange (String command) { + return pendingStateChanges.get(command).booleanValue(); + } + +} -- 1.7.10.4