import org.apache.log4j.Logger;\r
\r
import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
-import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command;\r
+import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandReadOnly;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError;\r
\r
public class CommandThread extends Thread {\r
\r
private static Logger logger = Logger.getLogger(CommandThread.class);\r
- Pazpar2Command command;\r
+ CommandReadOnly command;\r
SearchClient client;\r
private ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
private StringBuilder response = new StringBuilder(""); \r
\r
- public CommandThread (Pazpar2Command command, SearchClient client) {\r
+ public CommandThread (CommandReadOnly command, SearchClient client) {\r
this.command = command;\r
this.client = client;\r
}\r
return response.toString();\r
}\r
\r
- public Pazpar2Command getCommand() {\r
+ public CommandReadOnly getCommand() {\r
return command;\r
}\r
\r
import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
import com.indexdata.pz2utils4jsf.controls.ResultsPager;\r
import com.indexdata.pz2utils4jsf.errors.ErrorInterface;\r
-import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.RecordResponse;\r
import com.indexdata.pz2utils4jsf.pazpar2.data.ShowResponse;\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
+ logger.info(Utils.objectId(this) + " doing search for "+pz2.getCommandReadOnly("search").getParameterValue("query"));\r
pz2.doSearch();\r
}\r
\r
/* (non-Javadoc)\r
* @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#getQuery()\r
*/\r
+ /*\r
public String getQuery() {\r
return pz2.getQuery();\r
}\r
+ */\r
\r
/* (non-Javadoc)\r
* @see com.indexdata.pz2utils4jsf.pazpar2.Pz2Interface#setFacet(java.lang.String, java.lang.String)\r
return pz2.getRecordId();\r
}\r
\r
- public Pazpar2Command getSearchCommand () {\r
- return pz2.getCommand("search");\r
- }\r
-\r
}\r
import com.indexdata.pz2utils4jsf.config.Configuration;\r
import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
import com.indexdata.pz2utils4jsf.errors.ConfigurationException;\r
-import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command;\r
+import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandReadOnly;\r
import com.indexdata.pz2utils4jsf.utils.Utils;\r
\r
@Named @SessionScoped \r
}\r
\r
@Override\r
- public void setSearchCommand(Pazpar2Command command) {\r
+ public void setSearchCommand(CommandReadOnly command) {\r
ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
client.setSearchCommand(clientCommand); \r
}\r
\r
@Override\r
- public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) \r
+ public CommandResponse executeCommand(CommandReadOnly command, ByteArrayOutputStream baos) \r
throws Pazpar2ErrorException, IOException {\r
ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString());\r
Pazpar2HttpResponse pz2HttpResponse = client.executeCommand(clientCommand, baos);\r
* @param query a query on pazpar2 query syntax\r
* \r
*/\r
- public void setQuery (String query);\r
+ //public void setQuery (String query);\r
+ \r
\r
/**\r
* Gets the current query \r
* @return a pazpar2 query string\r
*/\r
- public String getQuery ();\r
+ //public String getQuery ();\r
\r
/**\r
* Sets a facet to limit the current query by,\r
\r
import com.indexdata.pz2utils4jsf.config.Configuration;\r
import com.indexdata.pz2utils4jsf.config.ConfigurationReader;\r
+import com.indexdata.pz2utils4jsf.pazpar2.commands.SearchCommand;\r
import com.indexdata.pz2utils4jsf.pazpar2.sp.ForServiceProxy;\r
import com.indexdata.pz2utils4jsf.pazpar2.sp.ServiceProxyClient;\r
import com.indexdata.pz2utils4jsf.pazpar2.sp.ServiceProxyInterface;\r
session().setUser(user);\r
session().resetDataObjects();\r
session().removeCommand("record");\r
- session().setQuery(null);\r
+ ((SearchCommand)session().getCommand("search")).setQuery(null);\r
return session().login(navigateTo);\r
}\r
\r
public void setServiceProxyUrl(String url) {\r
logger.info("Setting Service Proxy url: " + url);\r
session().setServiceProxyUrl(url); \r
- session().setQuery(null);\r
+ ((SearchCommand)session().getCommand("search")).setQuery(null);\r
session().resetDataObjects();\r
}\r
\r
import java.util.concurrent.ConcurrentHashMap;\r
\r
import javax.annotation.PostConstruct;\r
-import javax.enterprise.context.SessionScoped;\r
import javax.inject.Inject;\r
\r
import org.apache.log4j.Logger;\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.CommandReadOnly;\r
import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command;\r
import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Commands;\r
+import com.indexdata.pz2utils4jsf.pazpar2.commands.SearchCommand;\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
resetDataObjects();\r
removeCommand("record");\r
setCommandParameter("show",new CommandParameter("start","=",0)); \r
- logger.debug(Utils.objectId(this) + " is searching using "+getCommand("search").getParameter("query").getEncodedQueryString());\r
+ logger.debug(Utils.objectId(this) + " is searching using "+req.getCommandReadOnly("search").getUrlEncodedParameterValue("query"));\r
doCommand("search"); \r
}\r
\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()),searchClient)); \r
+ threadList.add(new CommandThread(req.getCommandReadOnly(tokens.nextToken()),searchClient)); \r
}\r
for (CommandThread thread : threadList) {\r
thread.start();\r
\r
}\r
\r
+ /*\r
public void setQuery (String query) {\r
logger.debug("Creating new command parameter for " + query);\r
setCommandParameter("search",new CommandParameter("query","=",query));\r
public String getQuery () {\r
return getCommandParameterValueSimple("search","query",null);\r
}\r
+ */\r
\r
public void setFacet (String facetKey, String term) { \r
if (term != null && term.length()>0) { \r
- Pazpar2Command command = getCommand("search");\r
+ Pazpar2Command command = req.getCommand("search");\r
command.getParameter("query").addExpression(new Expression(facetKey,"=",term));\r
stateMgr.checkIn(command);\r
doSearch();\r
public void setFacetOnQuery (String facetKey, String term) {\r
String facetExpression = facetKey + "=" + term; \r
if (term != null && term.length()>0) {\r
- setCommandParameter("search",new CommandParameter("query","=", getQuery() + " and " + facetExpression));\r
+ String currentQuery= req.getCommandReadOnly("search").getParameterValue("query");\r
+ setCommandParameter("search",new CommandParameter("query","=", currentQuery + " and " + facetExpression));\r
doSearch(); \r
} \r
}\r
\r
public void removeFacet(String facetKey, String term) {\r
- Pazpar2Command command = getCommand("search");\r
+ SearchCommand command = req.getSearch();\r
command.getParameter("query").removeExpression(new Expression(facetKey,"=",term));\r
stateMgr.checkIn(command);\r
doSearch();\r
\r
@Override\r
public boolean hasRecord (String recId) {\r
- return getCommand("record").hasParameters() && getRecord().getRecId().equals(recId);\r
+ return req.getCommandReadOnly("record").hasParameters() && getRecord().getRecId().equals(recId);\r
}\r
\r
public ShowResponse getShow () {\r
return hasSingleTargetFilter() && targetFilter.equals(this.singleTargetFilter);\r
}\r
\r
- protected boolean hasQuery() {\r
- logger.debug("req is " + req);\r
+ protected boolean hasQuery() { \r
return req.getSearch().getParameter("query") != null && req.getSearch().getParameter("query").getValueWithExpressions().length()>0;\r
}\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 (getCommand("record").hasParameters()) {\r
+ if (req.getCommandReadOnly("record").hasParameters()) {\r
update("record");\r
} else {\r
removeCommand("record"); \r
* @return\r
*/\r
protected Pazpar2Command getCommand(String name) {\r
- return stateMgr.checkOut(name);\r
+ return req.getCommand(name);\r
}\r
\r
+ /** \r
+ * Returns an interface to a Pazpar2Command with only String getters.\r
+ * \r
+ * Since the command cannot be modified (unless it is cast) we can avoid \r
+ * cloning it before returning it from the current state. \r
+ * It can be used for log statements, checks and for performing the \r
+ * actual pazpar2 request. \r
+ * \r
+ * @param name\r
+ * @return\r
+ */\r
+ protected CommandReadOnly getCommandReadOnly(String name) {\r
+ return req.getCommandReadOnly(name);\r
+ }\r
+\r
+ \r
protected void setCommandParameter(String commandName, CommandParameter parameter) {\r
logger.debug("Setting parameter for " + commandName + ": " + parameter);\r
- Pazpar2Command command = getCommand(commandName);\r
+ Pazpar2Command command = req.getCommand(commandName);\r
command.setParameter(parameter);\r
stateMgr.checkIn(command); \r
}\r
\r
\r
protected void removeCommandParameter(String commandName, String parameterName) {\r
- Pazpar2Command command = getCommand(commandName);\r
+ Pazpar2Command command = req.getCommand(commandName);\r
command.removeParameter(parameterName);\r
stateMgr.checkIn(command); \r
}\r
\r
protected void removeCommand (String commandName) {\r
- Pazpar2Command command = getCommand(commandName);\r
+ Pazpar2Command command = req.getCommand(commandName);\r
command.removeParameters();\r
stateMgr.checkIn(command);\r
}\r
\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
- if (parameter != null) {\r
- return parameter.getValueWithExpressions();\r
- }\r
- }\r
- return defaultValue; \r
- }\r
- \r
- protected String getCommandParameterValueSimple (String commandName, String parameterName, String defaultValue) { \r
- Pazpar2Command command = getCommand(commandName);\r
+ CommandReadOnly command = req.getCommandReadOnly(commandName);\r
if (command != null) {\r
- CommandParameter parameter = command.getParameter(parameterName);\r
+ String parameter = command.getParameterValue(parameterName);\r
if (parameter != null) {\r
- return parameter.getSimpleValue();\r
+ return parameter;\r
}\r
}\r
return defaultValue; \r
}\r
-\r
- \r
+ \r
protected int getCommandParameterValue (String commandName, String parameterName, int defaultValue) {\r
- Pazpar2Command command = getCommand(commandName);\r
+ CommandReadOnly command = req.getCommandReadOnly(commandName);\r
if (command != null) {\r
- CommandParameter parameter = command.getParameter(parameterName);\r
+ String parameter = command.getParameterValue(parameterName);\r
if (parameter != null) {\r
- return Integer.parseInt(parameter.getSimpleValue());\r
+ return Integer.parseInt(parameter);\r
}\r
}\r
return defaultValue; \r
}\r
\r
- protected String doCommand(String commandName) { \r
- Pazpar2Command command = req.getCommand(commandName); \r
- logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString());\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
}\r
\r
public String getFilter() {\r
- return getCommandParameterValueSimple("search", "filter", "");\r
+ return getCommandParameterValue("search", "filter", "");\r
}\r
\r
public boolean hasFilter () {\r
import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;\r
import com.indexdata.pz2utils4jsf.config.Configurable;\r
import com.indexdata.pz2utils4jsf.config.Configuration;\r
-import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command;\r
+import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandReadOnly;\r
\r
public interface SearchClient extends Configurable, Serializable {\r
\r
- public void setSearchCommand(Pazpar2Command command);\r
- public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException;\r
+ public void setSearchCommand(CommandReadOnly command);\r
+ public CommandResponse executeCommand(CommandReadOnly command, ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException;\r
public SearchClient cloneMe();\r
public boolean isAuthenticatingClient(); \r
public Configuration getConfiguration();\r
--- /dev/null
+package com.indexdata.pz2utils4jsf.pazpar2.commands;\r
+\r
+/**\r
+ * Interface to Pazpar2Command to be used when the given command\r
+ * should not change. Meant to avoid redundant cloning of commands\r
+ * for potential state changes. \r
+ * \r
+ * @author Niels Erik\r
+ *\r
+ */\r
+public interface CommandReadOnly {\r
+\r
+ public String getName();\r
+ public String getValueWithExpressions();\r
+ public String getEncodedQueryString();\r
+ public boolean hasParameters();\r
+ public boolean hasParameterSet(String parameterName);\r
+ public String getParameterValue(String parameterName);\r
+ public String getUrlEncodedParameterValue(String parameterName);\r
+ \r
+}\r
\r
import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager;\r
\r
-public class Pazpar2Command implements Serializable {\r
+public class Pazpar2Command implements CommandReadOnly, Serializable {\r
\r
private static Logger logger = Logger.getLogger(Pazpar2Command.class);\r
private static final long serialVersionUID = -6825491856480675917L; \r
public String toString () {\r
return parameters.toString();\r
}\r
+\r
+ @Override\r
+ public String getParameterValue(String parameterName) {\r
+ return getParameter(parameterName).getValueWithExpressions();\r
+ \r
+ }\r
+\r
+ @Override\r
+ public String getUrlEncodedParameterValue(String parameterName) {\r
+ return getParameter(parameterName).getEncodedQueryString();\r
+ }\r
+ \r
+ \r
\r
}\r
public Pazpar2Command getCommand(String name) {\r
return stateMgr.checkOut(name);\r
}\r
+ \r
+ public CommandReadOnly getCommandReadOnly(String name) {\r
+ return stateMgr.getCommand(name);\r
+ }\r
\r
}\r
setParameter(new CommandParameter("session","=",sessionId));\r
}\r
\r
- public void setQuery(String query) {\r
- logger.info("writing query: " + query);\r
+ public void setQuery(String query) { \r
setParameter(new CommandParameter("query","=",query));\r
}\r
\r
- public String getQuery () {\r
- logger.info("retrieving query");\r
+ public String getQuery () { \r
return getParameter("query") == null ? null : getParameter("query").getValueWithExpressions();\r
}\r
\r
import com.indexdata.pz2utils4jsf.pazpar2.CommandResponse;\r
import com.indexdata.pz2utils4jsf.pazpar2.SearchClient;\r
import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandParameter;\r
+import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandReadOnly;\r
import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command;\r
import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.AuthenticationEntity;\r
import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser;\r
* @throws ClientProtocolException\r
* @throws IOException\r
*/\r
- private byte[] send(Pazpar2Command command) throws ClientProtocolException, IOException {\r
+ private byte[] send(CommandReadOnly command) throws ClientProtocolException, IOException {\r
String url = serviceUrl + "?" + command.getEncodedQueryString(); \r
logger.info("Sending request "+url); \r
HttpGet httpget = new HttpGet(url); \r
}\r
\r
@Override\r
- public void setSearchCommand(Pazpar2Command command) {\r
+ public void setSearchCommand(CommandReadOnly command) {\r
// Do nothing, Service Proxy is handling this \r
}\r
\r
@Override\r
- public CommandResponse executeCommand(Pazpar2Command command,\r
+ public CommandResponse executeCommand(CommandReadOnly command,\r
ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException {\r
byte[] response = send(command);\r
baos.write(response);\r
\r
import org.apache.log4j.Logger;\r
\r
+import com.indexdata.pz2utils4jsf.pazpar2.commands.CommandReadOnly;\r
import com.indexdata.pz2utils4jsf.pazpar2.commands.Pazpar2Command;\r
import com.indexdata.pz2utils4jsf.utils.Utils;\r
\r
* @return Copy this state's instance of the given command\r
*/\r
public Pazpar2Command checkOut (String commandName) {\r
- logger.info("Getting " + commandName + " from state manager.");\r
+ logger.debug("Getting " + commandName + " from state manager.");\r
return getCurrentState().getCommand(commandName).copy();\r
}\r
\r
+ public CommandReadOnly getCommand (String commandName) {\r
+ return getCurrentState().getCommand(commandName);\r
+ }\r
+ \r
public Pazpar2State getCurrentState () {\r
return states.get(currentKey);\r
}\r