package com.indexdata.pz2utils4jsf.pazpar2;\r
\r
import java.io.Serializable;\r
+import java.util.ArrayList;\r
import java.util.List;\r
+import java.util.StringTokenizer;\r
\r
import javax.annotation.PostConstruct;\r
import javax.enterprise.context.SessionScoped;\r
+import javax.enterprise.inject.Alternative;\r
import javax.inject.Inject;\r
import javax.inject.Named;\r
\r
import org.apache.log4j.Logger;\r
\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.pazpar2.data.ByTarget;\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationError;\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.commands.CommandParameter;\r
+import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Commands;\r
+import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData;\r
+import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseParser;\r
+import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Responses;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse;\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.StateListener;\r
+import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager;\r
import com.indexdata.pz2utils4jsf.utils.Utils;\r
\r
-@Named("pz2") @SessionScoped\r
-public class Pz2Bean implements Pz2Interface, Serializable {\r
+@Named("pz2") @SessionScoped @Alternative\r
+public class Pz2Bean implements Pz2Interface, StateListener, Serializable {\r
\r
private static final long serialVersionUID = 3440277287081557861L;\r
private static Logger logger = Logger.getLogger(Pz2Bean.class);\r
\r
- Pz2Session pz2; \r
- @Inject Pz2Configurator pz2conf;\r
+ protected SearchClient searchClient = null;\r
\r
+ @Inject ConfigurationReader configurator;\r
+ @Inject StateManager stateMgr;\r
+ @Inject Pazpar2Commands req;\r
+ @Inject Pazpar2Responses data;\r
+ \r
+ protected ResultsPager pager = null; \r
+\r
+ protected List<ErrorInterface> configurationErrors = null;\r
+ protected ErrorHelper errorHelper = null;\r
+ \r
public Pz2Bean () {\r
- logger.info("Instantiating pz2 bean [" + Utils.objectId(this) + "]");\r
+ logger.info("Instantiating pz2 bean [" + Utils.objectId(this) + "]"); \r
}\r
\r
@PostConstruct\r
- public void initiatePz2Session() {\r
- logger.debug(Utils.objectId(this) + " will instantiate a Pz2Session next.");\r
- pz2 = new Pz2Session();\r
- logger.debug(Utils.objectId(this) + " will forward configuration to the new Pz2Session [" + Utils.objectId(pz2) + "]");\r
- pz2.init(pz2conf);\r
- }\r
+ public void postConstruct() { \r
+ logger.debug("in start of Pz2Bean post-construct configurator is " + configurator);\r
+ logger.debug(Utils.objectId(this) + " will instantiate a Pz2Client next.");\r
+ searchClient = new Pz2Client();\r
+ logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" \r
+ + Utils.objectId(configurator) + "]" ); \r
+ configureClient(searchClient,configurator);\r
+ stateMgr.addStateListener(this);\r
+ } \r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#doSearch(java.lang.String)\r
- */\r
- public void doSearch(String query) { \r
- pz2.doSearch(query);\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#doSearch()\r
- */\r
- public void doSearch() {\r
- logger.info(Utils.objectId(this) + " doing search for "+this.getQuery());\r
- pz2.doSearch();\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#update()\r
- */\r
- public String update() {\r
- return pz2.update();\r
+ public void configureClient(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
+ //\r
+ // Commented as I'm trying with regular instantiation instead\r
+ // this.searchClient = searchClient.cloneMe(); \r
+ } catch (ConfigurationException e) {\r
+ configurationErrors.add(new ConfigurationError("Search Client","Configuration",e.getMessage(),new ErrorHelper(configReader))); \r
+ } \r
+ logger.info(configReader.document());\r
+ data.reset(); \r
}\r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#update(java.lang.String)\r
- */\r
- public String update(String commands) {\r
- return pz2.update(commands);\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setQuery(java.lang.String)\r
- */\r
- public void setQuery(String query) {\r
- pz2.setQuery(query);\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getQuery()\r
- */\r
- public String getQuery() {\r
- return pz2.getQuery();\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setFacet(java.lang.String, java.lang.String)\r
- */\r
- public void setFacet(String facetKey, String term) {\r
- pz2.setFacet(facetKey, term);\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#removeFacet(java.lang.String, java.lang.String)\r
- */\r
- public void removeFacet(String facetKey, String term) {\r
- pz2.removeFacet(facetKey, term);\r
+ \r
+ public void doSearch(String query) {\r
+ req.getSearch().setParameter(new CommandParameter("query","=",query)); \r
+ doSearch();\r
}\r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setFacetOnQuery(java.lang.String, java.lang.String)\r
- */\r
- public void setFacetOnQuery(String facetKey, String term) {\r
- pz2.setFacetOnQuery(facetKey, term);\r
+ public void doSearch() { \r
+ stateMgr.hasPendingStateChange("search",false);\r
+ data.reset();\r
+ // TODO: avoid state proliferation here:\r
+ req.getRecord().removeParameters();\r
+ req.getShow().setParameter(new CommandParameter("start","=",0)); \r
+ logger.debug(Utils.objectId(this) + " is searching using "+req.getCommandReadOnly("search").getUrlEncodedParameterValue("query"));\r
+ doCommand("search"); \r
+ }\r
+ \r
+ /**\r
+ * Refreshes 'show', 'stat', 'termlist', and 'bytarget' data object from pazpar2\r
+ * \r
+ * @return Number of activeclients at the time of the 'show' command.\r
+ */\r
+ public String update () {\r
+ logger.debug("Updating show,stat,termlist,bytarget from pazpar2");\r
+ return update("show,stat,termlist,bytarget");\r
+ }\r
+ \r
+ /**\r
+ * Refreshes the data objects listed in 'commands' from pazpar2\r
+ * \r
+ * @param commands\r
+ * @return Number of activeclients at the time of the 'show' command\r
+ */\r
+ public String update (String commands) {\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(req.getCommandReadOnly(tokens.nextToken()),searchClient)); \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
+ }\r
+ }\r
+ for (CommandThread thread : threadList) {\r
+ String commandName = thread.getCommand().getName();\r
+ String response = thread.getResponse();\r
+ logger.debug("Response was: " + response);\r
+ Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response);\r
+ data.put(commandName, responseObject); \r
+ }\r
+ if (commands.equals("record")) {\r
+ logger.debug("Record: Active clients: "+data.getRecord().getActiveClients());\r
+ return data.getRecord().getActiveClients();\r
+ } else {\r
+ return data.getActiveClients();\r
+ } \r
+ } else {\r
+ logger.debug("Skipped requests for " + commands + " as there's not yet a query."); \r
+ data.reset();\r
+ return "0";\r
+ }\r
+ } else {\r
+ logger.error("Did not attempt to execute query since there are configuration errors.");\r
+ return "0";\r
+ }\r
+ \r
}\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setTargetFilter(java.lang.String, java.lang.String)\r
- */\r
- public void setTargetFilter(String targetId, String targetName) {\r
- pz2.setTargetFilter(targetId, targetName);\r
+ \r
+ public String toggleRecord (String recId) {\r
+ if (hasRecord(recId)) {\r
+ req.getRecord().removeParameters(); \r
+ data.put("record", new RecordResponse());\r
+ return "";\r
+ } else {\r
+ req.getRecord().setRecordId(recId);\r
+ return doCommand("record");\r
+ }\r
}\r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#removeTargetFilter()\r
- */\r
- public void removeTargetFilter () {\r
- pz2.removeTargetFilter();\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getTargetFilter()\r
- */\r
- public TargetFilter getTargetFilter() {\r
- return pz2.getTargetFilter();\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#hasTargetFilter()\r
- */\r
- public boolean hasTargetFilter() {\r
- return pz2.hasTargetFilter();\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setSort(java.lang.String)\r
- */\r
- public void setSort(String sortOption) {\r
- pz2.setSort(sortOption);\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getSort()\r
- */\r
- public String getSort() {\r
- return pz2.getSort();\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setPageSize(int)\r
- */\r
- public void setPageSize(int perPageOption) {\r
- pz2.setPageSize(perPageOption);\r
+ @Override\r
+ public boolean hasRecord (String recId) {\r
+ return req.getCommandReadOnly("record").hasParameters() && data.getRecord().getRecId().equals(recId);\r
}\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getPageSize()\r
- */\r
- public int getPageSize() {\r
- return pz2.getPageSize();\r
+ \r
+ public String getCurrentStateKey () { \r
+ return stateMgr.getCurrentState().getKey();\r
}\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setStart(int)\r
- */\r
- public void setStart(int start) {\r
- pz2.setStart(start);\r
+ \r
+ public void setCurrentStateKey(String key) { \r
+ stateMgr.setCurrentStateKey(key);\r
}\r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getStart()\r
- */\r
- public int getStart() {\r
- return pz2.getStart();\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#toggleRecord(java.lang.String)\r
- */\r
- public String toggleRecord(String recid) {\r
- return pz2.toggleRecord(recid);\r
+ public boolean hasConfigurationErrors () {\r
+ return (configurationErrors.size()>0); \r
}\r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getShow()\r
- */\r
- public ShowResponse getShow() {\r
- return pz2.getShow();\r
+ public boolean hasCommandErrors () {\r
+ return data.hasApplicationError();\r
}\r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getStat()\r
- */\r
- public StatResponse getStat() {\r
- return pz2.getStat();\r
- }\r
- \r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#hasRecord(java.lang.String)\r
+ /**\r
+ * Returns true if application error found in any response data objects \r
*/\r
- public boolean hasRecord(String recId) { \r
- return pz2.hasRecord(recId);\r
+ public boolean hasErrors () {\r
+ return hasConfigurationErrors() || hasCommandErrors();\r
}\r
- \r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getRecord()\r
- */\r
- public RecordResponse getRecord() {\r
- return pz2.getRecord();\r
+\r
+ public List<ErrorInterface> getConfigurationErrors() { \r
+ return configurationErrors;\r
}\r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getTermLists()\r
- */\r
- public TermListsResponse getTermLists() {\r
- return pz2.getTermLists();\r
- }\r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getFacetTerms(java.lang.String, int)\r
- */\r
- public List<TermResponse> getFacetTerms(String facet, int count) {\r
- return pz2.getFacetTerms(facet, count);\r
+ protected boolean hasQuery() { \r
+ return req.getSearch().getParameter("query") != null && req.getSearch().getParameter("query").getValueWithExpressions().length()>0;\r
}\r
- \r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getFacetTerms(java.lang.String)\r
- */\r
- public List<TermResponse> getFacetTerms(String facet) { \r
- return pz2.getFacetTerms(facet);\r
- } \r
- \r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getByTarget()\r
- */\r
- public ByTarget getByTarget() { \r
- return pz2.getByTarget();\r
+ \r
+ \r
+ public ResultsPager getPager () {\r
+ if (pager == null) {\r
+ pager = new ResultsPager(data); \r
+ } \r
+ return pager; \r
}\r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#hasRecords()\r
- */\r
- public boolean hasRecords() {\r
- return pz2.hasRecords();\r
+ public ResultsPager setPager (int pageRange) {\r
+ pager = new ResultsPager(data,pageRange,req);\r
+ return pager;\r
}\r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setPager(int)\r
- */\r
- public ResultsPager setPager(int pageRange) {\r
- return pz2.setPager(pageRange);\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getPager()\r
- */\r
- public ResultsPager getPager() {\r
- return pz2.getPager();\r
+ protected ErrorHelper getTroubleshooter() {\r
+ return errorHelper;\r
}\r
\r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getCurrentStateKey()\r
- */\r
- public String getCurrentStateKey() {\r
- return pz2.getCurrentStateKey();\r
- }\r
- \r
- /* (non-Javadoc)\r
- * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setCurrentStateKey(java.lang.String)\r
- */\r
- public void setCurrentStateKey(String key) {\r
- pz2.setCurrentStateKey(key); \r
+ protected void handleQueryStateChanges (String commands) {\r
+ if (stateMgr.hasPendingStateChange("search") && hasQuery()) { \r
+ logger.debug("Found pending search change. Doing search before updating " + commands); \r
+ doSearch();\r
+ } \r
+ if (stateMgr.hasPendingStateChange("record") && ! commands.equals("record")) { \r
+ logger.debug("Found pending record ID change. Doing record before updating " + commands);\r
+ stateMgr.hasPendingStateChange("record",false);\r
+ if (req.getCommandReadOnly("record").hasParameters()) {\r
+ update("record");\r
+ } else { \r
+ data.put("record", new RecordResponse());\r
+ }\r
+ }\r
}\r
\r
- public boolean hasErrors() {\r
- return pz2.hasErrors();\r
- }\r
- \r
- public ApplicationError getCommandError() {\r
- return pz2.getCommandError();\r
+ protected String doCommand(String commandName) { \r
+ logger.debug(req.getCommandReadOnly(commandName).getEncodedQueryString() + ": Results for "+ req.getCommandReadOnly("search").getEncodedQueryString());\r
+ return update(commandName);\r
}\r
\r
- public List<ApplicationError> getConfigurationErrors () {\r
- return pz2.getConfigurationErrors();\r
- }\r
-\r
@Override\r
- public boolean hasCommandErrors() {\r
- return pz2.hasCommandErrors();\r
+ public void stateUpdated(String commandName) {\r
+ logger.debug("State change reported for [" + commandName + "]");\r
+ if (commandName.equals("show")) {\r
+ logger.debug("Updating show");\r
+ update(commandName);\r
+ } \r
}\r
\r
- @Override\r
- public boolean hasConfigurationErrors() {\r
- return pz2.hasConfigurationErrors();\r
- }\r
\r
}\r