Clears info response when clearing init response
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / commands / SearchCommand.java
index 671e723..0c8d745 100644 (file)
@@ -9,11 +9,12 @@ import javax.inject.Named;
 import org.apache.log4j.Logger;\r
 \r
 import com.indexdata.mkjsf.pazpar2.Pz2Service;\r
+import com.indexdata.mkjsf.pazpar2.commands.sp.SearchCommandSp;\r
 import com.indexdata.mkjsf.pazpar2.commands.sp.ServiceProxyCommand;\r
 import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;\r
 \r
 /**\r
- * Represents a Pazpar2 <code>search</code> command. \r
+ * <b><code>search</code></b> Pazpar2 command, referenced as: <code>pzreq.search</code> \r
  * \r
  * @author Niels Erik\r
  *\r
@@ -56,29 +57,33 @@ public class SearchCommand extends Pazpar2Command implements ServiceProxyCommand
    * were added as expressions (that is, not set with <code>setQuery()</code>).\r
    */\r
   public String getQuery () {    \r
-    return getParameter("query") == null ? null  : getParameter("query").getSimpleValue();\r
+    return getParameter("query") == null ? null  : ((QueryParameter)getParameter("query")).getSimpleValue();\r
   }\r
 \r
   /** \r
    * Returns the complete <code>query</code> parameter value, including expressions.\r
    */\r
   public String getExtendedQuery () {    \r
-    return getParameter("query") == null ? null  : getParameter("query").getValueWithExpressions();\r
+    return getParameter("query") == null ? null  : ((QueryParameter)getParameter("query")).getValueWithExpressions();\r
   }\r
     \r
   /**\r
    * Sets the <code>filter</code> parameter. See Pazpar2 documentation for details.\r
    */  \r
-  public void setFilter(String filterExpression) {\r
-    if (filterExpression != null && filterExpression.length()>0) {\r
-      if (filterExpression.split("[=~]").length==1) {\r
-        removeFilters(filterExpression.split("[=~]")[0]);\r
-      } else if (filterExpression.split("[=~]").length==2) {\r
-        setParameter(new FilterParameter(new Expression(filterExpression)));\r
-      } else {\r
-        logger.error("Could not parse filter expression [" + filterExpression + "]");\r
+  public void setFilter(String compoundExpression) {\r
+    if (getParameter("filter") != null) removeParameterInState("filter");\r
+    if (compoundExpression != null && compoundExpression.length()>0) {      \r
+      // Split expression by commas that are not escaped with backslash\r
+      String[] subExpressions = compoundExpression.split("(?<!\\\\),");\r
+      for (int i=0; i<subExpressions.length; i++) {\r
+        if (i==0) {\r
+          setParameterInState(new FilterParameter(new Expression(subExpressions[i])));\r
+        } else {\r
+          addExpressionInState("filter",new Expression(subExpressions[i]));\r
+        }\r
       }\r
     }\r
+    checkInState(this);\r
   }\r
   \r
   /**\r
@@ -214,10 +219,20 @@ public class SearchCommand extends Pazpar2Command implements ServiceProxyCommand
   /**\r
    * Sets the <code>limit</code> parameter. See Pazpar2 documentation for details.\r
    */  \r
-  public void setLimit (String limitExpression) {   \r
-    if (limitExpression != null && limitExpression.length()>0) {\r
-      setParameter(new LimitParameter(new Expression(limitExpression)));\r
+  public void setLimit (String compoundExpression) {\r
+    if (getParameter("limit") != null) removeParameterInState("limit");\r
+    if (compoundExpression != null && compoundExpression.length()>0) {      \r
+      // Split expression by commas that are not escaped with backslash\r
+      String[] subExpressions = compoundExpression.split("(?<!\\\\),");\r
+      for (int i=0; i<subExpressions.length; i++) {\r
+        if (i==0) {\r
+          setParameterInState(new LimitParameter(new Expression(subExpressions[i])));\r
+        } else {\r
+          addExpressionInState("limit",new Expression(subExpressions[i]));\r
+        }\r
+      }\r
     }\r
+    checkInState(this);\r
   }\r
   \r
   /**\r
@@ -231,7 +246,7 @@ public class SearchCommand extends Pazpar2Command implements ServiceProxyCommand
    * Returns the <code>limit</code> parameter value.\r
    */\r
   public String getLimit () {\r
-    return getParameter("limit") == null ? null : ((FilterParameter)getParameter("limit")).getValueWithExpressions();    \r
+    return getParameter("limit") == null ? null : ((LimitParameter)getParameter("limit")).getValueWithExpressions();    \r
   }\r
     \r
   /**\r
@@ -276,7 +291,7 @@ public class SearchCommand extends Pazpar2Command implements ServiceProxyCommand
   }\r
   \r
   /**\r
-   * Returns a list of limit expressions with fields that matches on of <code>expressionFields</code>\r
+   * Returns a list of limit expressions with fields that matches one of <code>expressionFields</code>\r
    * \r
    * @param expressionFields limit expressions to look for\r
    */\r
@@ -420,18 +435,32 @@ public class SearchCommand extends Pazpar2Command implements ServiceProxyCommand
   \r
   \r
   /**\r
-   * Sets a facet, in CQL, to restrict the current results\r
+   * Adds an expression - for instance a facet criterion, with an optional label - to the query parameter\r
    * \r
-   * @param facetKey  i.e.  'au' for author\r
-   * @param term  i.e. 'Dickens, Charles'\r
+   * <p>Example:</p>\r
+   * <ul>\r
+   *  <li><code>{au}{=}{"Steinbeck, John"}{Steinbeck, John}</code>\r
+   * </ul>\r
    */\r
-  public void setFacet(String facetKey, String term) {\r
+  public void addQueryExpression(String field, String operator, String term, String label) {\r
     if (term != null && term.length()>0) { \r
-      addExpression("query", new Expression(facetKey,"=",term,null));                  \r
+      addExpression("query", new Expression(field,operator,term,label));                  \r
     }            \r
   }\r
   \r
   /**\r
+   * Removes a query expression - for instance a facet criterion - by its exact attributes\r
+   * \r
+   * @param field\r
+   * @param operator\r
+   * @param value\r
+   */\r
+  public void removeQueryExpression(String field, String operator, String value) {\r
+    removeExpression("query",new Expression(field, operator, value, null));    \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
@@ -444,8 +473,8 @@ public class SearchCommand extends Pazpar2Command implements ServiceProxyCommand
   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
+      String currentQuery= getQuery();\r
+      setParameter(new QueryParameter("query","=", currentQuery + " and " + facetExpression));      \r
     }            \r
   }\r
       \r
@@ -473,7 +502,7 @@ public class SearchCommand extends Pazpar2Command implements ServiceProxyCommand
 \r
   @Override\r
   public ServiceProxyCommand getSp() {\r
-    return this;\r
+    return new SearchCommandSp(this);\r
   }\r
 \r
   @Override\r