package com.indexdata.mkjsf.pazpar2.commands;\r
\r
+import java.util.Arrays;\r
import java.util.List;\r
\r
import javax.enterprise.context.SessionScoped;\r
public class SearchCommand extends Pazpar2Command implements ServiceProxyCommand {\r
\r
private static final long serialVersionUID = -1888520867838597236L;\r
- private static Logger logger = Logger.getLogger(SearchCommand.class);\r
- private SingleTargetFilter singleTargetFilter = null;\r
+ private static Logger logger = Logger.getLogger(SearchCommand.class); \r
\r
public SearchCommand() {\r
super("search");\r
setParameter(new FilterParameter(new Expression(field,operator,value,label)));\r
}\r
\r
+ public boolean hasFilterExpression(String... expressionFields) {\r
+ logger.trace("Checking for filter expression for " + Arrays.deepToString(expressionFields));\r
+ for (String field : expressionFields) {\r
+ if (getFilterExpressions(field) != null && getFilterExpressions(field).size()>0) {\r
+ logger.trace("Filter expression found (" + field + ")");\r
+ return true;\r
+ } \r
+ }\r
+ logger.trace("No filter expressions found");\r
+ return false;\r
+ }\r
+\r
+ \r
public String getFilter() {\r
return getParameter("filter") == null ? null : ((FilterParameter)getParameter("filter")).getValueWithExpressions();\r
}\r
\r
- public List<Expression> getFilters() {\r
+ public Expression getOneFilterExpression(String expressionField) {\r
+ List<Expression> exprs = getFilterExpressions(expressionField);\r
+ if (exprs != null && exprs.size()>0) {\r
+ if (exprs.size()>1) {\r
+ logger.warn("More that one filter expression found for [" + expressionField + "] but only asked to return the first one");\r
+ }\r
+ return exprs.get(0);\r
+ } else {\r
+ return null;\r
+ } \r
+ }\r
+\r
+ \r
+ public List<Expression> getFilterExpressions() {\r
return getParameter("filter").getExpressions();\r
}\r
+ \r
+ public List<Expression> getFilterExpressions(String... expressionFields) {\r
+ logger.trace("Checking for filter parameter");\r
+ if (parameters.get("filter")!=null) {\r
+ logger.trace("Found");\r
+ return getParameter("filter").getExpressions(expressionFields);\r
+ } else {\r
+ logger.trace("Not found");\r
+ return null;\r
+ }\r
+ }\r
+ \r
+ public boolean hasFilter () {\r
+ return getFilter().length()>0;\r
+ }\r
\r
public void addFilter(String field, String operator, String value, String label) {\r
if (getParameter("filter") == null) {\r
removeExpression("filter",new Expression(field, operator, value, null));\r
}\r
\r
- public void removeFiltersAfter(String field, String operator, String value) {\r
- removeExpressionsAfter("filter",new Expression(field,operator,value,null));\r
- }\r
- \r
- public boolean hasFilter () {\r
- return getFilter().length()>0;\r
+ public void removeFilters(String... fieldsToRemove) { \r
+ removeExpressions("filter",fieldsToRemove); \r
+ } \r
+ \r
+ public void removeFiltersAfter(String field, String operator, String value, String... fieldsToRemove) { \r
+ removeExpressionsAfter("filter",new Expression(field,operator,value,null),fieldsToRemove); \r
}\r
- \r
+\r
public void setLimit (String limitExpression) { \r
if (limitExpression != null && limitExpression.length()>0) {\r
setParameter(new LimitParameter(new Expression(limitExpression)));\r
public String getLimit () {\r
return getParameter("limit") == null ? null : ((FilterParameter)getParameter("limit")).getValueWithExpressions(); \r
}\r
- \r
- public List<Expression> getLimitExpressions() {\r
- return getParameter("limit").getExpressions();\r
- }\r
- \r
+ \r
public boolean hasLimitExpression(String... expressionFields) {\r
- logger.trace("Checking for limit expression for " + expressionFields);\r
+ logger.trace("Checking for limit expression for " + Arrays.deepToString(expressionFields));\r
for (String field : expressionFields) {\r
if (getLimitExpressions(field) != null && getLimitExpressions(field).size()>0) {\r
logger.trace("Limit expression found (" + field + ")");\r
return exprs.get(0);\r
} else {\r
return null;\r
- }\r
- \r
+ } \r
+ }\r
+ \r
+ public List<Expression> getLimitExpressions() {\r
+ return getParameter("limit").getExpressions();\r
}\r
\r
public List<Expression> getLimitExpressions(String... expressionFields) {\r
if (getParameter("limit") == null) {\r
setLimit(field, operator, value, label);\r
} else {\r
- addExpression("limit",new Expression(field,operator,value,label));\r
- \r
+ addExpression("limit",new Expression(field,operator,value,label)); \r
}\r
}\r
\r
removeParameter("limit");\r
}\r
\r
- public void removeLimits(String... fields) { \r
- removeExpressions("limit",fields); \r
+ public void removeLimits(String... fieldsToRemove) { \r
+ removeExpressions("limit",fieldsToRemove); \r
}\r
\r
public void removeLimit(String field, String operator, String value) {\r
removeExpression("limit",new Expression(field, operator, value, null)); \r
}\r
\r
- public void removeLimitsAfter(String field, String operator, String value, String... fields) { \r
- removeExpressionsAfter("limit",new Expression(field,operator,value,null),fields); \r
+ public void removeLimitsAfter(String field, String operator, String value, String... fieldsToRemove) { \r
+ removeExpressionsAfter("limit",new Expression(field,operator,value,null),fieldsToRemove); \r
}\r
\r
\r
getParameter("query").removeExpression(new Expression(facetKey,"=",term,null));\r
}\r
}\r
- \r
- \r
- /**\r
- * Adds a single target filter to restrict the current query by, \r
- * then executes the current search.\r
- * \r
- * This is a special case of the general setFilter function, \r
- * allowing to associate a descriptive target name with the \r
- * filter expression for display in UI. \r
- * \r
- * @param targetId pazpar2's ID for the target to limit by\r
- * @param targetName a descriptive name for the target\r
- */\r
- public void setSingleTargetFilter (String targetId, String targetName) { \r
- if (hasSingleTargetFilter(new SingleTargetFilter(targetId,targetName))) {\r
- logger.debug("Already using target filter " + this.singleTargetFilter.getFilterExpression());\r
- } else { \r
- logger.debug("Setting new single target filter for [" + targetName + "]");\r
- this.singleTargetFilter = new SingleTargetFilter(targetId,targetName);\r
- setParameter(new CommandParameter("filter","=",this.singleTargetFilter.getFilterExpression())); \r
- } \r
- }\r
-\r
- public SingleTargetFilter getSingleTargetFilter () {\r
- logger.debug("request to get the current single target filter " + singleTargetFilter);\r
- return singleTargetFilter;\r
- }\r
- \r
- /**\r
- * Removes the current target filter from the search\r
- * \r
- */\r
- public String removeSingleTargetFilter () {\r
- logger.debug("Removing target filter " + singleTargetFilter.getFilterExpression());\r
- this.singleTargetFilter = null;\r
- removeParameter("filter");\r
- return null;\r
- }\r
- \r
- /**\r
- * \r
- * @return The target filter set on the current search command\r
- */\r
- public boolean hasSingleTargetFilter() {\r
- logger.debug("Checking if a single target filter is set: " + (singleTargetFilter != null));\r
- return singleTargetFilter != null; \r
- }\r
- \r
- /**\r
- * Resolves if the current search command has a target filter - to\r
- * be used by the UI for conditional rendering of target filter info.\r
- * \r
- * @return true if the current search command is limited by a target \r
- * filter\r
- */\r
- protected boolean hasSingleTargetFilter(SingleTargetFilter targetFilter) {\r
- logger.debug("Checking if target filter for [" + targetFilter.getTargetName() + "] is set.");\r
- return hasSingleTargetFilter() && targetFilter.equals(this.singleTargetFilter);\r
- }\r
- \r
+ \r
public SearchCommand copy () {\r
SearchCommand newCommand = new SearchCommand();\r
for (String parameterName : parameters.keySet()) {\r
newCommand.setParameterInState(parameters.get(parameterName).copy()); \r
}\r
- newCommand.singleTargetFilter = this.singleTargetFilter;\r
return newCommand;\r
}\r
\r