From: Niels Erik G. Nielsen Date: Sat, 13 Apr 2013 05:30:35 +0000 (-0400) Subject: More work on commands, statemgmt, EL references X-Git-Tag: v0.0.7~176 X-Git-Url: http://jsfdemo.indexdata.com/cgi-bin?a=commitdiff_plain;h=977e4983e28683dd8a7d9fd19b0daee3ac349a3a;p=mkjsf-moved-to-github.git More work on commands, statemgmt, EL references Attempts to make state management still more transparent to the logic of setting command parameters - basically in order to allow command parameter settings to get arbitrarily complex without having to worry more than necessary about state handling Extracts commands to a separate class for EL access (from pages) - Can thus be referenced like, say, req.search.query for setting the query parameter of the search command. Attempts to reduce the number of methods on the pz2 object, which was gradually becoming less manageable. Creates a dedicated class for each pazpar2 command for type-safe setting of parameters and to manage complexity. Fixes missing faces-config.xml (might have caused the CDI framework to NOT scan the pz2utils4jsf jar for all annotations). Uses listener pattern to alert the session object about state changes. --- diff --git a/pom.xml b/pom.xml index 2df0cf2..dfb83d6 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ org.glassfish javax.faces - 2.1.16 + 2.1.21 javax.servlet.jsp.jstl diff --git a/src/META-INF/faces-config.xml b/src/META-INF/faces-config.xml new file mode 100644 index 0000000..5012d41 --- /dev/null +++ b/src/META-INF/faces-config.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandParameter.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandParameter.java deleted file mode 100644 index 212175b..0000000 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandParameter.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.indexdata.pz2utils4jsf.pazpar2; - -import java.io.Serializable; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.HashMap; -import java.util.Map; - -import org.apache.log4j.Logger; - -import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter; - -public class CommandParameter implements Serializable { - - private static Logger logger = Logger.getLogger(CommandParameter.class); - - private static final long serialVersionUID = 625502285668766749L; - String name = null; - String operator = null; - String value = null; - Map expressions = new HashMap(); - - public CommandParameter (String name) { - logger.debug("Instantiating command parameter (1) " + name); - this.name = name; - } - - public CommandParameter (String name, String operator, String value, Expression... expressions) { - logger.debug("Instantiating command parameter (2) " + name + " with " + expressions); - this.name = name; - this.operator = operator; - this.value = value; - for (Expression expr : expressions) { - this.expressions.put(expr.toString(), expr); - } - } - - public CommandParameter (String name, String operator, String value) { - logger.debug("Instantiating command parameter (3) " + name + " ("+this+") with " + name + " and " + value); - this.name = name; - this.operator = operator; - this.value = value; - } - - public CommandParameter (String name, String operator, int value) { - logger.debug("Instantiating command parameter (4) " + name + " ("+this+") with " + name + " and " + value); - this.name = name; - this.operator = operator; - this.value = value+""; - } - - - public String getName () { - return name; - } - - public Map getExpressions () { - return expressions; - } - - public void addExpression(Expression expression) { - logger.debug("Adding expression [" + expression + "] to " + this.getName() + " ("+this+")"); - this.expressions.put(expression.toString(),expression); - } - - public void removeExpression(Expression expression) { - this.expressions.remove(expression.toString()); - } - - - public boolean hasOperator() { - return operator != null; - } - - public String getEncodedQueryString () { - try { - return name + operator + URLEncoder.encode(getValueWithExpressions(),"UTF-8"); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - return null; - } - } - - public String getSimpleValue() { - return value; - } - - public String getValueWithExpressions () { - StringBuilder completeValue = new StringBuilder((value==null ? "" : value)); - for (String key : expressions.keySet()) { - completeValue.append(" and " + expressions.get(key)); - } - return completeValue.toString(); - - } - - @Override - public boolean equals (Object otherParameter) { - return - ((otherParameter instanceof CommandParameter) - && this.getValueWithExpressions().equals(((CommandParameter) otherParameter).getValueWithExpressions())); - } - - @Override - public int hashCode () { - return getValueWithExpressions().hashCode(); - } - - public String toString() { - return getValueWithExpressions(); - } - - public CommandParameter copy() { - CommandParameter newParam = new CommandParameter(name); - newParam.value = this.value; - newParam.operator = this.operator; - for (String key : expressions.keySet()) { - newParam.addExpression(expressions.get(key).copy()); - } - return newParam; - } - -} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java index 01b999c..323ef78 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/CommandThread.java @@ -6,6 +6,7 @@ import java.io.IOException; import org.apache.log4j.Logger; import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command; import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError; public class CommandThread extends Thread { diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ForServiceProxy.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ForServiceProxy.java new file mode 100644 index 0000000..786bd20 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ForServiceProxy.java @@ -0,0 +1,15 @@ +package com.indexdata.pz2utils4jsf.pazpar2; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) +public @interface ForServiceProxy { + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ForStraightPz2.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ForStraightPz2.java new file mode 100644 index 0000000..4132c3b --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/ForStraightPz2.java @@ -0,0 +1,15 @@ +package com.indexdata.pz2utils4jsf.pazpar2; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) +public @interface ForStraightPz2 { + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pazpar2Command.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pazpar2Command.java deleted file mode 100644 index 9d30f64..0000000 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pazpar2Command.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.indexdata.pz2utils4jsf.pazpar2; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.log4j.Logger; - -import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter; -import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; - -public class Pazpar2Command implements Serializable { - - private static Logger logger = Logger.getLogger(Pazpar2Command.class); - private static final long serialVersionUID = -6825491856480675917L; - public static List allCommands = new ArrayList(Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget")); - - private String name = ""; - private Map parameters = new HashMap(); - - public Pazpar2Command (String name) { - this.name = name; - } - - public Pazpar2Command copy () { - Pazpar2Command newCommand = new Pazpar2Command(name); - for (String parameterName : parameters.keySet()) { - newCommand.setParameter(parameters.get(parameterName).copy()); - } - return newCommand; - } - - public String getName() { - return name; - } - - public void setParameter (CommandParameter parameter) { - logger.debug("Setting parameter " + parameter.getName() + "=" + parameter.getValueWithExpressions() + " to " + this.getName()); - parameters.put(parameter.getName(),parameter); - } - - public CommandParameter getParameter (String name) { - return parameters.get(name); - } - - public void removeParameter (String name) { - parameters.remove(name); - } - - public void removeParameters() { - parameters = new HashMap(); - } - - public boolean hasParameters () { - return (parameters.keySet().size()>0); - } - - public String getEncodedQueryString () { - StringBuilder queryString = new StringBuilder("command="+name); - for (CommandParameter parameter : parameters.values()) { - queryString.append("&"+parameter.getEncodedQueryString()); - } - return queryString.toString(); - } - - public String getValueWithExpressions() { - StringBuilder value = new StringBuilder(""); - for (CommandParameter parameter : parameters.values()) { - value.append("&" + parameter.getName() + parameter.operator + parameter.getValueWithExpressions()); - } - return value.toString(); - } - - @Override - public boolean equals (Object otherCommand) { - return - ((otherCommand instanceof Pazpar2Command) - && this.getValueWithExpressions().equals(((Pazpar2Command) otherCommand).getValueWithExpressions())); - } - - @Override - public int hashCode () { - return getValueWithExpressions().hashCode(); - } - - public String toString () { - return parameters.toString(); - } - -} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java index 70dde92..345f049 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java @@ -14,6 +14,7 @@ import org.apache.log4j.Logger; import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.controls.ResultsPager; import com.indexdata.pz2utils4jsf.errors.ErrorInterface; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command; import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; @@ -29,24 +30,28 @@ public class Pz2Bean implements Pz2Interface, Serializable { private static Logger logger = Logger.getLogger(Pz2Bean.class); @Inject ConfigurationReader configurator; - protected Pz2Session pz2; + + @Inject @ForStraightPz2 Pz2Session pz2; + protected SearchClient searchClient; public Pz2Bean () { logger.info("Instantiating pz2 bean [" + Utils.objectId(this) + "]"); + logger.debug("in Pz2Bean configurator is " + configurator); } @PostConstruct - public void instantiatePz2SessionObject() { + public void instantiatePz2SessionObject() { + logger.debug("in start of Pz2Bean post-construct configurator is " + configurator); logger.debug(Utils.objectId(this) + " will instantiate a Pz2Session next."); - pz2 = new Pz2Session(); searchClient = new Pz2Client(); logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" + Utils.objectId(configurator) + "] on session [" + Utils.objectId(pz2) + "]" ); - pz2.configureClient(searchClient,configurator); - } - + pz2.configureClient(searchClient,configurator); + logger.debug("in end of Pz2Bean post-construct configurator is " + configurator); + + } /* (non-Javadoc) * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#doSearch(java.lang.String) @@ -81,9 +86,9 @@ public class Pz2Bean implements Pz2Interface, Serializable { * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setQuery(java.lang.String) */ public void setQuery(String query) { - pz2.setQuery(query); + pz2.req.getSearch().setQuery(query); } - + /* (non-Javadoc) * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getQuery() */ @@ -321,5 +326,9 @@ public class Pz2Bean implements Pz2Interface, Serializable { public String getRecordId() { return pz2.getRecordId(); } + + public Pazpar2Command getSearchCommand () { + return pz2.getCommand("search"); + } } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Client.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Client.java index 26e4a4d..affd08d 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Client.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Client.java @@ -26,6 +26,7 @@ import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException; import com.indexdata.pz2utils4jsf.config.Configuration; import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.errors.ConfigurationException; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command; import com.indexdata.pz2utils4jsf.utils.Utils; @Named @SessionScoped diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2ProxyBean.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2ProxyBean.java index 6982f9c..f5f4e55 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2ProxyBean.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2ProxyBean.java @@ -28,13 +28,14 @@ public class Pz2ProxyBean extends Pz2Bean implements ServiceProxyInterface { @Inject ConfigurationReader configurator; @Inject ServiceProxyUser user; + @Inject @ForServiceProxy ServiceProxySession pz2; + public Pz2ProxyBean() { } @PostConstruct public void instantiatePz2SessionObject() { - logger.debug(Utils.objectId(this) + " will instantiate a Pz2Session object next."); - pz2 = new ServiceProxySession(); + logger.debug(Utils.objectId(this) + " will instantiate a Pz2Session object next."); searchClient = new ServiceProxyClient(); logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" + Utils.objectId(configurator) + "] on session [" diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java index 48aa236..cdb1a6b 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -6,8 +6,9 @@ import java.util.Map; import java.util.StringTokenizer; import java.util.concurrent.ConcurrentHashMap; +import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; -import javax.inject.Named; +import javax.inject.Inject; import org.apache.log4j.Logger; @@ -17,6 +18,9 @@ import com.indexdata.pz2utils4jsf.errors.ConfigurationError; import com.indexdata.pz2utils4jsf.errors.ConfigurationException; import com.indexdata.pz2utils4jsf.errors.ErrorHelper; import com.indexdata.pz2utils4jsf.errors.ErrorInterface; +import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandParameter; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Commands; import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError; import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; @@ -27,17 +31,21 @@ 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.StateListener; import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; import com.indexdata.pz2utils4jsf.utils.Utils; -@Named @SessionScoped -public class Pz2Session implements Pz2Interface { +@ForStraightPz2 +public class Pz2Session implements Pz2Interface, StateListener { private static final long serialVersionUID = 3947514708343320514L; private static Logger logger = Logger.getLogger(Pz2Session.class); protected Map dataObjects = new ConcurrentHashMap(); - protected StateManager stateManager = new StateManager(); + + @Inject StateManager stateMgr; + @Inject Pazpar2Commands req; + protected ErrorHelper errorHelper = null; protected List configurationErrors = null; @@ -46,7 +54,14 @@ public class Pz2Session implements Pz2Interface { protected ResultsPager pager = null; public Pz2Session () { - logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); + logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); + } + + @PostConstruct + public void listenToStateManager() { + logger.debug("in post-construct of Pz2Session stateMgr is " + stateMgr); + logger.debug("in post-construct req is " + req); + stateMgr.addStateListener(this); } public void configureClient(SearchClient searchClient, ConfigurationReader configReader) { @@ -76,7 +91,7 @@ public class Pz2Session implements Pz2Interface { } public void doSearch() { - stateManager.hasPendingStateChange("search",false); + stateMgr.hasPendingStateChange("search",false); resetDataObjects(); removeCommand("record"); setCommandParameter("show",new CommandParameter("start","=",0)); @@ -158,7 +173,7 @@ public class Pz2Session implements Pz2Interface { if (term != null && term.length()>0) { Pazpar2Command command = getCommand("search"); command.getParameter("query").addExpression(new Expression(facetKey,"=",term)); - stateManager.checkIn(command); + stateMgr.checkIn(command); doSearch(); } } @@ -174,7 +189,7 @@ public class Pz2Session implements Pz2Interface { public void removeFacet(String facetKey, String term) { Pazpar2Command command = getCommand("search"); command.getParameter("query").removeExpression(new Expression(facetKey,"=",term)); - stateManager.checkIn(command); + stateMgr.checkIn(command); doSearch(); } @@ -294,11 +309,11 @@ public class Pz2Session implements Pz2Interface { public String getCurrentStateKey () { - return stateManager.getCurrentState().getKey(); + return stateMgr.getCurrentState().getKey(); } public void setCurrentStateKey(String key) { - stateManager.setCurrentStateKey(key); + stateMgr.setCurrentStateKey(key); } public boolean hasConfigurationErrors () { @@ -357,7 +372,8 @@ public class Pz2Session implements Pz2Interface { } protected boolean hasQuery() { - return getCommand("search").getParameter("query") != null && getCommand("search").getParameter("query").getValueWithExpressions().length()>0; + logger.debug("req is " + req); + return req.getSearch().getParameter("query") != null && req.getSearch().getParameter("query").getValueWithExpressions().length()>0; } public boolean hasRecords () { @@ -383,20 +399,20 @@ public class Pz2Session implements Pz2Interface { } protected void handleQueryStateChanges (String commands) { - if (stateManager.hasPendingStateChange("search") && hasQuery()) { + if (stateMgr.hasPendingStateChange("search") && hasQuery()) { logger.debug("Found pending search change. Doing search before updating " + commands); doSearch(); } - if (stateManager.hasPendingStateChange("record") && ! commands.equals("record")) { + if (stateMgr.hasPendingStateChange("record") && ! commands.equals("record")) { logger.debug("Found pending record ID change. Doing record before updating " + commands); - stateManager.hasPendingStateChange("record",false); + stateMgr.hasPendingStateChange("record",false); if (getCommand("record").hasParameters()) { update("record"); } else { removeCommand("record"); dataObjects.put("record", new RecordResponse()); } - } + } } protected String getActiveClients() { @@ -423,27 +439,27 @@ public class Pz2Session implements Pz2Interface { * @return */ protected Pazpar2Command getCommand(String name) { - return stateManager.checkOut(name); + return stateMgr.checkOut(name); } protected void setCommandParameter(String commandName, CommandParameter parameter) { logger.debug("Setting parameter for " + commandName + ": " + parameter); Pazpar2Command command = getCommand(commandName); command.setParameter(parameter); - stateManager.checkIn(command); + stateMgr.checkIn(command); } protected void removeCommandParameter(String commandName, String parameterName) { Pazpar2Command command = getCommand(commandName); command.removeParameter(parameterName); - stateManager.checkIn(command); + stateMgr.checkIn(command); } protected void removeCommand (String commandName) { Pazpar2Command command = getCommand(commandName); command.removeParameters(); - stateManager.checkIn(command); + stateMgr.checkIn(command); } protected String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { @@ -481,7 +497,7 @@ public class Pz2Session implements Pz2Interface { } protected String doCommand(String commandName) { - Pazpar2Command command = getCommand(commandName); + Pazpar2Command command = req.getCommand(commandName); logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString()); return update(commandName); } @@ -510,5 +526,14 @@ public class Pz2Session implements Pz2Interface { public boolean hasFilter () { return getFilter().length()>0; } + + @Override + public void stateUpdate(String commandName) { + logger.debug("State change reported for [" + commandName + "]"); + if (commandName.equals("show")) { + logger.debug("Updating show"); + update(commandName); + } + } } diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java index b0d188f..c7d0cf9 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/SearchClient.java @@ -7,6 +7,7 @@ import java.io.Serializable; import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; import com.indexdata.pz2utils4jsf.config.Configurable; import com.indexdata.pz2utils4jsf.config.Configuration; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command; public interface SearchClient extends Configurable, Serializable { diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/BytargetCommand.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/BytargetCommand.java new file mode 100644 index 0000000..720f0a6 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/BytargetCommand.java @@ -0,0 +1,13 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; + +public class BytargetCommand extends Pazpar2Command { + + private static final long serialVersionUID = 9070458716105294392L; + + public BytargetCommand(StateManager stateMgr) { + super("bytarget",stateMgr); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/CommandParameter.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/CommandParameter.java new file mode 100644 index 0000000..c899251 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/CommandParameter.java @@ -0,0 +1,124 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.Map; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.pazpar2.Expression; +import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandParameter; + +public class CommandParameter implements Serializable { + + private static Logger logger = Logger.getLogger(CommandParameter.class); + + private static final long serialVersionUID = 625502285668766749L; + String name = null; + String operator = null; + String value = null; + Map expressions = new HashMap(); + + public CommandParameter (String name) { + logger.debug("Instantiating command parameter (1) " + name); + this.name = name; + } + + public CommandParameter (String name, String operator, String value, Expression... expressions) { + logger.debug("Instantiating command parameter (2) " + name + " with " + expressions); + this.name = name; + this.operator = operator; + this.value = value; + for (Expression expr : expressions) { + this.expressions.put(expr.toString(), expr); + } + } + + public CommandParameter (String name, String operator, String value) { + logger.debug("Instantiating command parameter (3) " + name + " ("+this+") with " + value); + this.name = name; + this.operator = operator; + this.value = value; + } + + public CommandParameter (String name, String operator, int value) { + logger.debug("Instantiating command parameter (4) " + name + " ("+this+") with " + value); + this.name = name; + this.operator = operator; + this.value = value+""; + } + + + public String getName () { + return name; + } + + public Map getExpressions () { + return expressions; + } + + public void addExpression(Expression expression) { + logger.debug("Adding expression [" + expression + "] to " + this.getName() + " ("+this+")"); + this.expressions.put(expression.toString(),expression); + } + + public void removeExpression(Expression expression) { + this.expressions.remove(expression.toString()); + } + + + public boolean hasOperator() { + return operator != null; + } + + public String getEncodedQueryString () { + try { + return name + operator + URLEncoder.encode(getValueWithExpressions(),"UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return null; + } + } + + public String getSimpleValue() { + return value; + } + + public String getValueWithExpressions () { + StringBuilder completeValue = new StringBuilder((value==null ? "" : value)); + for (String key : expressions.keySet()) { + completeValue.append(" and " + expressions.get(key)); + } + return completeValue.toString(); + + } + + @Override + public boolean equals (Object otherParameter) { + return + ((otherParameter instanceof CommandParameter) + && this.getValueWithExpressions().equals(((CommandParameter) otherParameter).getValueWithExpressions())); + } + + @Override + public int hashCode () { + return getValueWithExpressions().hashCode(); + } + + public String toString() { + return getValueWithExpressions(); + } + + public CommandParameter copy() { + CommandParameter newParam = new CommandParameter(name); + newParam.value = this.value; + newParam.operator = this.operator; + for (String key : expressions.keySet()) { + newParam.addExpression(expressions.get(key).copy()); + } + return newParam; + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/InitCommand.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/InitCommand.java new file mode 100644 index 0000000..37fd8f1 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/InitCommand.java @@ -0,0 +1,19 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; + +public class InitCommand extends Pazpar2Command { + + private static final long serialVersionUID = -4915976465898889987L; + + public InitCommand(StateManager stateMgr) { + super("init",stateMgr); + } + + public void setClear(String clear) { + } + + public void setService(String serviceId) { + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/Pazpar2Command.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/Pazpar2Command.java new file mode 100644 index 0000000..a421ff8 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/Pazpar2Command.java @@ -0,0 +1,112 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; + +public class Pazpar2Command implements Serializable { + + private static Logger logger = Logger.getLogger(Pazpar2Command.class); + private static final long serialVersionUID = -6825491856480675917L; + private String name = ""; + protected Map parameters = new HashMap(); + + StateManager stateMgr; + + public Pazpar2Command (String name, StateManager stateMgr) { + this.name = name; + this.stateMgr = stateMgr; + } + + public Pazpar2Command copy () { + Pazpar2Command newCommand = new Pazpar2Command(name,stateMgr); + for (String parameterName : parameters.keySet()) { + newCommand.setParameterSilently(parameters.get(parameterName).copy()); + } + return newCommand; + } + + public String getName() { + return name; + } + + public void setParameter (CommandParameter parameter) { + logger.debug("Setting parameter " + parameter.getName() + "=" + parameter.getValueWithExpressions() + " to " + this.getName()); + parameters.put(parameter.getName(),parameter); + stateMgr.checkIn(this); + } + + public void setParameters (CommandParameter... params) { + for (CommandParameter param : params) { + logger.debug("Setting parameter " + param.getName() + "=" + param.getValueWithExpressions() + " to " + this.getName()); + parameters.put(param.getName(),param); + } + stateMgr.checkIn(this); + } + + + public void setParameterSilently (CommandParameter parameter) { + logger.debug("Setting parameter silently " + parameter.getName() + "=" + parameter.getValueWithExpressions() + " to " + this.getName()); + parameters.put(parameter.getName(),parameter); + } + + + public CommandParameter getParameter (String name) { + return parameters.get(name); + } + + public void removeParameter (String name) { + parameters.remove(name); + stateMgr.checkIn(this); + } + + public void removeParameters() { + parameters = new HashMap(); + stateMgr.checkIn(this); + } + + public boolean hasParameters () { + return (parameters.keySet().size()>0); + } + + public boolean hasParameterSet(String parameterName) { + return (parameters.get(parameterName) != null); + } + + public String getEncodedQueryString () { + StringBuilder queryString = new StringBuilder("command="+name); + for (CommandParameter parameter : parameters.values()) { + queryString.append("&"+parameter.getEncodedQueryString()); + } + return queryString.toString(); + } + + public String getValueWithExpressions() { + StringBuilder value = new StringBuilder(""); + for (CommandParameter parameter : parameters.values()) { + value.append("&" + parameter.getName() + parameter.operator + parameter.getValueWithExpressions()); + } + return value.toString(); + } + + @Override + public boolean equals (Object otherCommand) { + return + ((otherCommand instanceof Pazpar2Command) + && this.getValueWithExpressions().equals(((Pazpar2Command) otherCommand).getValueWithExpressions())); + } + + @Override + public int hashCode () { + return getValueWithExpressions().hashCode(); + } + + public String toString () { + return parameters.toString(); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/Pazpar2Commands.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/Pazpar2Commands.java new file mode 100644 index 0000000..b0e13b3 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/Pazpar2Commands.java @@ -0,0 +1,82 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import java.io.Serializable; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.SessionScoped; +import javax.inject.Inject; +import javax.inject.Named; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; +import com.indexdata.pz2utils4jsf.utils.Utils; + +@Named("req") @SessionScoped +public class Pazpar2Commands implements Serializable { + + private static final long serialVersionUID = -5172466320351302413L; + private static Logger logger = Logger.getLogger(Pazpar2Commands.class); + + public static final String INIT = "init"; + public static final String PING = "ping"; + public static final String SETTINGS = "settings"; + public static final String SEARCH = "search"; + public static final String STAT = "stat"; + public static final String SHOW = "show"; + public static final String RECORD = "record"; + public static final String TERMLIST = "termlist"; + public static final String BYTARGET = "bytarget"; + + @Inject StateManager stateMgr; + + public Pazpar2Commands() { + logger.info("Initializing Pazpar2Commands [" + Utils.objectId(this) + "]"); + } + + @PostConstruct + public void postConstruct() { + logger.info("in post-construct stateMgr is " + stateMgr); + } + + public InitCommand getInit() { + return (InitCommand) (stateMgr.checkOut(INIT)); + } + + public PingCommand getPing() { + return (PingCommand) (stateMgr.checkOut(PING)); + } + + public SettingsCommand getSettings() { + return (SettingsCommand) (stateMgr.checkOut(SETTINGS)); + } + + public SearchCommand getSearch() { + return (SearchCommand) (stateMgr.checkOut(SEARCH)); + } + + public StatCommand getStat() { + return (StatCommand) (stateMgr.checkOut(STAT)); + } + + public ShowCommand getShow() { + return (ShowCommand) (stateMgr.checkOut(SHOW)); + } + + public RecordCommand getRecord() { + return (RecordCommand) (stateMgr.checkOut(RECORD)); + } + + public TermlistCommand getTermlist() { + return (TermlistCommand) (stateMgr.checkOut(TERMLIST)); + } + + public BytargetCommand getBytarget() { + return (BytargetCommand) (stateMgr.checkOut(BYTARGET)); + } + + public Pazpar2Command getCommand(String name) { + return stateMgr.checkOut(name); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/PingCommand.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/PingCommand.java new file mode 100644 index 0000000..a182db1 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/PingCommand.java @@ -0,0 +1,13 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; + +public class PingCommand extends Pazpar2Command { + + private static final long serialVersionUID = 8876721711326535847L; + + public PingCommand(StateManager stateMgr) { + super("ping",stateMgr); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/RecordCommand.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/RecordCommand.java new file mode 100644 index 0000000..aa44fb9 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/RecordCommand.java @@ -0,0 +1,13 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; + +public class RecordCommand extends Pazpar2Command { + + private static final long serialVersionUID = 2817539422114569506L; + + public RecordCommand(StateManager stateMgr) { + super("record",stateMgr); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/SearchCommand.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/SearchCommand.java new file mode 100644 index 0000000..9486e86 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/SearchCommand.java @@ -0,0 +1,72 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import javax.enterprise.context.SessionScoped; + +import org.apache.log4j.Logger; + +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; + +@SessionScoped +public class SearchCommand extends Pazpar2Command { + + private static final long serialVersionUID = -1888520867838597236L; + private static Logger logger = Logger.getLogger(SearchCommand.class); + + public SearchCommand(StateManager stateMgr) { + super("search",stateMgr); + } + + public void setSession (String sessionId) { + setParameter(new CommandParameter("session","=",sessionId)); + } + + public void setQuery(String query) { + logger.info("writing query: " + query); + setParameter(new CommandParameter("query","=",query)); + } + + public String getQuery () { + logger.info("retrieving query"); + return getParameter("query") == null ? null : getParameter("query").getValueWithExpressions(); + } + + public void setFilter(String filterExpression) { + setParameter(new CommandParameter("filter","=",filterExpression)); + } + + public String getFilter() { + return getParameter("filter") == null ? null : getParameter("filter").getValueWithExpressions(); + } + + + public void setLimit (String limit) { + + } + + public void addFilter(String filterExpression) { + if (hasParameterSet("filter")) { + setFilter(filterExpression); + } else { + //TODO + getParameter("filter"); + } + } + + public void removeFilters () { + + } + + public void removeFilter(String filterExpression) { + + } + + public SearchCommand copy () { + SearchCommand newCommand = new SearchCommand(stateMgr); + for (String parameterName : parameters.keySet()) { + newCommand.setParameterSilently(parameters.get(parameterName).copy()); + } + return newCommand; + } + + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/SettingsCommand.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/SettingsCommand.java new file mode 100644 index 0000000..7aa1128 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/SettingsCommand.java @@ -0,0 +1,14 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; + +public class SettingsCommand extends Pazpar2Command { + + private static final long serialVersionUID = 2291179325470387102L; + + public SettingsCommand(StateManager stateMgr) { + super("settings",stateMgr); + // TODO Auto-generated constructor stub + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/ShowCommand.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/ShowCommand.java new file mode 100644 index 0000000..7a0fc02 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/ShowCommand.java @@ -0,0 +1,38 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; + +public class ShowCommand extends Pazpar2Command { + + private static final long serialVersionUID = -8242768313266051307L; + + public ShowCommand(StateManager stateMgr) { + super("show",stateMgr); + } + + public void setSort (String sort) { + setParameter(new CommandParameter("sort","=",sort)); + } + + public String getSort () { + return getParameter("sort") != null ? getParameter("sort").value : "relevance"; + } + + public void setPageSize (String perPageOption) { + setParameters(new CommandParameter("num","=",perPageOption), + new CommandParameter("start","=",0)); + } + + public String getPageSize () { + return getParameter("num") != null ? getParameter("num").value : "20"; + } + + public ShowCommand copy () { + ShowCommand newCommand = new ShowCommand(stateMgr); + for (String parameterName : parameters.keySet()) { + newCommand.setParameterSilently(parameters.get(parameterName).copy()); + } + return newCommand; + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/StatCommand.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/StatCommand.java new file mode 100644 index 0000000..4f93e50 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/StatCommand.java @@ -0,0 +1,13 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; + +public class StatCommand extends Pazpar2Command { + + private static final long serialVersionUID = 3980630346114157336L; + + public StatCommand(StateManager stateMgr) { + super("stat",stateMgr); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/TermlistCommand.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/TermlistCommand.java new file mode 100644 index 0000000..cd934b4 --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/commands/TermlistCommand.java @@ -0,0 +1,13 @@ +package com.indexdata.pz2utils4jsf.pazpar2.commands; + +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; + +public class TermlistCommand extends Pazpar2Command { + + private static final long serialVersionUID = -7067878552863021727L; + + public TermlistCommand(StateManager stateMgr) { + super("termlist",stateMgr); + } + +} diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java index 61464db..c4b77d8 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxyClient.java @@ -39,10 +39,10 @@ import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; import com.indexdata.pz2utils4jsf.config.Configuration; import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.errors.ConfigurationException; -import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter; import com.indexdata.pz2utils4jsf.pazpar2.CommandResponse; -import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; import com.indexdata.pz2utils4jsf.pazpar2.SearchClient; +import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandParameter; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command; import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.AuthenticationEntity; import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser; import com.indexdata.pz2utils4jsf.utils.Utils; @@ -96,7 +96,7 @@ public class ServiceProxyClient implements SearchClient { try { logger.info("Authenticating [" + user.getProperty("name") + "]"); this.user = (ServiceProxyUser) user; - Pazpar2Command auth = new Pazpar2Command("auth"); + Pazpar2Command auth = new Pazpar2Command("auth",null); auth.setParameter(new CommandParameter("action","=","login")); auth.setParameter(new CommandParameter("username","=",user.getProperty("name"))); auth.setParameter(new CommandParameter("password","=",user.getProperty("password"))); @@ -121,7 +121,7 @@ public class ServiceProxyClient implements SearchClient { public boolean checkAuthentication () { try { - Pazpar2Command check = new Pazpar2Command("auth"); + Pazpar2Command check = new Pazpar2Command("auth",null); check.setParameter(new CommandParameter("action","=","check")); byte[] response = send(check); logger.info(new String(response,"UTF-8")); diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxySession.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxySession.java index c7571ac..6dc7b6b 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxySession.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/sp/ServiceProxySession.java @@ -5,11 +5,13 @@ import java.io.UnsupportedEncodingException; import org.apache.log4j.Logger; -import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter; +import com.indexdata.pz2utils4jsf.pazpar2.ForServiceProxy; import com.indexdata.pz2utils4jsf.pazpar2.Pz2Session; +import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandParameter; import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser; import com.indexdata.pz2utils4jsf.utils.Utils; +@ForServiceProxy public class ServiceProxySession extends Pz2Session implements ServiceProxyInterface { private ServiceProxyUser user; 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 4412a7f..9850ef9 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/Pazpar2State.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/Pazpar2State.java @@ -1,24 +1,45 @@ package com.indexdata.pz2utils4jsf.pazpar2.state; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; - +import com.indexdata.pz2utils4jsf.pazpar2.commands.BytargetCommand; +import com.indexdata.pz2utils4jsf.pazpar2.commands.InitCommand; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Commands; +import com.indexdata.pz2utils4jsf.pazpar2.commands.PingCommand; +import com.indexdata.pz2utils4jsf.pazpar2.commands.RecordCommand; +import com.indexdata.pz2utils4jsf.pazpar2.commands.SearchCommand; +import com.indexdata.pz2utils4jsf.pazpar2.commands.SettingsCommand; +import com.indexdata.pz2utils4jsf.pazpar2.commands.ShowCommand; +import com.indexdata.pz2utils4jsf.pazpar2.commands.StatCommand; +import com.indexdata.pz2utils4jsf.pazpar2.commands.TermlistCommand; +/** + * Holds a 'pazpar2 state', understood as a full set of pazpar2 commands and + * all their parameter settings at a given point in time. + * + * @author Niels Erik + * + */ 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)); - } + public Pazpar2State (StateManager mgr) { + commands.put(Pazpar2Commands.INIT, new InitCommand(mgr)); + commands.put(Pazpar2Commands.PING, new PingCommand(mgr)); + commands.put(Pazpar2Commands.SETTINGS, new SettingsCommand(mgr)); + commands.put(Pazpar2Commands.SEARCH, new SearchCommand(mgr)); + commands.put(Pazpar2Commands.STAT, new StatCommand(mgr)); + commands.put(Pazpar2Commands.SHOW, new ShowCommand(mgr)); + commands.put(Pazpar2Commands.RECORD, new RecordCommand(mgr)); + commands.put(Pazpar2Commands.TERMLIST, new TermlistCommand(mgr)); + commands.put(Pazpar2Commands.BYTARGET, new BytargetCommand(mgr)); key = "#initial"; } - + /** * Creates new state by cloning all commands of the provided state and * then overriding one of them with the provided state changing command. diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/StateListener.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/StateListener.java new file mode 100644 index 0000000..05fd5db --- /dev/null +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/StateListener.java @@ -0,0 +1,7 @@ +package com.indexdata.pz2utils4jsf.pazpar2.state; + +public interface StateListener { + + public void stateUpdate(String commandName); + +} 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 7479bdd..14951e6 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/StateManager.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/state/StateManager.java @@ -1,27 +1,53 @@ package com.indexdata.pz2utils4jsf.pazpar2.state; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; +import javax.enterprise.context.SessionScoped; + import org.apache.log4j.Logger; -import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command; +import com.indexdata.pz2utils4jsf.utils.Utils; -public class StateManager { +@SessionScoped +public class StateManager implements Serializable { + private static final long serialVersionUID = 8152558351351730035L; + Map states = new HashMap(); String currentKey = ""; + private static List allCommands = new ArrayList(Arrays.asList("init","ping","settings","search","stat","show","record","termlist","bytarget")); Map pendingStateChanges = new HashMap(); private static Logger logger = Logger.getLogger(StateManager.class); + private List listeners = new ArrayList(); public StateManager () { - Pazpar2State initialState = new Pazpar2State(); + logger.info("Initializing a Pazpar2 state manager [" + Utils.objectId(this) + "]"); + Pazpar2State initialState = new Pazpar2State(this); states.put(initialState.getKey(), initialState); currentKey = initialState.getKey(); - for (String command : Pazpar2Command.allCommands) { + for (String command : allCommands) { pendingStateChanges.put(command, new Boolean(false)); } - + } + + public void addStateListener(StateListener listener) { + listeners.add(listener); + } + + public void removeStateListener (StateListener listener) { + listeners.remove(listener); + } + + private void updateListeners (String command) { + for (StateListener lsnr : listeners) { + lsnr.stateUpdate(command); + } } /** @@ -38,6 +64,7 @@ public class StateManager { */ public void checkIn(Pazpar2Command command) { if (getCurrentState().stateMutating(command)) { + logger.debug("State changed by: " + command.getName()); Pazpar2State state = new Pazpar2State(getCurrentState(),command); states.put(state.getKey(), state); currentKey = state.getKey(); @@ -56,13 +83,14 @@ public class StateManager { * @return Copy this state's instance of the given command */ public Pazpar2Command checkOut (String commandName) { + logger.info("Getting " + commandName + " from state manager."); return getCurrentState().getCommand(commandName).copy(); } public Pazpar2State getCurrentState () { return states.get(currentKey); } - + /** * Changes the current state key. Invoked from the UI to have the state * manager switch to another state than the current one. @@ -89,19 +117,20 @@ public class StateManager { } /** - * Sets a pending-state-change flag for the given command. Used by - * the beans to decide whether, say, a search should be executed before - * doing the next show. + * Sets a pending-state-change flag for the given command and notifies + * registered listeners. * - * It is up to the client to set and reset this flag since the state - * manager is not otherwise informed about actual request activities - * (only about the definition of commands to be executed) + * It is up to the listener to reset the flag as needed. * * @param command * @param bool */ public void hasPendingStateChange(String command, boolean bool) { pendingStateChanges.put(command, new Boolean(bool)); + if (bool) { + logger.debug("Updating listeners with state change from " + command); + updateListeners(command); + } } /**