X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2FPz2Bean.java;h=b12082b0b5ec0d5ee6079b792dd2ecfef5752f69;hb=130dfe23d607762588d449967bd6b074542a36fd;hp=82f71ed08e7688abc7e39428552820c8ef5c39bb;hpb=437baaa9cfedf7b5abe9dd66b15a531949121136;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 82f71ed..b12082b 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,316 +15,232 @@ import org.apache.log4j.Logger; import com.indexdata.pz2utils4jsf.config.ConfigurationReader; import com.indexdata.pz2utils4jsf.controls.ResultsPager; -import com.indexdata.pz2utils4jsf.errors.ErrorInterface; -import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget; +import com.indexdata.pz2utils4jsf.errors.ConfigurationError; +import com.indexdata.pz2utils4jsf.errors.ConfigurationException; +import com.indexdata.pz2utils4jsf.errors.ErrorHelper; +import com.indexdata.pz2utils4jsf.errors.ErrorCentral; +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); + private static Logger responseLogger = Logger.getLogger("com.indexdata.pz2utils4jsf.pazpar2.responses"); + + protected SearchClient searchClient = null; @Inject ConfigurationReader configurator; + @Inject StateManager stateMgr; + @Inject Pazpar2Commands pzreq; + @Inject Pazpar2Responses pzresp; + @Inject ErrorCentral errors; - @Inject @ForStraightPz2 Pz2Session pz2; + protected ResultsPager pager = null; + - protected SearchClient searchClient; - + 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 instantiatePz2SessionObject() { + 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.configureClient(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 "+pz2.getCommandReadOnly("search").getParameterValue("query")); - 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); + public void configureClient(SearchClient searchClient, ConfigurationReader configReader) { + logger.debug(Utils.objectId(this) + " will configure search client for the session"); + try { + searchClient.configure(configReader); + } catch (ConfigurationException e) { + logger.debug("Pz2Bean adding configuration error"); + errors.addConfigurationError(new ConfigurationError("Search Client","Configuration",e.getMessage())); + } + logger.info(configReader.document()); + pzresp.reset(); } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setQuery(java.lang.String) - */ - public void setQuery(String query) { - pz2.req.getSearch().setQuery(query); - } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getQuery() - */ - /* - public String getQuery() { - return pz2.getQuery(); - } - */ - - /* (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); - } - - /* (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); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setTargetFilter(java.lang.String, java.lang.String) - */ - public void setSingleTargetFilter(String targetId, String targetName) { - pz2.setSingleTargetFilter(targetId, targetName); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getTargetFilter() - */ - public SingleTargetFilter getSingleTargetFilter() { - return pz2.getSingleTargetFilter(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#removeTargetFilter() - */ - public void removeSingleTargetFilter () { - pz2.removeSingleTargetFilter(); + public void doSearch(String query) { + pzreq.getSearch().setParameter(new CommandParameter("query","=",query)); + doSearch(); + } + + public void doSearch() { + stateMgr.hasPendingStateChange("search",false); + pzresp.reset(); + // resets some record and show command parameters without + // changing state or creating state change feedback + pzreq.getRecord().removeParametersInState(); + pzreq.getShow().setParameterInState(new CommandParameter("start","=",0)); + logger.debug(Utils.objectId(this) + " is searching using "+pzreq.getCommand("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 (! errors.hasConfigurationErrors()) { + if (commandsAreValid(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(pzreq.getCommand(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(); + responseLogger.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 attemt to run command(s) due to a validation error."); + 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#hasTargetFilter() - */ - public boolean hasSingleTargetFilter() { - return pz2.hasSingleTargetFilter(); + public boolean commandsAreValid(String commands) { + if (commands.equals("record")) { + if (!pzreq.getCommand("record").hasParameterSet("id")) { + logger.error("Attempt to send record command without the id parameter"); + return false; + } + } + return true; } - - - public String getFilter () { - return pz2.getFilter(); + + 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"); + } } - public void setFilter (String filterExpression) { - pz2.setFilter(filterExpression); - } - - - /* (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(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setPageSize(int) - */ - public void setPageSize(int perPageOption) { - pz2.setPageSize(perPageOption); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getPageSize() - */ - public int getPageSize() { - return pz2.getPageSize(); + @Override + public boolean hasRecord (String recId) { + return pzreq.getCommand("record").hasParameters() && pzresp.getRecord().getRecId().equals(recId); } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setStart(int) - */ - public void setStart(int start) { - pz2.setStart(start); + + public String getCurrentStateKey () { + return stateMgr.getCurrentState().getKey(); } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getStart() - */ - public int getStart() { - return pz2.getStart(); + + public void setCurrentStateKey(String key) { + stateMgr.setCurrentStateKey(key); } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#toggleRecord(java.lang.String) - */ - public String toggleRecord(String recid) { - return pz2.toggleRecord(recid); - } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getShow() - */ - public ShowResponse getShow() { - return pz2.getShow(); - } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getStat() - */ - public StatResponse getStat() { - return pz2.getStat(); + protected boolean hasQuery() { + return pzreq.getCommand("search").hasParameterSet("query"); } - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#hasRecord(java.lang.String) - */ - public boolean hasRecord(String recId) { - return pz2.hasRecord(recId); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getRecord() - */ - public RecordResponse getRecord() { - return pz2.getRecord(); - } - - /* (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); - } - - /* (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(); - } - - /* (non-Javadoc) - * @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#hasRecords() - */ - public boolean hasRecords() { - return pz2.hasRecords(); - } - - /* (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(); - } - - /* (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); + public ResultsPager getPager () { + if (pager == null) { + pager = new ResultsPager(pzresp); + } + return pager; } - public boolean hasErrors() { - return pz2.hasErrors(); + public ResultsPager setPager (int pageRange) { + pager = new ResultsPager(pzresp,pageRange,pzreq); + return pager; } - public ErrorInterface getCommandError() { - return pz2.getCommandError(); + 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.getCommand("record").hasParameterSet("id")) { + update("record"); + } else { + pzresp.put("record", new RecordResponse()); + } + } + } + + protected String doCommand(String commandName) { + logger.debug(pzreq.getCommand(commandName).getEncodedQueryString() + ": Results for "+ pzreq.getCommand("search").getEncodedQueryString()); + return update(commandName); } - public List getConfigurationErrors () { - return pz2.getConfigurationErrors(); - } - - @Override - public boolean hasCommandErrors() { - return pz2.hasCommandErrors(); - } - @Override - public boolean hasConfigurationErrors() { - return pz2.hasConfigurationErrors(); + public void stateUpdated(String commandName) { + logger.debug("State change reported for [" + commandName + "]"); + if (commandName.equals("show")) { + logger.debug("Updating show"); + update(commandName); + } } - @Override - public void setRecordId(String recId) { - pz2.setRecordId(recId); - } - @Override - public String getRecordId() { - return pz2.getRecordId(); - } - }