X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2Fcommands%2FCommandParameter.java;h=6c6df868ee2219489ab4c360b5e79e104ee0841f;hb=6c92dd9a72ae1fa27dd061c1897531879b8c0ba6;hp=f97497fed171ee35e9ad5f429b89b0b6993e0b33;hpb=c6430ad85e3b04ea2823df38c15e8473342ff95b;p=mkjsf-moved-to-github.git
diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java
index f97497f..6c6df86 100644
--- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java
+++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java
@@ -3,15 +3,25 @@ package com.indexdata.mkjsf.pazpar2.commands;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
+import java.util.ArrayList;
import java.util.Arrays;
-import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
-import java.util.Map;
import org.apache.log4j.Logger;
-import com.indexdata.mkjsf.pazpar2.commands.CommandParameter;
-
+/**
+ * Represents a Pazpar2 command parameter with a name, an operator,
+ * a simple value and/or one or more complex values (expressions).
+ *
Examples:
+ *
+ * {name}{=}{value}
+ * {name}{=}{value} AND {expr1=value1} AND {expr2=value2}
+ * {name}{=}{expr1~value1},{expr2~value2}
+ *
+ * @author Niels Erik
+ *
+ */
public class CommandParameter implements Serializable {
private static Logger logger = Logger.getLogger(CommandParameter.class);
@@ -20,61 +30,236 @@ public class CommandParameter implements Serializable {
String name = null;
String operator = null;
String value = null;
- Map expressions = new HashMap();
+ List expressions = new ArrayList();
private static List nologparams = Arrays.asList("password");
public CommandParameter (String name) {
- logger.debug("Instantiating command parameter '" + name + "'");
+ logger.trace("Instantiating command parameter '" + name + "'");
this.name = name;
}
+ /**
+ * Instantiates a parameter with a simple value and one or more expressions
+ *
+ * @param name
+ * @param operator
+ * @param value
+ * @param expressions
+ */
public CommandParameter (String name, String operator, String value, Expression... expressions) {
- logger.debug("Instantiating command parameter " + name + " with expressions: [" + expressions + "]");
+ logger.trace("Instantiating command parameter " + name + " with value [" + value + "] and expressions: [" + expressions + "]");
this.name = name;
this.operator = operator;
this.value = value;
for (Expression expr : expressions) {
- this.expressions.put(expr.toString(), expr);
+ this.expressions.add(expr);
+ }
+ }
+
+ /**
+ * Instantiates a parameter with one or more expressions
+ * @param name
+ * @param operator
+ * @param expressions
+ */
+ public CommandParameter (String name, String operator, Expression... expressions) {
+ logger.trace("Instantiating command parameter " + name + " with expressions: [" + expressions + "]");
+ this.name = name;
+ this.operator = operator;
+ for (Expression expr : expressions) {
+ this.expressions.add(expr);
}
}
+
+ /**
+ * Instantiates a parameter with a simple value
+ * @param name
+ * @param operator
+ * @param value
+ */
public CommandParameter (String name, String operator, String value) {
- if (!nologparams.contains(name)) logger.debug("Instantiating command parameter '" + name + "' with String: [" + value + "]");
+ if (!nologparams.contains(name)) logger.trace("Instantiating command parameter '" + name + "' with String: [" + value + "]");
this.name = name;
this.operator = operator;
this.value = value;
}
+ /**
+ * Instantiates a parameter with a numeric value
+ * @param name
+ * @param operator
+ * @param value
+ */
public CommandParameter (String name, String operator, int value) {
- logger.debug("Instantiating command parameter '" + name + "' with int: [" + value + "]");
+ logger.trace("Instantiating command parameter '" + name + "' with int: [" + value + "]");
this.name = name;
this.operator = operator;
this.value = value+"";
}
-
+ /**
+ * Returns the name (left of operator) of this parameter
+ *
+ * @return name (left of operator) of this parameter
+ */
public String getName () {
return name;
}
- public Map getExpressions () {
+ /**
+ * Returns a list of all current expressions
+ *
+ * @return a list of all current expressions
+ */
+ public List getExpressions () {
return expressions;
}
+
+ /**
+ * Returns expressions selected by their left-hand keys - as in 'expressionField=value'.
+ *
+ * If the parameter has expressions expr1=x,expr2=y,expr3=z,expr1=u then invoking this method
+ * with {"expr1","expr3"} would return expr1=x,expr3=z,expr1=u but not expr2=y.
+ *
+ * @param expressionFields The expression types to return
+ * @return a list of expressions with the given keys to the left of the operator
+ *
+ */
+ public List getExpressions(String... expressionFields) {
+ List requestedFields = Arrays.asList(expressionFields);
+ List exprs = new ArrayList();
+ for (Expression expr : expressions) {
+ if (requestedFields.contains(expr.getField())) {
+ exprs.add(expr);
+ }
+ }
+ return exprs;
+ }
+ /**
+ * Adds an expression to the end of the list of current expressions (if any)
+ *
+ * @param expression to add
+ */
public void addExpression(Expression expression) {
logger.debug("Adding expression [" + expression + "] to '" + name + "'");
- this.expressions.put(expression.toString(),expression);
+ this.expressions.add(expression);
}
+ /**
+ * Removes a single expression identified by all its characteristics
+ *
+ * @param expression to remove
+ */
public void removeExpression(Expression expression) {
- this.expressions.remove(expression.toString());
+ for (Expression expr : expressions) {
+ if (expr.toString().equals(expression.toString())) {
+ expressions.remove(expr);
+ break;
+ }
+ }
}
+ /**
+ * Removes all expressions that appear after the provided expression and that
+ * have the given keys to the left of their operators - as in 'expressionField=value'.
+ *
+ * This method is intended for bread crumb-like UI controls
+ *
+ * @param expression The expression to use a starting point for removal (not inclusive)
+ * @param expressionFields The expression fields to remove
+ */
+ public void removeExpressionsAfter (Expression expression, String... expressionFields) {
+ List exprFieldsToRemove = Arrays.asList(expressionFields);
+ int fromIdx = 0;
+ for (Expression expr : expressions) {
+ fromIdx++;
+ if (expr.toString().equals(expression.toString())) {
+ break;
+ }
+ }
+ if (fromIdx candidatesForRemoval = expressions.subList(fromIdx, expressions.size()).iterator();
+ while (candidatesForRemoval.hasNext()) {
+ Expression exp = candidatesForRemoval.next();
+ if (exprFieldsToRemove.contains(exp.getField())) {
+ expressions.remove(exp);
+ }
+ }
+ }
+ }
+ /**
+ * Removes expressions selected by their left-of-operator fields/keys - as in 'expressionField=value'.
+ *
+ * If the parameter has expressions expr1=x,expr2=y,expr3=z,expr1=u then invoking this method
+ * with {"expr1","expr3"} would remove expr1=x,expr3=z and expr1=u but leave expr2=y.
+ *
+ * @param expressionFields The expression types (by field) to remove
+ * @return a list of expressions with the given left-of-operator keys
+ *
+ */
+ public void removeExpressions (String... expressionFields) {
+ List fieldsToRemove = Arrays.asList(expressionFields);
+ Iterator i = expressions.iterator();
+ while (i.hasNext()) {
+ Expression expr = i.next();
+ if (fieldsToRemove.contains(expr.getField())) {
+ logger.trace("Removing expression: " + expr.toString());
+ i.remove();
+ }
+ }
+ }
+
+ /**
+ *
+ * @return true if an operator was defined for this parameter yet
+ */
public boolean hasOperator() {
return operator != null;
}
+ /**
+ * Returns true if this parameter has a simple value
+ *
+ * @return true if this parameter has a simple value
+ */
+ public boolean hasValue() {
+ return value != null && value.length()>0;
+ }
+
+ /**
+ * Returns true if this parameter has expressions (complex values)
+ *
+ * @return true if this parameter has expressions (complex values)
+ */
+ public boolean hasExpressions() {
+ return expressions.size()>0;
+ }
+
+ /**
+ * Returns true if this parameter has expressions of the given type,
+ * that is, expressions where the left-of-operator key equals 'expressionField'
+ *
+ * @param expressionField the type of expression to look for
+ * @return true if this parameter has expressions of the given type,
+ * that is, expressions where the left-of-operator key equals 'expressionField'
+ */
+ public boolean hasExpressions(String expressionField) {
+ for (Expression expr : expressions) {
+ if (expr.getField().equals(expressionField)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns a URL encoded string of this parameter with name, operator, simple value and/or expressions
+ *
+ * @return URL encoded string of this parameter with name, operator, simple value and/or expressions
+ */
public String getEncodedQueryString () {
try {
return name + operator + URLEncoder.encode(getValueWithExpressions(),"UTF-8");
@@ -84,18 +269,33 @@ public class CommandParameter implements Serializable {
}
}
+ /**
+ * Returns the simple parameter value or null if no simple value was set for this parameter
+ *
+ * @return the simple parameter value, null if no simple value was set for this parameter
+ */
public String getSimpleValue() {
return value;
}
+ /**
+ * Returns the simple parameter value and/or any expressions, separated by 'AND'
+ *
+ * @return the simple parameter value and/or any expressions separated by 'AND'
+ */
public String getValueWithExpressions () {
- StringBuilder completeValue = new StringBuilder((value==null ? "" : value));
- for (String key : expressions.keySet()) {
- completeValue.append(" and " + expressions.get(key));
+ StringBuilder completeValue = new StringBuilder((value==null ? "" : value));
+ boolean first=true;
+ for (Expression expr : expressions) {
+ if (value == null && first) {
+ first = false;
+ completeValue.append(expr.toString());
+ } else {
+ completeValue.append(" AND " + expr.toString());
+ }
}
- return completeValue.toString();
-
- }
+ return completeValue.toString();
+ }
@Override
public boolean equals (Object otherParameter) {
@@ -113,13 +313,19 @@ public class CommandParameter implements Serializable {
return getValueWithExpressions();
}
+ /**
+ * Clones the CommandParameter
+ *
+ * @return a deep, detached clone of this command parameter, for copying
+ * a parameter to a new state.
+ */
public CommandParameter copy() {
- logger.debug("Copying parameter '"+ name + "' for modification");
+ logger.trace("Copying parameter '"+ name + "' for modification");
CommandParameter newParam = new CommandParameter(name);
newParam.value = this.value;
newParam.operator = this.operator;
- for (String key : expressions.keySet()) {
- newParam.addExpression(expressions.get(key).copy());
+ for (Expression expr : expressions) {
+ newParam.addExpression(expr.copy());
}
return newParam;
}