package com.indexdata.pz2utils4jsf.pazpar2;\r
\r
-import java.io.IOException;\r
import java.util.ArrayList;\r
import java.util.List;\r
import java.util.Map;\r
\r
import org.apache.log4j.Logger;\r
\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.Pz2Configurator;\r
+import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\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.errors.ConfigurationException;\r
+import com.indexdata.pz2utils4jsf.errors.ErrorHelper;\r
+import com.indexdata.pz2utils4jsf.errors.ErrorInterface;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget;\r
+import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError;\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
\r
@Named @SessionScoped \r
public class Pz2Session implements Pz2Interface {\r
- \r
+ \r
+ private static final long serialVersionUID = 3947514708343320514L;\r
private static Logger logger = Logger.getLogger(Pz2Session.class);\r
\r
- private Map<String,Pazpar2ResponseData> dataObjects = new ConcurrentHashMap<String,Pazpar2ResponseData>();\r
- private QueryStates queryStates = new QueryStates();\r
- \r
- private static final long serialVersionUID = 3947514708343320514L; \r
- private Pazpar2ClientConfiguration cfg = null;\r
- private Pazpar2Client client = null; \r
- private TargetFilter targetFilter = null; \r
- private ResultsPager pager = null; \r
- private ErrorHelper errorHelper = null;\r
- private List<ApplicationError> configurationErrors = null;\r
+ protected Map<String,Pazpar2ResponseData> dataObjects = new ConcurrentHashMap<String,Pazpar2ResponseData>();\r
+ protected QueryStates queryStates = new QueryStates();\r
+ protected ErrorHelper errorHelper = null;\r
\r
+ protected List<ErrorInterface> configurationErrors = null;\r
+ protected SearchClient searchClient = null; \r
+ protected TargetFilter targetFilter = null; \r
+ protected ResultsPager pager = null; \r
+ \r
public Pz2Session () {\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
- } else {\r
- logger.warn("Attempt to configure session but it already has a configured client");\r
- }\r
+ public void init(SearchClient searchClient, ConfigurationReader configReader) {\r
+ configurationErrors = new ArrayList<ErrorInterface>();\r
+ errorHelper = new ErrorHelper(configReader); \r
+ logger.debug(Utils.objectId(this) + " will configure search client for the session");\r
+ try {\r
+ searchClient.configure(configReader); \r
+ // At the time of writing this search client is injected using Weld. \r
+ // However, the client is used for asynchronously sending off requests\r
+ // to the server AND propagation of context to threads is currently \r
+ // not supported. Trying to do so throws a WELD-001303 error. \r
+ // To avoid that, a context free client is cloned from the context \r
+ // dependent one. \r
+ // If propagation to threads gets supported, the cloning can go. \r
+ this.searchClient = searchClient.cloneMe(); \r
+ \r
+ } catch (ConfigurationException e) {\r
+ configurationErrors.add(new ConfigurationError("Search Client","Configuration",e.getMessage(),new ErrorHelper(configReader))); \r
+ } \r
+ logger.info(configReader.document());\r
+ resetDataObjects();\r
}\r
- \r
+ \r
public void doSearch(String query) {\r
setCommandParameter("search",new CommandParameter("query","=",query)); \r
doSearch();\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
+ threadList.add(new CommandThread(getCommand(tokens.nextToken()),searchClient)); \r
}\r
for (CommandThread thread : threadList) {\r
thread.start();\r
}\r
for (CommandThread thread : threadList) {\r
String commandName = thread.getCommand().getName();\r
- Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(thread.getResponse());\r
+ String response = thread.getResponse();\r
+ logger.debug("Response was: " + response);\r
+ Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response);\r
dataObjects.put(commandName, responseObject); \r
}\r
return getActiveClients();\r
return "0";\r
}\r
} else {\r
- configurationErrors.add(\r
- new ConfigurationError("Querying while errors",\r
- "App halted",\r
- "Cannot query Pazpar2 while there are configuration errors.",\r
- errorHelper));\r
+ logger.error("Did not attempt to execute query since there are configuration errors.");\r
return "0";\r
}\r
\r
return queryStates.getCurrentStateKey();\r
}\r
\r
- public void setCurrentStateKey(String key) {\r
- logger.debug("************** request to set state key to: [" + key + "]"); \r
+ public void setCurrentStateKey(String key) { \r
queryStates.setCurrentStateKey(key);\r
}\r
\r
return hasConfigurationErrors() || hasCommandErrors();\r
}\r
\r
- public List<ApplicationError> getConfigurationErrors() {\r
- logger.info("Returning " + configurationErrors.size() + " configuration errors");\r
+ public List<ErrorInterface> getConfigurationErrors() { \r
return configurationErrors;\r
}\r
\r
* error found for an arbitrary command, if any, otherwise\r
* an empty dummy error. \r
*/ \r
- public ApplicationError getCommandError() {\r
+ public ErrorInterface getCommandError() {\r
CommandError error = new CommandError(); \r
if (dataObjects.get("search").hasApplicationError()) {\r
error = dataObjects.get("search").getApplicationError(); \r
}\r
\r
\r
- private boolean hasTargetFilter(TargetFilter targetFilter) {\r
+ protected boolean hasTargetFilter(TargetFilter targetFilter) {\r
return hasTargetFilter() && targetFilter.equals(this.targetFilter);\r
}\r
\r
- private boolean hasQuery() {\r
+ protected boolean hasQuery() {\r
return !(getCommand("search").getParameter("query") == null);\r
}\r
\r
return errorHelper;\r
}\r
\r
- private void handleQueryStateChanges (String commands) {\r
+ protected void handleQueryStateChanges (String commands) {\r
if (queryStates.hasPendingStateChange("search")) { \r
logger.debug("Found pending search change. Doing search before updating " + commands);\r
doSearch();\r
} \r
}\r
\r
- private String getActiveClients() { \r
+ protected String getActiveClients() { \r
if (getShow()!=null) {\r
logger.debug("Active clients: "+getShow().getActiveClients());\r
return getShow().getActiveClients();\r
}\r
}\r
\r
- private Pazpar2Command getCommand(String name) {\r
+ protected Pazpar2Command getCommand(String name) {\r
return queryStates.getCurrentState().getCommand(name);\r
}\r
\r
- private void setCommandParameter(String commandName, CommandParameter parameter) {\r
+ protected void setCommandParameter(String commandName, CommandParameter parameter) {\r
logger.debug("Setting parameter for " + commandName + ": " + parameter);\r
queryStates.getCurrentState().setCommandParameter(commandName, parameter, queryStates); \r
}\r
\r
\r
- private void removeCommandParameter(String commandName, String parameterName) {\r
+ protected void removeCommandParameter(String commandName, String parameterName) {\r
queryStates.getCurrentState().removeCommandParameter(commandName,parameterName,queryStates); \r
}\r
\r
- private void removeCommand (String commandName) {\r
+ protected void removeCommand (String commandName) {\r
queryStates.getCurrentState().removeCommand(commandName, queryStates);\r
}\r
\r
- private String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { \r
+ protected String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { \r
Pazpar2Command command = getCommand(commandName);\r
if (command != null) {\r
CommandParameter parameter = command.getParameter(parameterName);\r
return defaultValue; \r
}\r
\r
- private String getCommandParameterValueSimple (String commandName, String parameterName, String defaultValue) { \r
+ protected String getCommandParameterValueSimple (String commandName, String parameterName, String defaultValue) { \r
Pazpar2Command command = getCommand(commandName);\r
if (command != null) {\r
CommandParameter parameter = command.getParameter(parameterName);\r
}\r
\r
\r
- private int getCommandParameterValue (String commandName, String parameterName, int defaultValue) {\r
+ protected int getCommandParameterValue (String commandName, String parameterName, int defaultValue) {\r
Pazpar2Command command = getCommand(commandName);\r
if (command != null) {\r
CommandParameter parameter = command.getParameter(parameterName);\r
return defaultValue; \r
}\r
\r
- private String doCommand(String commandName) { \r
+ protected 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
+ protected void resetDataObjects() {\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("search", new SearchResponse());\r
}\r
\r
-\r
}\r