package com.indexdata.pz2utils4jsf.pazpar2;\r
\r
-import java.io.Serializable;\r
+import java.io.IOException;\r
import java.util.ArrayList;\r
import java.util.List;\r
import java.util.Map;\r
import java.util.StringTokenizer;\r
import java.util.concurrent.ConcurrentHashMap;\r
\r
+import javax.enterprise.context.SessionScoped;\r
+import javax.inject.Named;\r
+\r
import org.apache.log4j.Logger;\r
\r
-import com.indexdata.pz2utils4jsf.controls.ResultsPager;\r
-import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter;\r
-import com.indexdata.pz2utils4jsf.pazpar2.CommandThread;\r
-import com.indexdata.pz2utils4jsf.pazpar2.Expression;\r
-import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command;\r
-import com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface;\r
-import com.indexdata.pz2utils4jsf.pazpar2.Pz2Session;\r
-import com.indexdata.pz2utils4jsf.pazpar2.TargetFilter;\r
import com.indexdata.masterkey.pazpar2.client.Pazpar2Client;\r
import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration;\r
import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric;\r
import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException;\r
-import com.indexdata.pz2utils4jsf.config.JsfdemoPazpar2ClientConfiguration;\r
+import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.controls.ResultsPager;\r
+import com.indexdata.pz2utils4jsf.errors.ApplicationError;\r
+import com.indexdata.pz2utils4jsf.errors.ErrorHelper;\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationError;\r
+import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseParser;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse;\r
+import com.indexdata.pz2utils4jsf.pazpar2.data.SearchResponse;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.StatResponse;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.TermListsResponse;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.TermResponse;\r
import com.indexdata.pz2utils4jsf.pazpar2.state.QueryStates;\r
+import com.indexdata.pz2utils4jsf.utils.Utils;\r
\r
-public class Pz2Session implements Serializable, Pz2Interface {\r
+@Named @SessionScoped \r
+public class Pz2Session implements Pz2Interface {\r
\r
private static Logger logger = Logger.getLogger(Pz2Session.class);\r
\r
private Pazpar2Client client = null; \r
private TargetFilter targetFilter = null; \r
private ResultsPager pager = null; \r
-\r
+ private ErrorHelper errorHelper = null;\r
+ private List<ApplicationError> configurationErrors = null;\r
+ \r
public Pz2Session () {\r
- logger.debug("Instantiating pz2"); \r
- try {\r
- cfg = new Pazpar2ClientConfiguration(new JsfdemoPazpar2ClientConfiguration().getModuleConfiguration());\r
- client = new Pazpar2ClientGeneric(cfg);\r
+ logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); \r
+ }\r
+ \r
+ public void init(Pz2Configurator pz2conf) {\r
+ if (client==null) {\r
+ configurationErrors = new ArrayList<ApplicationError>();\r
+ errorHelper = new ErrorHelper(pz2conf);\r
+ logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(pz2conf));\r
+ try {\r
+ cfg = new Pazpar2ClientConfiguration(pz2conf.getConfig());\r
+ } catch (ProxyErrorException pe) {\r
+ logger.error("Could not configure Pazpar2 client: " + pe.getMessage());\r
+ configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + pe.getMessage(),errorHelper));\r
+ } catch (IOException io) {\r
+ logger.error("Could not configure Pazpar2 client: " + io.getMessage());\r
+ configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + io.getMessage(),errorHelper));\r
+ }\r
+ try {\r
+ client = new Pazpar2ClientGeneric(cfg); \r
+ } catch (ProxyErrorException pe) {\r
+ logger.error("Could not instantiate Pazpar2 client: " + pe.getMessage());\r
+ configurationErrors.add(new ConfigurationError("Pz2Client error","ProxyError","Could not create Pazpar2 client: " +pe.getMessage(),errorHelper)); \r
+ } \r
+ logger.info("Got " + configurationErrors.size() + " configuration errors");\r
resetDataObjects();\r
- } catch (ProxyErrorException e) {\r
- e.printStackTrace();\r
- } \r
+ } else {\r
+ logger.warn("Attempt to configure session but it already has a configured client");\r
+ }\r
}\r
-\r
+ \r
public void doSearch(String query) {\r
setCommandParameter("search",new CommandParameter("query","=",query)); \r
doSearch();\r
queryStates.hasPendingStateChange("search",false);\r
resetDataObjects();\r
setCommandParameter("show",new CommandParameter("start","=",0)); \r
- logger.info("Searching using "+getCommand("search").getParameter("query").getEncodedQueryString());\r
+ logger.debug(Utils.objectId(this) + " is searching using "+getCommand("search").getParameter("query").getEncodedQueryString());\r
doCommand("search"); \r
}\r
\r
* @return Number of activeclients at the time of the 'show' command.\r
*/\r
public String update () {\r
- logger.info("Updating show,stat,termlist, and bytarget data from pazpar2");\r
+ logger.debug("Updating show,stat,termlist,bytarget from pazpar2");\r
return update("show,stat,termlist,bytarget");\r
}\r
- \r
+ \r
/**\r
* Refreshes the data objects listed in 'commands' from pazpar2\r
* \r
* @return Number of activeclients at the time of the 'show' command\r
*/\r
public String update (String commands) {\r
- if (hasQuery()) {\r
- handleQueryStateChanges(commands);\r
- logger.debug("Processing request for " + commands); \r
- List<CommandThread> threadList = new ArrayList<CommandThread>();\r
- StringTokenizer tokens = new StringTokenizer(commands,",");\r
- while (tokens.hasMoreElements()) {\r
- threadList.add(new CommandThread(getCommand(tokens.nextToken()),client)); \r
- }\r
- for (CommandThread thread : threadList) {\r
- thread.start();\r
- }\r
- for (CommandThread thread : threadList) {\r
- try {\r
- thread.join();\r
- } catch (InterruptedException e) {\r
- e.printStackTrace();\r
+ if (! hasConfigurationErrors()) {\r
+ if (hasQuery()) {\r
+ handleQueryStateChanges(commands);\r
+ logger.debug("Processing request for " + commands); \r
+ List<CommandThread> threadList = new ArrayList<CommandThread>();\r
+ StringTokenizer tokens = new StringTokenizer(commands,",");\r
+ while (tokens.hasMoreElements()) {\r
+ threadList.add(new CommandThread(getCommand(tokens.nextToken()),client)); \r
}\r
- }\r
- for (CommandThread thread : threadList) {\r
- if (!thread.getCommand().getName().equals("search")) {\r
- dataObjects.put(thread.getCommand().getName(), new Pazpar2ResponseParser().getObject(thread.getResponse()));\r
+ for (CommandThread thread : threadList) {\r
+ thread.start();\r
+ }\r
+ for (CommandThread thread : threadList) {\r
+ try {\r
+ thread.join();\r
+ } catch (InterruptedException e) {\r
+ e.printStackTrace();\r
+ }\r
}\r
+ for (CommandThread thread : threadList) {\r
+ String commandName = thread.getCommand().getName();\r
+ Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(thread.getResponse());\r
+ dataObjects.put(commandName, responseObject); \r
+ }\r
+ return getActiveClients();\r
+ } else {\r
+ logger.debug("Skipped requests for " + commands + " as there's not yet a query."); \r
+ resetDataObjects();\r
+ return "0";\r
}\r
- return getActiveClients();\r
} else {\r
- logger.info("Skipped requests for " + commands + " as there's not yet a query."); \r
- resetDataObjects();\r
+ configurationErrors.add(\r
+ new ConfigurationError("Querying while errors",\r
+ "App halted",\r
+ "Cannot query Pazpar2 while there are configuration errors.",\r
+ errorHelper));\r
return "0";\r
}\r
\r
logger.debug("************** request to set state key to: [" + key + "]"); \r
queryStates.setCurrentStateKey(key);\r
}\r
+ \r
+ public boolean hasConfigurationErrors () {\r
+ return (configurationErrors.size()>0); \r
+ }\r
+ \r
+ public boolean hasCommandErrors () {\r
+ if (dataObjects.get("search").hasApplicationError()) {\r
+ logger.info("Error detected in search");\r
+ return true;\r
+ }\r
+ for (String name : dataObjects.keySet()) {\r
+ if (dataObjects.get(name).hasApplicationError()) {\r
+ logger.info("Error detected in " + name);\r
+ return true;\r
+ }\r
+ } \r
+ return false; \r
+ }\r
+ \r
+ /**\r
+ * Returns true if application error found in any response data objects \r
+ */\r
+ public boolean hasErrors () {\r
+ return hasConfigurationErrors() || hasCommandErrors();\r
+ }\r
+\r
+ public List<ApplicationError> getConfigurationErrors() {\r
+ logger.info("Returning " + configurationErrors.size() + " configuration errors");\r
+ return configurationErrors;\r
+ }\r
+ \r
+ /**\r
+ * Returns a search command error, if any, otherwise the first\r
+ * error found for an arbitrary command, if any, otherwise\r
+ * an empty dummy error. \r
+ */ \r
+ public ApplicationError getCommandError() {\r
+ CommandError error = new CommandError(); \r
+ if (dataObjects.get("search").hasApplicationError()) {\r
+ error = dataObjects.get("search").getApplicationError(); \r
+ } else {\r
+ for (String name : dataObjects.keySet()) { \r
+ if (dataObjects.get(name).hasApplicationError()) { \r
+ error = dataObjects.get(name).getApplicationError(); \r
+ break;\r
+ } \r
+ }\r
+ }\r
+ error.setErrorHelper(errorHelper);\r
+ return error; \r
+ }\r
+\r
\r
private boolean hasTargetFilter(TargetFilter targetFilter) {\r
return hasTargetFilter() && targetFilter.equals(this.targetFilter);\r
return pager;\r
}\r
\r
+ protected ErrorHelper getTroubleshooter() {\r
+ return errorHelper;\r
+ }\r
+ \r
private void handleQueryStateChanges (String commands) {\r
if (queryStates.hasPendingStateChange("search")) { \r
logger.debug("Found pending search change. Doing search before updating " + commands);\r
} \r
}\r
\r
- private String getActiveClients() {\r
- logger.debug("Active clients: "+getShow().getActiveClients());\r
+ private String getActiveClients() { \r
if (getShow()!=null) {\r
+ logger.debug("Active clients: "+getShow().getActiveClients());\r
return getShow().getActiveClients();\r
} else {\r
return "";\r
return defaultValue; \r
}\r
\r
- private String doCommand(String commandName) {\r
+ private String doCommand(String commandName) { \r
Pazpar2Command command = getCommand(commandName); \r
logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString());\r
- return update(commandName);\r
+ return update(commandName); \r
}\r
\r
private void resetDataObjects() {\r
- logger.debug("Resetting show,stat,termlist,bytarget response objects.");\r
+ logger.debug("Resetting show,stat,termlist,bytarget,search response objects.");\r
dataObjects = new ConcurrentHashMap<String,Pazpar2ResponseData>();\r
dataObjects.put("show", new ShowResponse());\r
dataObjects.put("stat", new StatResponse());\r
dataObjects.put("termlist", new TermListsResponse());\r
dataObjects.put("bytarget", new ByTarget());\r
dataObjects.put("record", new RecordResponse());\r
+ dataObjects.put("search", new SearchResponse());\r
}\r
+ \r
\r
}\r