X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fpz2utils4jsf%2Fpazpar2%2FPz2Session.java;h=ed4c0880e867debb45f15e6d737e26830a1cc2fe;hb=33338bd223ec711264392b7768d89d0f3fbce677;hp=f35523e3ded79f9ac7eb8584622366b2fe523c2d;hpb=edf26614e68f0c3f17d245c1f00f01667fe6ac05;p=mkjsf-moved-to-github.git diff --git a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java index f35523e..ed4c088 100644 --- a/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java +++ b/src/main/java/com/indexdata/pz2utils4jsf/pazpar2/Pz2Session.java @@ -13,6 +13,7 @@ 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; @@ -35,20 +36,20 @@ public class Pz2Session implements Pz2Interface { private static final long serialVersionUID = 3947514708343320514L; private static Logger logger = Logger.getLogger(Pz2Session.class); - private Map dataObjects = new ConcurrentHashMap(); - private QueryStates queryStates = new QueryStates(); - private ErrorHelper errorHelper = null; + protected Map dataObjects = new ConcurrentHashMap(); + protected QueryStates queryStates = new QueryStates(); + protected ErrorHelper errorHelper = null; - private List configurationErrors = null; - private SearchClient searchClient = null; - private TargetFilter targetFilter = null; - private ResultsPager pager = null; + protected List configurationErrors = null; + protected SearchClient searchClient = null; + protected SingleTargetFilter singleTargetFilter = null; + protected ResultsPager pager = null; public Pz2Session () { logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]"); } - public void init(SearchClient searchClient, ConfigurationReader configReader) { + 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"); @@ -61,16 +62,14 @@ public class Pz2Session implements Pz2Interface { // To avoid that, a context free client is cloned from the context // dependent one. // If propagation to threads gets supported, the cloning can go. - this.searchClient = searchClient.cloneMe(); - + this.searchClient = searchClient.cloneMe(); } catch (ConfigurationException e) { - // TODO: set errors - logger.info("Found " + configurationErrors.size() + " configuration errors"); + configurationErrors.add(new ConfigurationError("Search Client","Configuration",e.getMessage(),new ErrorHelper(configReader))); } logger.info(configReader.document()); resetDataObjects(); } - + public void doSearch(String query) { setCommandParameter("search",new CommandParameter("query","=",query)); doSearch(); @@ -79,6 +78,7 @@ public class Pz2Session implements Pz2Interface { public void doSearch() { queryStates.hasPendingStateChange("search",false); resetDataObjects(); + removeCommand("record"); setCommandParameter("show",new CommandParameter("start","=",0)); logger.debug(Utils.objectId(this) + " is searching using "+getCommand("search").getParameter("query").getEncodedQueryString()); doCommand("search"); @@ -127,7 +127,12 @@ public class Pz2Session implements Pz2Interface { Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response); dataObjects.put(commandName, responseObject); } - return getActiveClients(); + if (commands.equals("record")) { + logger.debug("Record: Active clients: "+getRecord().getActiveClients()); + return getRecord().getActiveClients(); + } else { + return getActiveClients(); + } } else { logger.debug("Skipped requests for " + commands + " as there's not yet a query."); resetDataObjects(); @@ -169,29 +174,29 @@ public class Pz2Session implements Pz2Interface { doSearch(); } - public void setTargetFilter (String targetId, String targetName) { - if (hasTargetFilter(new TargetFilter(targetId,targetName))) { - logger.debug("Already using target filter " + this.targetFilter.getFilterExpression()); + public void setSingleTargetFilter (String targetId, String targetName) { + if (hasSingleTargetFilter(new SingleTargetFilter(targetId,targetName))) { + logger.debug("Already using target filter " + this.singleTargetFilter.getFilterExpression()); } else { - this.targetFilter = new TargetFilter(targetId,targetName); - setCommandParameter("search",new CommandParameter("filter","=",this.targetFilter.getFilterExpression())); + this.singleTargetFilter = new SingleTargetFilter(targetId,targetName); + setCommandParameter("search",new CommandParameter("filter","=",this.singleTargetFilter.getFilterExpression())); doSearch(); } } - public TargetFilter getTargetFilter () { - return targetFilter; + public SingleTargetFilter getSingleTargetFilter () { + return singleTargetFilter; } - public void removeTargetFilter () { - logger.debug("Removing target filter " + targetFilter.getFilterExpression()); - this.targetFilter = null; + public void removeSingleTargetFilter () { + logger.debug("Removing target filter " + singleTargetFilter.getFilterExpression()); + this.singleTargetFilter = null; removeCommandParameter("search","filter"); doSearch(); } - public boolean hasTargetFilter() { - return targetFilter != null; + public boolean hasSingleTargetFilter() { + return singleTargetFilter != null; } public void setSort (String sortOption) { @@ -228,22 +233,29 @@ public class Pz2Session implements Pz2Interface { public int getStart() { return getCommandParameterValue("show","start",0); } - + public String toggleRecord (String recId) { if (hasRecord(recId)) { removeCommand("record"); dataObjects.put("record", new RecordResponse()); return ""; } else { - return updateRecord(recId); + setRecordId(recId); + return doCommand("record"); } } - private String updateRecord(String recId) { - setCommandParameter("record",new CommandParameter("id","=",recId)); - return doCommand("record"); + @Override + public void setRecordId(String recId) { + setCommandParameter("record",new CommandParameter("id","=",recId)); + } + + @Override + public String getRecordId () { + return getCommandParameterValue("record","recid",""); } + @Override public boolean hasRecord (String recId) { return getCommand("record").hasParameters() && getRecord().getRecId().equals(recId); } @@ -336,11 +348,11 @@ public class Pz2Session implements Pz2Interface { } - private boolean hasTargetFilter(TargetFilter targetFilter) { - return hasTargetFilter() && targetFilter.equals(this.targetFilter); + protected boolean hasSingleTargetFilter(SingleTargetFilter targetFilter) { + return hasSingleTargetFilter() && targetFilter.equals(this.singleTargetFilter); } - private boolean hasQuery() { + protected boolean hasQuery() { return !(getCommand("search").getParameter("query") == null); } @@ -366,16 +378,16 @@ public class Pz2Session implements Pz2Interface { return errorHelper; } - private void handleQueryStateChanges (String commands) { + protected void handleQueryStateChanges (String commands) { if (queryStates.hasPendingStateChange("search")) { - logger.debug("Found pending search change. Doing search before updating " + commands); + logger.debug("Found pending search change. Doing search before updating " + commands); doSearch(); } if (queryStates.hasPendingStateChange("record") && ! commands.equals("record")) { logger.debug("Found pending record ID change. Doing record before updating " + commands); queryStates.hasPendingStateChange("record",false); if (getCommand("record").hasParameters()) { - updateRecord(getCommand("record").getParameter("id").getSimpleValue()); + update("record"); } else { removeCommand("record"); dataObjects.put("record", new RecordResponse()); @@ -383,7 +395,7 @@ public class Pz2Session implements Pz2Interface { } } - private String getActiveClients() { + protected String getActiveClients() { if (getShow()!=null) { logger.debug("Active clients: "+getShow().getActiveClients()); return getShow().getActiveClients(); @@ -392,25 +404,25 @@ public class Pz2Session implements Pz2Interface { } } - private Pazpar2Command getCommand(String name) { + protected Pazpar2Command getCommand(String name) { return queryStates.getCurrentState().getCommand(name); } - private void setCommandParameter(String commandName, CommandParameter parameter) { + protected void setCommandParameter(String commandName, CommandParameter parameter) { logger.debug("Setting parameter for " + commandName + ": " + parameter); queryStates.getCurrentState().setCommandParameter(commandName, parameter, queryStates); } - private void removeCommandParameter(String commandName, String parameterName) { + protected void removeCommandParameter(String commandName, String parameterName) { queryStates.getCurrentState().removeCommandParameter(commandName,parameterName,queryStates); } - private void removeCommand (String commandName) { + protected void removeCommand (String commandName) { queryStates.getCurrentState().removeCommand(commandName, queryStates); } - private String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { + protected String getCommandParameterValue (String commandName, String parameterName, String defaultValue) { Pazpar2Command command = getCommand(commandName); if (command != null) { CommandParameter parameter = command.getParameter(parameterName); @@ -421,7 +433,7 @@ public class Pz2Session implements Pz2Interface { return defaultValue; } - private String getCommandParameterValueSimple (String commandName, String parameterName, String defaultValue) { + protected String getCommandParameterValueSimple (String commandName, String parameterName, String defaultValue) { Pazpar2Command command = getCommand(commandName); if (command != null) { CommandParameter parameter = command.getParameter(parameterName); @@ -433,7 +445,7 @@ public class Pz2Session implements Pz2Interface { } - private int getCommandParameterValue (String commandName, String parameterName, int defaultValue) { + protected int getCommandParameterValue (String commandName, String parameterName, int defaultValue) { Pazpar2Command command = getCommand(commandName); if (command != null) { CommandParameter parameter = command.getParameter(parameterName); @@ -444,13 +456,13 @@ public class Pz2Session implements Pz2Interface { return defaultValue; } - private String doCommand(String commandName) { + protected String doCommand(String commandName) { Pazpar2Command command = getCommand(commandName); logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString()); - return update(commandName); + return update(commandName); } - private void resetDataObjects() { + protected void resetDataObjects() { logger.debug("Resetting show,stat,termlist,bytarget,search response objects."); dataObjects = new ConcurrentHashMap(); dataObjects.put("show", new ShowResponse()); @@ -460,5 +472,19 @@ public class Pz2Session implements Pz2Interface { dataObjects.put("record", new RecordResponse()); dataObjects.put("search", new SearchResponse()); } + + @Override + public void setFilter(String filterExpression) { + logger.debug("Setting filter to " + filterExpression); + setCommandParameter("search",new CommandParameter("filter","=",filterExpression)); + } + + public String getFilter() { + return getCommandParameterValueSimple("search", "filter", ""); + } + + public boolean hasFilter () { + return getFilter().length()>0; + } }