X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2FPz2Session.java;h=cdb1a6b7949efaa7e8dc989bde59bbe87179a762;hb=977e4983e28683dd8a7d9fd19b0daee3ac349a3a;hp=3034a7497bc63cb606f56228b94aa3a4988c3e6e;hpb=c6b02eac06fa279177b30839c38e8e99f5dc445f;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 3034a74..cdb1a6b 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -1,28 +1,28 @@ package com.indexdata.pz2utils4jsf.pazpar2; -import java.io.IOException; import java.util.ArrayList; import java.util.List; 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; -import com.indexdata.masterkey.pazpar2.client.Pazpar2Client; -import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration; -import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric; -import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException; -import com.indexdata.pz2utils4jsf.config.Pz2Configurator; +import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.controls.ResultsPager; -import com.indexdata.pz2utils4jsf.errors.ErrorInterface; -import com.indexdata.pz2utils4jsf.errors.ErrorHelper; import com.indexdata.pz2utils4jsf.errors.ConfigurationError; -import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError; +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; import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseParser; import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse; @@ -31,66 +31,69 @@ 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.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); - private Map dataObjects = new ConcurrentHashMap(); - private QueryStates queryStates = new QueryStates(); + protected Map dataObjects = new ConcurrentHashMap(); + + @Inject StateManager stateMgr; + @Inject Pazpar2Commands req; - private static final long serialVersionUID = 3947514708343320514L; - private Pazpar2ClientConfiguration cfg = null; - private Pazpar2Client client = null; - private TargetFilter targetFilter = null; - private ResultsPager pager = null; - private ErrorHelper errorHelper = null; - private List configurationErrors = null; + protected ErrorHelper errorHelper = null; + protected List configurationErrors = null; + protected SearchClient searchClient = null; + protected SingleTargetFilter singleTargetFilter = null; + protected ResultsPager pager = null; + public Pz2Session () { - logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); + logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); } - - public void init(Pz2Configurator pz2conf) { - if (client==null) { - configurationErrors = new ArrayList(); - errorHelper = new ErrorHelper(pz2conf); - logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(pz2conf)); - try { - cfg = new Pazpar2ClientConfiguration(pz2conf.getConfig()); - } catch (ProxyErrorException pe) { - logger.error("Could not configure Pazpar2 client: " + pe.getMessage()); - configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + pe.getMessage(),errorHelper)); - } catch (IOException io) { - logger.error("Could not configure Pazpar2 client: " + io.getMessage()); - configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + io.getMessage(),errorHelper)); - } - if (cfg != null) { - try { - client = new Pazpar2ClientGeneric(cfg); - } catch (ProxyErrorException pe) { - logger.error("Could not instantiate Pazpar2 client: " + pe.getMessage()); - configurationErrors.add(new ConfigurationError("Pz2Client error","ProxyError","Could not create Pazpar2 client: " +pe.getMessage(),errorHelper)); - } - logger.info("Got " + configurationErrors.size() + " configuration errors"); - } - resetDataObjects(); - } else { - logger.warn("Attempt to configure session but it already has a configured client"); - } + + @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) { + configurationErrors = new ArrayList(); + errorHelper = new ErrorHelper(configReader); + logger.debug(Utils.objectId(this) + " will configure search client for the session"); + try { + searchClient.configure(configReader); + // At the time of writing this search client is injected using Weld. + // However, the client is used for asynchronously sending off requests + // to the server AND propagation of context to threads is currently + // not supported. Trying to do so throws a WELD-001303 error. + // To avoid that, a context free client is cloned from the context + // dependent one. + // If propagation to threads gets supported, the cloning can go. + this.searchClient = searchClient.cloneMe(); + } catch (ConfigurationException e) { + configurationErrors.add(new ConfigurationError("Search Client","Configuration",e.getMessage(),new ErrorHelper(configReader))); + } + logger.info(configReader.document()); + resetDataObjects(); + } + public void doSearch(String query) { setCommandParameter("search",new CommandParameter("query","=",query)); doSearch(); } public void doSearch() { - queryStates.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()); doCommand("search"); @@ -119,8 +122,8 @@ public class Pz2Session implements Pz2Interface { logger.debug("Processing request for " + commands); List threadList = new ArrayList(); StringTokenizer tokens = new StringTokenizer(commands,","); - while (tokens.hasMoreElements()) { - threadList.add(new CommandThread(getCommand(tokens.nextToken()),client)); + while (tokens.hasMoreElements()) { + threadList.add(new CommandThread(getCommand(tokens.nextToken()),searchClient)); } for (CommandThread thread : threadList) { thread.start(); @@ -134,10 +137,17 @@ public class Pz2Session implements Pz2Interface { } for (CommandThread thread : threadList) { String commandName = thread.getCommand().getName(); - Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(thread.getResponse()); + String response = thread.getResponse(); + logger.debug("Response was: " + response); + Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response); dataObjects.put(commandName, responseObject); } - return getActiveClients(); + if (commands.equals("record")) { + logger.debug("Record: Active clients: "+getRecord().getActiveClients()); + return getRecord().getActiveClients(); + } else { + return getActiveClients(); + } } else { logger.debug("Skipped requests for " + commands + " as there's not yet a query."); resetDataObjects(); @@ -160,8 +170,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)); + stateMgr.checkIn(command); doSearch(); } } @@ -175,33 +187,35 @@ 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)); + stateMgr.checkIn(command); doSearch(); } - public void setTargetFilter (String targetId, String targetName) { - if (hasTargetFilter(new TargetFilter(targetId,targetName))) { - logger.debug("Already using target filter " + this.targetFilter.getFilterExpression()); + public void setSingleTargetFilter (String targetId, String targetName) { + if (hasSingleTargetFilter(new SingleTargetFilter(targetId,targetName))) { + logger.debug("Already using target filter " + this.singleTargetFilter.getFilterExpression()); } else { - this.targetFilter = new TargetFilter(targetId,targetName); - setCommandParameter("search",new CommandParameter("filter","=",this.targetFilter.getFilterExpression())); + this.singleTargetFilter = new SingleTargetFilter(targetId,targetName); + setCommandParameter("search",new CommandParameter("filter","=",this.singleTargetFilter.getFilterExpression())); doSearch(); } } - public TargetFilter getTargetFilter () { - return targetFilter; + public SingleTargetFilter getSingleTargetFilter () { + return singleTargetFilter; } - public void removeTargetFilter () { - logger.debug("Removing target filter " + targetFilter.getFilterExpression()); - this.targetFilter = null; + public void removeSingleTargetFilter () { + logger.debug("Removing target filter " + singleTargetFilter.getFilterExpression()); + this.singleTargetFilter = null; removeCommandParameter("search","filter"); doSearch(); } - public boolean hasTargetFilter() { - return targetFilter != null; + public boolean hasSingleTargetFilter() { + return singleTargetFilter != null; } public void setSort (String sortOption) { @@ -238,22 +252,29 @@ public class Pz2Session implements Pz2Interface { public int getStart() { return getCommandParameterValue("show","start",0); } - + public String toggleRecord (String recId) { if (hasRecord(recId)) { removeCommand("record"); dataObjects.put("record", new RecordResponse()); return ""; } else { - return updateRecord(recId); + setRecordId(recId); + return doCommand("record"); } } - private String updateRecord(String recId) { - setCommandParameter("record",new CommandParameter("id","=",recId)); - return doCommand("record"); + @Override + public void setRecordId(String recId) { + setCommandParameter("record",new CommandParameter("id","=",recId)); + } + + @Override + public String getRecordId () { + return getCommandParameterValue("record","recid",""); } + @Override public boolean hasRecord (String recId) { return getCommand("record").hasParameters() && getRecord().getRecId().equals(recId); } @@ -288,11 +309,11 @@ public class Pz2Session implements Pz2Interface { public String getCurrentStateKey () { - return queryStates.getCurrentStateKey(); + return stateMgr.getCurrentState().getKey(); } public void setCurrentStateKey(String key) { - queryStates.setCurrentStateKey(key); + stateMgr.setCurrentStateKey(key); } public boolean hasConfigurationErrors () { @@ -346,12 +367,13 @@ public class Pz2Session implements Pz2Interface { } - private boolean hasTargetFilter(TargetFilter targetFilter) { - return hasTargetFilter() && targetFilter.equals(this.targetFilter); + protected boolean hasSingleTargetFilter(SingleTargetFilter targetFilter) { + return hasSingleTargetFilter() && targetFilter.equals(this.singleTargetFilter); } - private boolean hasQuery() { - return !(getCommand("search").getParameter("query") == null); + protected boolean hasQuery() { + logger.debug("req is " + req); + return req.getSearch().getParameter("query") != null && req.getSearch().getParameter("query").getValueWithExpressions().length()>0; } public boolean hasRecords () { @@ -376,24 +398,24 @@ public class Pz2Session implements Pz2Interface { return errorHelper; } - private void handleQueryStateChanges (String commands) { - if (queryStates.hasPendingStateChange("search")) { - logger.debug("Found pending search change. Doing search before updating " + commands); + protected void handleQueryStateChanges (String commands) { + if (stateMgr.hasPendingStateChange("search") && hasQuery()) { + logger.debug("Found pending search change. Doing search before updating " + commands); doSearch(); } - if (queryStates.hasPendingStateChange("record") && ! commands.equals("record")) { + if (stateMgr.hasPendingStateChange("record") && ! commands.equals("record")) { logger.debug("Found pending record ID change. Doing record before updating " + commands); - queryStates.hasPendingStateChange("record",false); + stateMgr.hasPendingStateChange("record",false); if (getCommand("record").hasParameters()) { - updateRecord(getCommand("record").getParameter("id").getSimpleValue()); + update("record"); } else { removeCommand("record"); dataObjects.put("record", new RecordResponse()); } - } + } } - private String getActiveClients() { + protected String getActiveClients() { if (getShow()!=null) { logger.debug("Active clients: "+getShow().getActiveClients()); return getShow().getActiveClients(); @@ -402,25 +424,45 @@ public class Pz2Session implements Pz2Interface { } } - private Pazpar2Command getCommand(String name) { - return queryStates.getCurrentState().getCommand(name); + /** + * 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 stateMgr.checkOut(name); } - private void setCommandParameter(String commandName, CommandParameter parameter) { + 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); + stateMgr.checkIn(command); } - private void removeCommandParameter(String commandName, String parameterName) { - queryStates.getCurrentState().removeCommandParameter(commandName,parameterName,queryStates); + protected void removeCommandParameter(String commandName, String parameterName) { + Pazpar2Command command = getCommand(commandName); + command.removeParameter(parameterName); + stateMgr.checkIn(command); } - private void removeCommand (String commandName) { - queryStates.getCurrentState().removeCommand(commandName, queryStates); + protected void removeCommand (String commandName) { + Pazpar2Command command = getCommand(commandName); + command.removeParameters(); + stateMgr.checkIn(command); } - private String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { + protected String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { Pazpar2Command command = getCommand(commandName); if (command != null) { CommandParameter parameter = command.getParameter(parameterName); @@ -431,7 +473,7 @@ public class Pz2Session implements Pz2Interface { return defaultValue; } - private String getCommandParameterValueSimple (String commandName, String parameterName, String defaultValue) { + protected String getCommandParameterValueSimple (String commandName, String parameterName, String defaultValue) { Pazpar2Command command = getCommand(commandName); if (command != null) { CommandParameter parameter = command.getParameter(parameterName); @@ -443,7 +485,7 @@ public class Pz2Session implements Pz2Interface { } - private int getCommandParameterValue (String commandName, String parameterName, int defaultValue) { + protected int getCommandParameterValue (String commandName, String parameterName, int defaultValue) { Pazpar2Command command = getCommand(commandName); if (command != null) { CommandParameter parameter = command.getParameter(parameterName); @@ -454,13 +496,13 @@ public class Pz2Session implements Pz2Interface { return defaultValue; } - private String doCommand(String commandName) { - Pazpar2Command command = getCommand(commandName); + protected String doCommand(String commandName) { + Pazpar2Command command = req.getCommand(commandName); logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString()); - return update(commandName); + return update(commandName); } - private void resetDataObjects() { + protected void resetDataObjects() { logger.debug("Resetting show,stat,termlist,bytarget,search response objects."); dataObjects = new ConcurrentHashMap(); dataObjects.put("show", new ShowResponse()); @@ -471,4 +513,27 @@ public class Pz2Session implements Pz2Interface { dataObjects.put("search", new SearchResponse()); } + @Override + public void setFilter(String filterExpression) { + logger.debug("Setting filter to " + filterExpression); + setCommandParameter("search",new CommandParameter("filter","=",filterExpression)); + } + + public String getFilter() { + return getCommandParameterValueSimple("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); + } + } + }