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=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..cae075f 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -12,13 +12,21 @@ import javax.inject.Named; 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.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; @@ -35,27 +43,39 @@ public class Pz2Session implements Pz2Interface { private QueryStates queryStates = new QueryStates(); 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 Pazpar2ClientConfiguration cfg = null; + private Pazpar2Client client = null; private TargetFilter targetFilter = null; private ResultsPager pager = null; - + private ErrorHelper errorHelper = null; + private List configurationErrors = null; + public Pz2Session () { 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 com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration(pz2conf.getConfig()); - client = new com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric(cfg); + 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(); - } catch (IOException ioe) { - ioe.printStackTrace(); - } } else { logger.warn("Attempt to configure session but it already has a configured client"); } @@ -83,7 +103,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 +111,42 @@ 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()),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.debug("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"; } @@ -268,6 +297,58 @@ public class Pz2Session implements 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); @@ -295,6 +376,10 @@ public class Pz2Session implements 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); @@ -312,9 +397,9 @@ public class Pz2Session implements 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 ""; @@ -373,20 +458,22 @@ public class Pz2Session implements 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()); } + }