X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2FPz2Session.java;h=1bac38bc4e3d34e98d38db13bdeeaf10c3c68023;hb=6300a68c2452725149ec64bbaac8416dce4b3aa8;hp=822b41611b8a0443a23afc975e2057fc445bd31c;hpb=ab7a23c769e77affa25a47112d2e5e93ae23d7bc;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 822b416..1bac38b 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -12,9 +12,16 @@ 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.ErrorInterface; +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; @@ -36,27 +43,41 @@ 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)); + } + 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("Found " + 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"); } @@ -92,31 +113,38 @@ 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) { + 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"; } - for (CommandThread thread : threadList) { - dataObjects.put(thread.getCommand().getName(), new Pazpar2ResponseParser().getObject(thread.getResponse())); - } - 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"; } @@ -263,46 +291,58 @@ 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 hasErrors () { - if (dataObjects.get("search").isError()) { + 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).isError()) { + if (dataObjects.get(name).hasApplicationError()) { logger.info("Error detected in " + name); return true; } } - return false; + return false; } - - public String getErrorMessages() { - StringBuilder msgs = new StringBuilder(""); - for (String name : dataObjects.keySet()) { - if (dataObjects.get(name).isError()) { - msgs.append(name + ": " + dataObjects.get(name).getErrorMessage()); - } - } - return msgs.toString(); + + /** + * Returns true if application error found in any response data objects + */ + public boolean hasErrors () { + return hasConfigurationErrors() || hasCommandErrors(); + } + + public List getConfigurationErrors() { + return configurationErrors; } - public String getFirstErrorMessage() { - if (dataObjects.get("search").isError()) { - return "Error doing search: " + dataObjects.get("search").getErrorMessage(); - } - for (String name : dataObjects.keySet()) { - if (dataObjects.get(name).isError()) { - return name + ": " + dataObjects.get(name).getErrorMessage(); - } + /** + * 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; + } + } } - return ""; - + error.setErrorHelper(errorHelper); + return error; } @@ -332,6 +372,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); @@ -410,10 +454,10 @@ 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() { @@ -427,5 +471,4 @@ public class Pz2Session implements Pz2Interface { dataObjects.put("search", new SearchResponse()); } - }