X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2FPz2Bean.java;h=65706a9fd7d445d8450698c5296819d217ce9f21;hb=7b71381e71db35eed511606e80c0d3e18cab4d2b;hp=a8d14cc8a4a6ef28ba7e730cb3d5c1820d7eeff4;hpb=aa4634e1a3c9eec2103b299dda20d916dcba8b20;p=mkjsf-moved-to-github.git diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java index a8d14cc..65706a9 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Bean.java @@ -1,7 +1,9 @@ package com.indexdata.pz2utils4jsf.pazpar2; import java.io.Serializable; +import java.util.ArrayList; import java.util.List; +import java.util.StringTokenizer; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; @@ -13,293 +15,247 @@ import org.apache.log4j.Logger; import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.controls.ResultsPager; +import com.indexdata.pz2utils4jsf.errors.ConfigurationError; +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; +import com.indexdata.pz2utils4jsf.errors.ErrorHelper; import com.indexdata.pz2utils4jsf.errors.ErrorInterface; -import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; +import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandParameter; +import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Commands; +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData; +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseParser; +import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2Responses; import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse; -import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse; -import com.indexdata.pz2utils4jsf.pazpar2.data.StatResponse; -import com.indexdata.pz2utils4jsf.pazpar2.data.TermListsResponse; -import com.indexdata.pz2utils4jsf.pazpar2.data.TermResponse; +import com.indexdata.pz2utils4jsf.pazpar2.state.StateListener; +import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager; import com.indexdata.pz2utils4jsf.utils.Utils; @Named("pz2") @SessionScoped @Alternative -public class Pz2Bean implements Pz2Interface, Serializable { +public class Pz2Bean implements Pz2Interface, StateListener, Serializable { private static final long serialVersionUID = 3440277287081557861L; private static Logger logger = Logger.getLogger(Pz2Bean.class); + protected SearchClient searchClient = null; + @Inject ConfigurationReader configurator; - protected Pz2Session pz2; - protected SearchClient searchClient; - + @Inject StateManager stateMgr; + @Inject Pazpar2Commands pzreq; + @Inject Pazpar2Responses pzresp; + + protected ResultsPager pager = null; + + protected List configurationErrors = null; + protected ErrorHelper errorHelper = null; + public Pz2Bean () { - logger.info("Instantiating pz2 bean [" + Utils.objectId(this) + "]"); + logger.info("Instantiating pz2 bean [" + Utils.objectId(this) + "]"); } @PostConstruct - public void initiatePz2Session() { - logger.debug(Utils.objectId(this) + " will instantiate a Pz2Session next."); - pz2 = new Pz2Session(); + public void postConstruct() { + logger.debug("in start of Pz2Bean post-construct configurator is " + configurator); + logger.debug(Utils.objectId(this) + " will instantiate a Pz2Client next."); searchClient = new Pz2Client(); logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" - + Utils.objectId(configurator) + "] on session [" - + Utils.objectId(pz2) + "]" ); - pz2.init(searchClient,configurator); - } - + + Utils.objectId(configurator) + "]" ); + configureClient(searchClient,configurator); + stateMgr.addStateListener(this); + } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#doSearch(java.lang.String) - */ - public void doSearch(String query) { - pz2.doSearch(query); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#doSearch() - */ - public void doSearch() { - logger.info(Utils.objectId(this) + " doing search for "+this.getQuery()); - pz2.doSearch(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#update() - */ - public String update() { - return pz2.update(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#update(java.lang.String) - */ - public String update(String commands) { - return pz2.update(commands); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setQuery(java.lang.String) - */ - public void setQuery(String query) { - pz2.setQuery(query); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getQuery() - */ - public String getQuery() { - return pz2.getQuery(); + public void configureClient(SearchClient searchClient, ConfigurationReader configReader) { + configurationErrors = new ArrayList(); + errorHelper = new ErrorHelper(configReader); + logger.debug(Utils.objectId(this) + " will configure search client for the session"); + try { + searchClient.configure(configReader); + // At the time of writing this search client is injected using Weld. + // However, the client is used for asynchronously sending off requests + // to the server AND propagation of context to threads is currently + // not supported. Trying to do so throws a WELD-001303 error. + // To avoid that, a context free client is cloned from the context + // dependent one. + // If propagation to threads gets supported, the cloning can go. + // + // Commented as I'm trying with regular instantiation instead + // this.searchClient = searchClient.cloneMe(); + } catch (ConfigurationException e) { + configurationErrors.add(new ConfigurationError("Search Client","Configuration",e.getMessage(),new ErrorHelper(configReader))); + } + logger.info(configReader.document()); + pzresp.reset(); } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setFacet(java.lang.String, java.lang.String) - */ - public void setFacet(String facetKey, String term) { - pz2.setFacet(facetKey, term); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#removeFacet(java.lang.String, java.lang.String) - */ - public void removeFacet(String facetKey, String term) { - pz2.removeFacet(facetKey, term); + + public void doSearch(String query) { + pzreq.getSearch().setParameter(new CommandParameter("query","=",query)); + doSearch(); } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setFacetOnQuery(java.lang.String, java.lang.String) - */ - public void setFacetOnQuery(String facetKey, String term) { - pz2.setFacetOnQuery(facetKey, term); + public void doSearch() { + stateMgr.hasPendingStateChange("search",false); + pzresp.reset(); + // TODO: avoid state proliferation here: + pzreq.getRecord().removeParameters(); + pzreq.getShow().setParameter(new CommandParameter("start","=",0)); + logger.debug(Utils.objectId(this) + " is searching using "+pzreq.getCommandReadOnly("search").getUrlEncodedParameterValue("query")); + doCommand("search"); + } + + /** + * Refreshes 'show', 'stat', 'termlist', and 'bytarget' data object from pazpar2 + * + * @return Number of activeclients at the time of the 'show' command. + */ + public String update () { + logger.debug("Updating show,stat,termlist,bytarget from pazpar2"); + return update("show,stat,termlist,bytarget"); + } + + /** + * Refreshes the data objects listed in 'commands' from pazpar2 + * + * @param commands + * @return Number of activeclients at the time of the 'show' command + */ + public String update (String commands) { + 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(pzreq.getCommandReadOnly(tokens.nextToken()),searchClient)); + } + 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(); + String response = thread.getResponse(); + logger.debug("Response was: " + response); + Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response); + pzresp.put(commandName, responseObject); + } + if (commands.equals("record")) { + logger.debug("Record: Active clients: "+pzresp.getRecord().getActiveClients()); + return pzresp.getRecord().getActiveClients(); + } else { + return pzresp.getActiveClients(); + } + } else { + logger.debug("Skipped requests for " + commands + " as there's not yet a query."); + pzresp.reset(); + return "0"; + } + } else { + logger.error("Did not attempt to execute query since there are configuration errors."); + return "0"; + } + } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setTargetFilter(java.lang.String, java.lang.String) - */ - public void setTargetFilter(String targetId, String targetName) { - pz2.setTargetFilter(targetId, targetName); + + public String toggleRecord (String recId) { + if (hasRecord(recId)) { + pzreq.getRecord().removeParameters(); + pzresp.put("record", new RecordResponse()); + return ""; + } else { + pzreq.getRecord().setId(recId); + return doCommand("record"); + } } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#removeTargetFilter() - */ - public void removeTargetFilter () { - pz2.removeTargetFilter(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getTargetFilter() - */ - public TargetFilter getTargetFilter() { - return pz2.getTargetFilter(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#hasTargetFilter() - */ - public boolean hasTargetFilter() { - return pz2.hasTargetFilter(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setSort(java.lang.String) - */ - public void setSort(String sortOption) { - pz2.setSort(sortOption); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getSort() - */ - public String getSort() { - return pz2.getSort(); + @Override + public boolean hasRecord (String recId) { + return pzreq.getCommandReadOnly("record").hasParameters() && pzresp.getRecord().getRecId().equals(recId); } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setPageSize(int) - */ - public void setPageSize(int perPageOption) { - pz2.setPageSize(perPageOption); + + public String getCurrentStateKey () { + return stateMgr.getCurrentState().getKey(); } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getPageSize() - */ - public int getPageSize() { - return pz2.getPageSize(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setStart(int) - */ - public void setStart(int start) { - pz2.setStart(start); + + public void setCurrentStateKey(String key) { + stateMgr.setCurrentStateKey(key); } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getStart() - */ - public int getStart() { - return pz2.getStart(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#toggleRecord(java.lang.String) - */ - public String toggleRecord(String recid) { - return pz2.toggleRecord(recid); + public boolean hasConfigurationErrors () { + return (configurationErrors.size()>0); } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getShow() - */ - public ShowResponse getShow() { - return pz2.getShow(); + public boolean hasCommandErrors () { + return pzresp.hasApplicationError(); } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getStat() - */ - public StatResponse getStat() { - return pz2.getStat(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#hasRecord(java.lang.String) + /** + * Returns true if application error found in any response data objects */ - public boolean hasRecord(String recId) { - return pz2.hasRecord(recId); + public boolean hasErrors () { + return hasConfigurationErrors() || hasCommandErrors(); } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getRecord() - */ - public RecordResponse getRecord() { - return pz2.getRecord(); + + public List getConfigurationErrors() { + return configurationErrors; } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getTermLists() - */ - public TermListsResponse getTermLists() { - return pz2.getTermLists(); - } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getFacetTerms(java.lang.String, int) - */ - public List getFacetTerms(String facet, int count) { - return pz2.getFacetTerms(facet, count); + protected boolean hasQuery() { + return pzreq.getCommandReadOnly("search").hasParameterSet("query"); } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getFacetTerms(java.lang.String) - */ - public List getFacetTerms(String facet) { - return pz2.getFacetTerms(facet); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getByTarget() - */ - public ByTarget getByTarget() { - return pz2.getByTarget(); + + + public ResultsPager getPager () { + if (pager == null) { + pager = new ResultsPager(pzresp); + } + return pager; } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#hasRecords() - */ - public boolean hasRecords() { - return pz2.hasRecords(); + public ResultsPager setPager (int pageRange) { + pager = new ResultsPager(pzresp,pageRange,pzreq); + return pager; } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setPager(int) - */ - public ResultsPager setPager(int pageRange) { - return pz2.setPager(pageRange); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getPager() - */ - public ResultsPager getPager() { - return pz2.getPager(); + protected ErrorHelper getTroubleshooter() { + return errorHelper; } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getCurrentStateKey() - */ - public String getCurrentStateKey() { - return pz2.getCurrentStateKey(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setCurrentStateKey(java.lang.String) - */ - public void setCurrentStateKey(String key) { - pz2.setCurrentStateKey(key); + protected void handleQueryStateChanges (String commands) { + if (stateMgr.hasPendingStateChange("search") && hasQuery()) { + logger.debug("Found pending search change. Doing search before updating " + commands); + doSearch(); + } + if (stateMgr.hasPendingStateChange("record") && ! commands.equals("record")) { + logger.debug("Found pending record ID change. Doing record before updating " + commands); + stateMgr.hasPendingStateChange("record",false); + if (pzreq.getCommandReadOnly("record").hasParameters()) { + update("record"); + } else { + pzresp.put("record", new RecordResponse()); + } + } } - public boolean hasErrors() { - return pz2.hasErrors(); - } - - public ErrorInterface getCommandError() { - return pz2.getCommandError(); + protected String doCommand(String commandName) { + logger.debug(pzreq.getCommandReadOnly(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommandReadOnly("search").getEncodedQueryString()); + return update(commandName); } - public List getConfigurationErrors () { - return pz2.getConfigurationErrors(); - } - @Override - public boolean hasCommandErrors() { - return pz2.hasCommandErrors(); + public void stateUpdated(String commandName) { + logger.debug("State change reported for [" + commandName + "]"); + if (commandName.equals("show")) { + logger.debug("Updating show"); + update(commandName); + } } - @Override - public boolean hasConfigurationErrors() { - return pz2.hasConfigurationErrors(); - } }