Moves more search and show methods
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / commands / SearchCommand.java
index 25dc68e..a7f8168 100644 (file)
@@ -4,6 +4,7 @@ import javax.enterprise.context.SessionScoped;
 \r
 import org.apache.log4j.Logger;\r
 \r
+import com.indexdata.pz2utils4jsf.pazpar2.Expression;\r
 import com.indexdata.pz2utils4jsf.pazpar2.state.StateManager;\r
 \r
 @SessionScoped\r
@@ -28,6 +29,52 @@ public class SearchCommand extends Pazpar2Command {
     return getParameter("query") == null ? null  : getParameter("query").getValueWithExpressions();\r
   }\r
   \r
+  /**\r
+   * Sets a facet, in CQL, to restrict the current results,\r
+   * then executes the search \r
+   * \r
+   * @param facetKey  i.e.  'au' for author\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));            \r
+    }            \r
+  }\r
+  \r
+  /**\r
+   * Sets a facet to limit the current query by. The \r
+   * facet is appended to the query string itself (rather\r
+   * as a separately managed entity. It will thus appear\r
+   * in a query field as retrieved by getQuery(). It will\r
+   * not be removed by removeFacet(...)\r
+   * \r
+   * @param facetKey  i.e. 'au' for author\r
+   * @param term i.e. 'Dickens, Charles'\r
+   */\r
+  public void setFacetOnQuery (String facetKey, String term) {\r
+    String facetExpression = facetKey + "=" + term;    \r
+    if (term != null && term.length()>0) {\r
+      String currentQuery= getParameterValue("query");\r
+      setParameter(new CommandParameter("query","=", currentQuery + " and " + facetExpression));      \r
+    }            \r
+  }\r
+      \r
+  /**\r
+   * Removes a facet set by setFacet(...), then executes\r
+   * the search.\r
+   * \r
+   * Will not remove facets set by setFacetOnQuery(...)\r
+   *  \r
+   * @param facetKey i.e. 'au' for author\r
+   * @param term i.e. 'Dickens, Charles'\r
+   */\r
+  public void removeFacet(String facetKey, String term) {\r
+    if (getParameter("query") != null) {\r
+      getParameter("query").removeExpression(new Expression(facetKey,"=",term));\r
+    }\r
+  }\r
+  \r
   public void setFilter(String filterExpression) {\r
     setParameter(new CommandParameter("filter","=",filterExpression));\r
   }\r