import com.indexdata.mkjsf.pazpar2.data.ResponseParser;\r
import com.indexdata.mkjsf.pazpar2.data.Responses;\r
\r
+/**\r
+ * Represents a generic Pazpar2 or Service Proxy command with all its current parameters, and has\r
+ * methods for executing the command against the currently selected Pazpar2 service</p>\r
+ * <p>Being an abstract class it only has generic methods for getting and setting parameters. \r
+ * Implementing classes are supposed to create named getters and setters for convenient access\r
+ * to parameters from the UI.</p> \r
+ * <p>Parameters can be set with or without notifying the state manager.<p>\r
+ * \r
+ * <p><i>Note: Internally the application has to be able to set parameters without state changes \r
+ * - for instance to avoid eternal feedback when copying parameter from one state to the next. A \r
+ * setting from the UI should spawn a new search state however.</i></p> \r
+ * \r
+ * @author Niels Erik\r
+ *\r
+ */\r
public abstract class Pazpar2Command implements Serializable {\r
\r
private static Logger logger = Logger.getLogger(Pazpar2Command.class);\r
this.name = name; \r
}\r
\r
+ /**\r
+ * Commands must implement this method to provide an completely detached, deep clone of \r
+ * themselves.\r
+ * \r
+ * The clone is needed by the state manager to transfer commands with current setting \r
+ * from one state to the next.\r
+ * \r
+ * Whenever a non-standard attribute is added to a command class, the copy method must \r
+ * be updated to ensure that the new attribute is brought over as well. \r
+ * \r
+ * @return a Pazpar2 command of the given type\r
+ */\r
public abstract Pazpar2Command copy ();\r
\r
public String getCommandName() {\r
return name;\r
}\r
\r
+ /**\r
+ * Executes the command with the currently selected parameters against \r
+ * the currently selected Pazpar2 service\r
+ * \r
+ * @return Response data object based on the Pazpar2 service response. \r
+ */\r
public ResponseDataObject run() { \r
return run(Pz2Service.get().getSearchClient(),\r
Pz2Service.get().getPzresp());\r
}\r
\r
+ /**\r
+ * Executes the commands with the currently selected parameters, while adding\r
+ * the parameters provided\r
+ * @param parameters A list of parameters on the form [key=value]\r
+ * \r
+ * @return Response data object based on the Pazpar2 service response\r
+ */\r
public ResponseDataObject runWith(String... parameters) {\r
for (String parameter : parameters) {\r
StringTokenizer tokenizer = new StringTokenizer(parameter,"=");\r
}\r
\r
/**\r
- * For running the command in a thread. Client and Responses must be \r
- * provided because at this point the CDI bean cannot be retrieved \r
- * from within a thread.\r
+ * Executes the command in a thread. \r
+ * \r
+ * Note: Client and Responses must be provided because at this point \r
+ * CDI beans cannot be retrieved from within a thread.\r
* \r
* @param client\r
* @param pzresp\r
return responseObject; \r
}\r
\r
- \r
+ \r
+ /**\r
+ * Sets a parameter on this command and notifies the state manager\r
+ * about the change\r
+ * \r
+ * @param parameter \r
+ */\r
public void setParameter (CommandParameter parameter) {\r
Pazpar2Command copy = this.copy();\r
logger.trace(name + " command: setting parameter [" + parameter.getName() + "=" + parameter.getValueWithExpressions() + "]");\r
checkInState(copy);\r
}\r
\r
+ /**\r
+ * Sets multiple parameters on the command and notifies the state\r
+ * manager -- once -- about the change\r
+ * \r
+ * @param params \r
+ */\r
public void setParameters (CommandParameter... params) {\r
Pazpar2Command copy = this.copy();\r
for (CommandParameter param : params) {\r
checkInState(copy);\r
}\r
\r
+ /**\r
+ * Sets multiple parameters on this command without notifying the state manager. \r
+ * Typically used when one parameter setting should automatically trigger\r
+ * other parameters to be reset to defaults etc. Intended to avoid \r
+ * useless proliferation of states \r
+ * \r
+ * @param params\r
+ */\r
public void setParametersInState (CommandParameter... params) { \r
for (CommandParameter param : params) {\r
logger.trace(name + " command: setting parameter [" + param.getName() + "=" + param.getValueWithExpressions() + "] silently");\r
parameters.put(param.getName(),param);\r
} \r
}\r
- \r
+ \r
+ /**\r
+ * Sets a parameter on this command without notifying the state manager. \r
+ * Typically used when one parameter setting should automatically trigger\r
+ * other parameters to be reset to defaults etc. Intended to avoid \r
+ * useless proliferation of states \r
+ * \r
+ * @param parameter\r
+ */ \r
public void setParameterInState (CommandParameter parameter) {\r
logger.trace(name + " command: setting parameter [" + parameter.getName() + "=" + parameter.getValueWithExpressions() + "] silently");\r
parameters.put(parameter.getName(),parameter); \r
}\r
\r
\r
+ /**\r
+ * Retrieves a command parameter by parameter name\r
+ * \r
+ * @param name of the parameter\r
+ * @return CommandParameter\r
+ */\r
public CommandParameter getParameter (String name) {\r
return parameters.get(name);\r
}\r
\r
+ /**\r
+ * Removes a parameter completely and notifies the state manager\r
+ * about the change\r
+ * \r
+ * @param name of the parameter to remove\r
+ */\r
public void removeParameter (String name) {\r
Pazpar2Command copy = this.copy();\r
copy.parameters.remove(name);\r
checkInState(copy);\r
}\r
\r
+ /**\r
+ * Removes multiple parameters completely and notifies the state manager\r
+ * -- once -- about the change\r
+ * \r
+ * @param name of the parameter to remove\r
+ */ \r
public void removeParameters() {\r
Pazpar2Command copy = this.copy();\r
copy.parameters = new HashMap<String,CommandParameter>();\r
checkInState(copy);\r
}\r
\r
+ \r
+ /**\r
+ * Removes all parameters without notifying the state manager. For instance\r
+ * used in case of change of Pazpar2 service or renewed login to a service.\r
+ * \r
+ */\r
public void removeParametersInState() {\r
parameters = new HashMap<String,CommandParameter>(); \r
}\r
\r
+ /**\r
+ * Adds an expression to an ordered list of expressions on a given parameter\r
+ * and notifies the state manager of the change\r
+ * \r
+ * @param parameterName name of the parameter to add the expression to\r
+ * @param expression\r
+ */\r
public void addExpression(String parameterName, Expression expression) {\r
Pazpar2Command copy = this.copy();\r
copy.getParameter(parameterName).addExpression(expression);\r
return getParameter(parameterName)==null ? "" : getParameter(parameterName).getValueWithExpressions(); \r
}\r
\r
+ /*\r
public String getUrlEncodedParameterValue(String parameterName) {\r
return getParameter(parameterName).getEncodedQueryString();\r
}\r
+ */\r
\r
public void setSession (String sessionId) {\r
setParameter(new CommandParameter("session","=",sessionId));\r
return getParameterValue("session");\r
} \r
\r
- private void checkInState(Pazpar2Command command) {\r
+ /**\r
+ * Notifies the state manager that this command changed a parameter\r
+ * \r
+ * @param command\r
+ */\r
+ protected void checkInState(Pazpar2Command command) {\r
Pz2Service.get().getStateMgr().checkIn(command);\r
}\r
- \r
- public String navigateTo (String target) {\r
- return target;\r
- }\r
- \r
+ \r
+ /**\r
+ * Implementing classes must provide their Service Proxy \r
+ * extension command if any extension parameters exists, \r
+ * or -- just to be polite -- 'this' if there is no\r
+ * Service Proxy extension to the given command.\r
+ * @return\r
+ */\r
public abstract ServiceProxyCommand getSp();\r
- \r
+ \r
+ /**\r
+ * Implementing commands publishes whether they only \r
+ * apply to the Service Proxy - or can be executed \r
+ * against straight Pazpar2 as well. Convenient for a \r
+ * UI that switches between service types - whether \r
+ * deployment time or run time.\r
+ * \r
+ * @return false if the command applies to straight Pazpar2\r
+ */\r
public abstract boolean spOnly(); \r
}\r
--- /dev/null
+package com.indexdata.mkjsf.pazpar2.commands;\r
+\r
+public class QueryParameter extends CommandParameter {\r
+\r
+ private static final long serialVersionUID = -3649052232241100927L;\r
+ private String booleanOperator = "AND";\r
+\r
+ public QueryParameter(String name) {\r
+ super(name);\r
+ }\r
+\r
+ public QueryParameter(String name, String operator, String value,\r
+ Expression... expressions) {\r
+ super(name, operator, value, expressions);\r
+ }\r
+\r
+ public QueryParameter(String name, String operator, Expression... expressions) {\r
+ super(name, operator, expressions);\r
+ }\r
+\r
+ public QueryParameter(String name, String operator, String value) {\r
+ super(name, operator, value);\r
+ }\r
+\r
+ public QueryParameter(String name, String operator, int value) {\r
+ super(name, operator, value);\r
+ }\r
+ \r
+ public void setBooleanOperator (String operator) {\r
+ this.booleanOperator = operator;\r
+ }\r
+ \r
+ public String getValueWithExpressions () {\r
+ StringBuilder completeValue = new StringBuilder((value==null ? "" : value));\r
+ boolean first = true;\r
+ for (Expression expr : expressions) {\r
+ if (value == null && first) {\r
+ first = false;\r
+ completeValue.append(expr.toString());\r
+ } else {\r
+ completeValue.append(" "+booleanOperator+" " + expr.toString());\r
+ }\r
+ }\r
+ return completeValue.toString(); \r
+ } \r
+ \r
+ public QueryParameter copy() { \r
+ QueryParameter newParam = new QueryParameter(name);\r
+ newParam.value = this.value;\r
+ newParam.operator = this.operator;\r
+ newParam.booleanOperator = this.booleanOperator;\r
+ for (Expression expr : expressions) {\r
+ newParam.addExpression(expr.copy()); \r
+ }\r
+ return newParam;\r
+ }\r
+\r
+\r
+}\r
}\r
\r
public void setQuery(String query) { \r
- setParameter(new CommandParameter("query","=",query));\r
+ setParameter(new QueryParameter("query","=",query));\r
+ }\r
+ \r
+ public void setBooleanOperatorForQuery(String operator) {\r
+ Pazpar2Command copy = this.copy();\r
+ ((QueryParameter) getParameter("query")).setBooleanOperator(operator);\r
+ checkInState(copy);\r
}\r
\r
public String getQuery () { \r
+ return getParameter("query") == null ? null : getParameter("query").getSimpleValue();\r
+ }\r
+ \r
+ public String getExtendedQuery () { \r
return getParameter("query") == null ? null : getParameter("query").getValueWithExpressions();\r
}\r
\r
if (getParameter("filter") == null) {\r
setFilter(field + operator + value);\r
} else {\r
- getParameter("filter").addExpression(new Expression(field,operator,value,(label != null ? label : value)));\r
+ addExpression("filter",new Expression(field,operator,value,(label != null ? label : value)));\r
}\r
}\r
\r
* @param term i.e. 'Dickens, Charles'\r
*/\r
public void setFacet(String facetKey, String term) {\r
- if (term != null && term.length()>0) { \r
- getParameter("query").addExpression(new Expression(facetKey,"=",term,null)); \r
+ if (term != null && term.length()>0) { \r
+ addExpression("query", new Expression(facetKey,"=",term,null)); \r
} \r
}\r
\r
*/\r
public void removeFacet(String facetKey, String term) {\r
if (getParameter("query") != null) {\r
- getParameter("query").removeExpression(new Expression(facetKey,"=",term,null));\r
+ removeExpression("query",new Expression(facetKey,"=",term,null));\r
}\r
}\r
\r