X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2FPz2Session.java;h=56aa95bb94ed9c2e5a951b24d6275b79b52647d4;hb=f9b06d877390d5b980dc3ad9c86b2b334cf550c9;hp=4d8f8c2d2a582003862523ed52b1cad7674304a3;hpb=0f71d8e1a79d3d007cc52b58bfe33517a69224b7;p=mkjsf-moved-to-github.git diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java index 4d8f8c2..56aa95b 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -6,8 +6,8 @@ import java.util.Map; import java.util.StringTokenizer; import java.util.concurrent.ConcurrentHashMap; -import javax.enterprise.context.SessionScoped; -import javax.inject.Named; +import javax.annotation.PostConstruct; +import javax.inject.Inject; import org.apache.log4j.Logger; @@ -17,6 +17,11 @@ 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.CommandReadOnly; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Commands; +import com.indexdata.pz2utils4jsf.pazpar2.commands.SearchCommand; import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError; import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; @@ -27,17 +32,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 +55,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,11 +92,11 @@ 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)); - logger.debug(Utils.objectId(this) + " is searching using "+getCommand("search").getParameter("query").getEncodedQueryString()); + logger.debug(Utils.objectId(this) + " is searching using "+req.getCommandReadOnly("search").getUrlEncodedParameterValue("query")); doCommand("search"); } @@ -108,7 +124,7 @@ public class Pz2Session implements Pz2Interface { List threadList = new ArrayList(); StringTokenizer tokens = new StringTokenizer(commands,","); while (tokens.hasMoreElements()) { - threadList.add(new CommandThread(getCommand(tokens.nextToken()),searchClient)); + threadList.add(new CommandThread(req.getCommandReadOnly(tokens.nextToken()),searchClient)); } for (CommandThread thread : threadList) { thread.start(); @@ -145,6 +161,7 @@ public class Pz2Session implements Pz2Interface { } + /* public void setQuery (String query) { logger.debug("Creating new command parameter for " + query); setCommandParameter("search",new CommandParameter("query","=",query)); @@ -153,12 +170,13 @@ public class Pz2Session implements Pz2Interface { public String getQuery () { return getCommandParameterValueSimple("search","query",null); } + */ public void setFacet (String facetKey, String term) { if (term != null && term.length()>0) { - Pazpar2Command command = getCommand("search"); + Pazpar2Command command = req.getCommand("search"); command.getParameter("query").addExpression(new Expression(facetKey,"=",term)); - stateManager.checkIn(command); + stateMgr.checkIn(command); doSearch(); } } @@ -166,15 +184,16 @@ public class Pz2Session implements Pz2Interface { public void setFacetOnQuery (String facetKey, String term) { String facetExpression = facetKey + "=" + term; if (term != null && term.length()>0) { - setCommandParameter("search",new CommandParameter("query","=", getQuery() + " and " + facetExpression)); + String currentQuery= req.getCommandReadOnly("search").getParameterValue("query"); + setCommandParameter("search",new CommandParameter("query","=", currentQuery + " and " + facetExpression)); doSearch(); } } public void removeFacet(String facetKey, String term) { - Pazpar2Command command = getCommand("search"); + SearchCommand command = req.getSearch(); command.getParameter("query").removeExpression(new Expression(facetKey,"=",term)); - stateManager.checkIn(command); + stateMgr.checkIn(command); doSearch(); } @@ -202,42 +221,7 @@ public class Pz2Session implements Pz2Interface { public boolean hasSingleTargetFilter() { return singleTargetFilter != null; } - - public void setSort (String sortOption) { - logger.debug("Setting sort option: " + sortOption); - setCommandParameter("show",new CommandParameter("sort","=",sortOption)); - update("show"); - } - - public String getSort () { - return getCommandParameterValue("show","sort","relevance"); - } - - public void setPageSize (int perPageOption) { - if (getPageSize()!=perPageOption) { - logger.debug("Setting perpage option to " + perPageOption + " and resetting start page."); - setCommandParameter("show",new CommandParameter("num","=",perPageOption)); - setCommandParameter("show",new CommandParameter("start","=",0)); - update("show"); - } else { - logger.debug("Not updating page size, already is " + perPageOption); - } - } - - public int getPageSize () { - return getCommandParameterValue("show","num",20); - } - - public void setStart (int start) { - logger.debug("Setting start num to " + start); - setCommandParameter("show", new CommandParameter("start","=",start)); - update("show"); - } - - public int getStart() { - return getCommandParameterValue("show","start",0); - } - + public String toggleRecord (String recId) { if (hasRecord(recId)) { removeCommand("record"); @@ -261,7 +245,7 @@ public class Pz2Session implements Pz2Interface { @Override public boolean hasRecord (String recId) { - return getCommand("record").hasParameters() && getRecord().getRecId().equals(recId); + return req.getCommandReadOnly("record").hasParameters() && getRecord().getRecId().equals(recId); } public ShowResponse getShow () { @@ -294,11 +278,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 () { @@ -356,8 +340,8 @@ public class Pz2Session implements Pz2Interface { return hasSingleTargetFilter() && targetFilter.equals(this.singleTargetFilter); } - protected boolean hasQuery() { - return getCommand("search").getParameter("query") != null && getCommand("search").getParameter("query").getValueWithExpressions().length()>0; + protected boolean hasQuery() { + return req.getSearch().getParameter("query") != null && req.getSearch().getParameter("query").getValueWithExpressions().length()>0; } public boolean hasRecords () { @@ -374,7 +358,7 @@ public class Pz2Session implements Pz2Interface { } public ResultsPager setPager (int pageRange) { - pager = new ResultsPager(this,pageRange); + pager = new ResultsPager(this,pageRange,req); return pager; } @@ -383,20 +367,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); - if (getCommand("record").hasParameters()) { + stateMgr.hasPendingStateChange("record",false); + if (req.getCommandReadOnly("record").hasParameters()) { update("record"); } else { removeCommand("record"); dataObjects.put("record", new RecordResponse()); } - } + } } protected String getActiveClients() { @@ -423,64 +407,69 @@ public class Pz2Session implements Pz2Interface { * @return */ protected Pazpar2Command getCommand(String name) { - return stateManager.checkOut(name); + return req.getCommand(name); + } + + /** + * Returns an interface to a Pazpar2Command with only String getters. + * + * Since the command cannot be modified (unless it is cast) we can avoid + * cloning it before returning it from the current state. + * It can be used for log statements, checks and for performing the + * actual pazpar2 request. + * + * @param name + * @return + */ + protected CommandReadOnly getCommandReadOnly(String name) { + return req.getCommandReadOnly(name); } + protected void setCommandParameter(String commandName, CommandParameter parameter) { logger.debug("Setting parameter for " + commandName + ": " + parameter); - Pazpar2Command command = getCommand(commandName); + Pazpar2Command command = req.getCommand(commandName); command.setParameter(parameter); - stateManager.checkIn(command); + stateMgr.checkIn(command); } protected void removeCommandParameter(String commandName, String parameterName) { - Pazpar2Command command = getCommand(commandName); + Pazpar2Command command = req.getCommand(commandName); command.removeParameter(parameterName); - stateManager.checkIn(command); + stateMgr.checkIn(command); } protected void removeCommand (String commandName) { - stateManager.checkIn(new Pazpar2Command(commandName)); + Pazpar2Command command = req.getCommand(commandName); + command.removeParameters(); + stateMgr.checkIn(command); } protected String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { - Pazpar2Command command = getCommand(commandName); + CommandReadOnly command = req.getCommandReadOnly(commandName); if (command != null) { - CommandParameter parameter = command.getParameter(parameterName); + String parameter = command.getParameterValue(parameterName); if (parameter != null) { - return parameter.getValueWithExpressions(); + return parameter; } } return defaultValue; } - - protected String getCommandParameterValueSimple (String commandName, String parameterName, String defaultValue) { - Pazpar2Command command = getCommand(commandName); - if (command != null) { - CommandParameter parameter = command.getParameter(parameterName); - if (parameter != null) { - return parameter.getSimpleValue(); - } - } - return defaultValue; - } - - + protected int getCommandParameterValue (String commandName, String parameterName, int defaultValue) { - Pazpar2Command command = getCommand(commandName); + CommandReadOnly command = req.getCommandReadOnly(commandName); if (command != null) { - CommandParameter parameter = command.getParameter(parameterName); + String parameter = command.getParameterValue(parameterName); if (parameter != null) { - return Integer.parseInt(parameter.getSimpleValue()); + return Integer.parseInt(parameter); } } return defaultValue; } - protected String doCommand(String commandName) { - Pazpar2Command command = getCommand(commandName); - logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString()); + protected String doCommand(String commandName) { + logger.debug(req.getCommandReadOnly(commandName).getEncodedQueryString() + ": Results for "+ req.getCommandReadOnly("search").getEncodedQueryString()); return update(commandName); } @@ -502,11 +491,20 @@ public class Pz2Session implements Pz2Interface { } public String getFilter() { - return getCommandParameterValueSimple("search", "filter", ""); + return getCommandParameterValue("search", "filter", ""); } 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); + } + } }