X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2FPz2Session.java;h=3dec233899d19c1ad85724c889e156a378c3d809;hb=a78346a10c75cd39bd0009517aebabc73871e2c3;hp=9675696ebee4b5ab5cdfaf3e5321fb2e9014e00f;hpb=c7293a0fadc8a364a2f71a2cada38df685fb8720;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 9675696..3dec233 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -1,6 +1,5 @@ package com.indexdata.pz2utils4jsf.pazpar2; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -12,13 +11,18 @@ import javax.inject.Named; import org.apache.log4j.Logger; -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.ConfigurationError; +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; +import com.indexdata.pz2utils4jsf.errors.ErrorHelper; +import com.indexdata.pz2utils4jsf.errors.ErrorInterface; 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; +import com.indexdata.pz2utils4jsf.pazpar2.data.SearchResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.StatResponse; import com.indexdata.pz2utils4jsf.pazpar2.data.TermListsResponse; @@ -28,39 +32,45 @@ import com.indexdata.pz2utils4jsf.utils.Utils; @Named @SessionScoped public class Pz2Session implements Pz2Interface { - + + 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(); + protected QueryStates queryStates = new QueryStates(); + protected ErrorHelper errorHelper = null; - private static final long serialVersionUID = 3947514708343320514L; - private com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration cfg = null; - private com.indexdata.masterkey.pazpar2.client.Pazpar2Client client = null; - private TargetFilter targetFilter = null; - private ResultsPager pager = null; - + protected List configurationErrors = null; + protected SearchClient searchClient = null; + protected TargetFilter targetFilter = null; + protected ResultsPager pager = null; + public Pz2Session () { logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); } - public void init(Pz2Configurator pz2conf) { - if (client==null) { - logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(pz2conf)); + 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 { - cfg = new com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration(pz2conf.getConfig()); - client = new com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric(cfg); - resetDataObjects(); - } catch (ProxyErrorException e) { - e.printStackTrace(); - } catch (IOException ioe) { - ioe.printStackTrace(); - } - } else { - logger.warn("Attempt to configure session but it already has a configured client"); - } + 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(); @@ -83,7 +93,7 @@ public class Pz2Session implements Pz2Interface { logger.debug("Updating show,stat,termlist,bytarget from pazpar2"); return update("show,stat,termlist,bytarget"); } - + /** * Refreshes the data objects listed in 'commands' from pazpar2 * @@ -91,33 +101,40 @@ public class Pz2Session implements Pz2Interface { * @return Number of activeclients at the time of the 'show' command */ public String update (String commands) { - if (hasQuery()) { - handleQueryStateChanges(commands); - logger.debug("Processing request for " + commands); - List threadList = new ArrayList(); - StringTokenizer tokens = new StringTokenizer(commands,","); - while (tokens.hasMoreElements()) { - threadList.add(new CommandThread(getCommand(tokens.nextToken()),client)); - } - for (CommandThread thread : threadList) { - thread.start(); - } - for (CommandThread thread : threadList) { - try { - thread.join(); - } catch (InterruptedException e) { - e.printStackTrace(); + if (! hasConfigurationErrors()) { + if (hasQuery()) { + handleQueryStateChanges(commands); + logger.debug("Processing request for " + commands); + List threadList = new ArrayList(); + StringTokenizer tokens = new StringTokenizer(commands,","); + while (tokens.hasMoreElements()) { + threadList.add(new CommandThread(getCommand(tokens.nextToken()),searchClient)); } - } - for (CommandThread thread : threadList) { - if (!thread.getCommand().getName().equals("search")) { - dataObjects.put(thread.getCommand().getName(), new Pazpar2ResponseParser().getObject(thread.getResponse())); + for (CommandThread thread : threadList) { + thread.start(); + } + for (CommandThread thread : threadList) { + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } } + for (CommandThread thread : threadList) { + String commandName = thread.getCommand().getName(); + String response = thread.getResponse(); + logger.debug("Response was: " + response); + Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response); + dataObjects.put(commandName, responseObject); + } + return getActiveClients(); + } else { + logger.debug("Skipped requests for " + commands + " as there's not yet a query."); + resetDataObjects(); + return "0"; } - return getActiveClients(); } else { - logger.debug("Skipped requests for " + commands + " as there's not yet a query."); - resetDataObjects(); + logger.error("Did not attempt to execute query since there are configuration errors."); return "0"; } @@ -264,16 +281,66 @@ public class Pz2Session implements Pz2Interface { return queryStates.getCurrentStateKey(); } - public void setCurrentStateKey(String key) { - logger.debug("************** request to set state key to: [" + key + "]"); + public void setCurrentStateKey(String key) { queryStates.setCurrentStateKey(key); } + + public boolean hasConfigurationErrors () { + return (configurationErrors.size()>0); + } + + public boolean hasCommandErrors () { + if (dataObjects.get("search").hasApplicationError()) { + logger.info("Error detected in search"); + return true; + } + for (String name : dataObjects.keySet()) { + if (dataObjects.get(name).hasApplicationError()) { + logger.info("Error detected in " + name); + return true; + } + } + return false; + } + + /** + * Returns true if application error found in any response data objects + */ + public boolean hasErrors () { + return hasConfigurationErrors() || hasCommandErrors(); + } + + public List getConfigurationErrors() { + return configurationErrors; + } + + /** + * Returns a search command error, if any, otherwise the first + * error found for an arbitrary command, if any, otherwise + * an empty dummy error. + */ + public ErrorInterface getCommandError() { + CommandError error = new CommandError(); + if (dataObjects.get("search").hasApplicationError()) { + error = dataObjects.get("search").getApplicationError(); + } else { + for (String name : dataObjects.keySet()) { + if (dataObjects.get(name).hasApplicationError()) { + error = dataObjects.get(name).getApplicationError(); + break; + } + } + } + error.setErrorHelper(errorHelper); + return error; + } + - private boolean hasTargetFilter(TargetFilter targetFilter) { + protected boolean hasTargetFilter(TargetFilter targetFilter) { return hasTargetFilter() && targetFilter.equals(this.targetFilter); } - private boolean hasQuery() { + protected boolean hasQuery() { return !(getCommand("search").getParameter("query") == null); } @@ -295,7 +362,11 @@ public class Pz2Session implements Pz2Interface { return pager; } - private void handleQueryStateChanges (String commands) { + protected ErrorHelper getTroubleshooter() { + return errorHelper; + } + + protected void handleQueryStateChanges (String commands) { if (queryStates.hasPendingStateChange("search")) { logger.debug("Found pending search change. Doing search before updating " + commands); doSearch(); @@ -312,34 +383,34 @@ public class Pz2Session implements Pz2Interface { } } - private String getActiveClients() { - logger.debug("Active clients: "+getShow().getActiveClients()); + protected String getActiveClients() { if (getShow()!=null) { + logger.debug("Active clients: "+getShow().getActiveClients()); return getShow().getActiveClients(); } else { return ""; } } - private Pazpar2Command getCommand(String name) { + protected Pazpar2Command getCommand(String name) { return queryStates.getCurrentState().getCommand(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); } - private void removeCommandParameter(String commandName, String parameterName) { + protected void removeCommandParameter(String commandName, String parameterName) { queryStates.getCurrentState().removeCommandParameter(commandName,parameterName,queryStates); } - private void removeCommand (String commandName) { + protected void removeCommand (String commandName) { queryStates.getCurrentState().removeCommand(commandName, queryStates); } - 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); @@ -350,7 +421,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); @@ -362,7 +433,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); @@ -373,20 +444,21 @@ public class Pz2Session implements Pz2Interface { return defaultValue; } - private String doCommand(String commandName) { + protected String doCommand(String commandName) { Pazpar2Command command = getCommand(commandName); logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString()); return update(commandName); } - private void resetDataObjects() { - logger.debug("Resetting show,stat,termlist,bytarget response objects."); + protected void resetDataObjects() { + logger.debug("Resetting show,stat,termlist,bytarget,search response objects."); dataObjects = new ConcurrentHashMap(); dataObjects.put("show", new ShowResponse()); dataObjects.put("stat", new StatResponse()); dataObjects.put("termlist", new TermListsResponse()); dataObjects.put("bytarget", new ByTarget()); dataObjects.put("record", new RecordResponse()); + dataObjects.put("search", new SearchResponse()); } - + }