X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2FPz2Session.java;h=cae075fe41effa33f2c20bb00cc7ff316bf133d9;hb=2aa1c4adceb245c1f59f62e9adb77fda9f02291a;hp=0d8cc3fa076fdff78e1c74eb27f4fec8875e3d46;hpb=1724e77234e35c61de392b943730b76771ce5bb3;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 0d8cc3f..cae075f 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -1,38 +1,41 @@ package com.indexdata.pz2utils4jsf.pazpar2; -import java.io.Serializable; +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.enterprise.context.SessionScoped; +import javax.inject.Named; + import org.apache.log4j.Logger; -import com.indexdata.pz2utils4jsf.controls.ResultsPager; -import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter; -import com.indexdata.pz2utils4jsf.pazpar2.CommandThread; -import com.indexdata.pz2utils4jsf.pazpar2.Expression; -import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command; -import com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface; -import com.indexdata.pz2utils4jsf.pazpar2.Pz2Session; -import com.indexdata.pz2utils4jsf.pazpar2.TargetFilter; 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.JsfdemoPazpar2ClientConfiguration; +import com.indexdata.pz2utils4jsf.config.Pz2Configurator; +import com.indexdata.pz2utils4jsf.controls.ResultsPager; +import com.indexdata.pz2utils4jsf.errors.ApplicationError; +import com.indexdata.pz2utils4jsf.errors.ErrorHelper; +import com.indexdata.pz2utils4jsf.errors.ConfigurationError; +import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError; import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; 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; import com.indexdata.pz2utils4jsf.pazpar2.data.TermResponse; import com.indexdata.pz2utils4jsf.pazpar2.state.QueryStates; +import com.indexdata.pz2utils4jsf.utils.Utils; -public class Pz2Session implements Serializable, Pz2Interface { +@Named @SessionScoped +public class Pz2Session implements Pz2Interface { private static Logger logger = Logger.getLogger(Pz2Session.class); @@ -44,18 +47,40 @@ public class Pz2Session implements Serializable, Pz2Interface { private Pazpar2Client client = null; private TargetFilter targetFilter = null; private ResultsPager pager = null; - + private ErrorHelper errorHelper = null; + private List configurationErrors = null; + public Pz2Session () { - logger.debug("Instantiating pz2"); - try { - cfg = new Pazpar2ClientConfiguration(new JsfdemoPazpar2ClientConfiguration().getModuleConfiguration()); - client = new Pazpar2ClientGeneric(cfg); + 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)); + } + try { + client = new Pazpar2ClientGeneric(cfg); + } catch (ProxyErrorException pe) { + logger.error("Could not instantiate Pazpar2 client: " + pe.getMessage()); + configurationErrors.add(new ConfigurationError("Pz2Client","ProxyError","Could not create Pazpar2 client: " +pe.getMessage(),errorHelper)); + } + logger.info("Got " + configurationErrors.size() + " configuration errors"); resetDataObjects(); - } catch (ProxyErrorException e) { - e.printStackTrace(); - } + } else { + logger.warn("Attempt to configure session but it already has a configured client"); + } } - + public void doSearch(String query) { setCommandParameter("search",new CommandParameter("query","=",query)); doSearch(); @@ -65,7 +90,7 @@ public class Pz2Session implements Serializable, Pz2Interface { queryStates.hasPendingStateChange("search",false); resetDataObjects(); setCommandParameter("show",new CommandParameter("start","=",0)); - logger.info("Searching using "+getCommand("search").getParameter("query").getEncodedQueryString()); + logger.debug(Utils.objectId(this) + " is searching using "+getCommand("search").getParameter("query").getEncodedQueryString()); doCommand("search"); } @@ -75,10 +100,10 @@ public class Pz2Session implements Serializable, Pz2Interface { * @return Number of activeclients at the time of the 'show' command. */ public String update () { - logger.info("Updating show,stat,termlist, and bytarget data from pazpar2"); + logger.debug("Updating show,stat,termlist,bytarget from pazpar2"); return update("show,stat,termlist,bytarget"); } - + /** * Refreshes the data objects listed in 'commands' from pazpar2 * @@ -86,33 +111,42 @@ public class Pz2Session implements Serializable, 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()),client)); } - } - 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(); + Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(thread.getResponse()); + 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.info("Skipped requests for " + commands + " as there's not yet a query."); - resetDataObjects(); + configurationErrors.add( + new ConfigurationError("Querying while errors", + "App halted", + "Cannot query Pazpar2 while there are configuration errors.", + errorHelper)); return "0"; } @@ -263,6 +297,58 @@ public class Pz2Session implements Serializable, Pz2Interface { logger.debug("************** request to set state key to: [" + 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() { + logger.info("Returning " + configurationErrors.size() + " configuration errors"); + 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 ApplicationError 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) { return hasTargetFilter() && targetFilter.equals(this.targetFilter); @@ -290,6 +376,10 @@ public class Pz2Session implements Serializable, Pz2Interface { return pager; } + protected ErrorHelper getTroubleshooter() { + return errorHelper; + } + private void handleQueryStateChanges (String commands) { if (queryStates.hasPendingStateChange("search")) { logger.debug("Found pending search change. Doing search before updating " + commands); @@ -307,9 +397,9 @@ public class Pz2Session implements Serializable, Pz2Interface { } } - private String getActiveClients() { - logger.debug("Active clients: "+getShow().getActiveClients()); + private String getActiveClients() { if (getShow()!=null) { + logger.debug("Active clients: "+getShow().getActiveClients()); return getShow().getActiveClients(); } else { return ""; @@ -368,20 +458,22 @@ public class Pz2Session implements Serializable, Pz2Interface { return defaultValue; } - private String doCommand(String commandName) { + private String doCommand(String commandName) { Pazpar2Command command = getCommand(commandName); logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString()); - return update(commandName); + return update(commandName); } private void resetDataObjects() { - logger.debug("Resetting show,stat,termlist,bytarget response objects."); + 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()); } + }