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=d08cde1200f4d9bc12e5944acff211b61c00546c;hpb=31a027596723261b413d69c74428b176def3627f;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 d08cde1..6c6df86 100644
--- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java
+++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/CommandParameter.java
@@ -10,6 +10,18 @@ import java.util.List;
import org.apache.log4j.Logger;
+/**
+ * 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);
@@ -26,6 +38,14 @@ public class CommandParameter implements Serializable {
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.trace("Instantiating command parameter " + name + " with value [" + value + "] and expressions: [" + expressions + "]");
this.name = name;
@@ -36,6 +56,12 @@ public class CommandParameter implements Serializable {
}
}
+ /**
+ * 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;
@@ -46,6 +72,12 @@ public class CommandParameter implements Serializable {
}
+ /**
+ * 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.trace("Instantiating command parameter '" + name + "' with String: [" + value + "]");
this.name = name;
@@ -53,6 +85,12 @@ public class CommandParameter implements Serializable {
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.trace("Instantiating command parameter '" + name + "' with int: [" + value + "]");
this.name = name;
@@ -60,15 +98,34 @@ public class CommandParameter implements Serializable {
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;
}
+ /**
+ * 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();
@@ -80,11 +137,21 @@ public class CommandParameter implements Serializable {
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.add(expression);
}
+ /**
+ * Removes a single expression identified by all its characteristics
+ *
+ * @param expression to remove
+ */
public void removeExpression(Expression expression) {
for (Expression expr : expressions) {
if (expr.toString().equals(expression.toString())) {
@@ -94,6 +161,15 @@ public class CommandParameter implements Serializable {
}
}
+ /**
+ * 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;
@@ -114,6 +190,16 @@ public class CommandParameter implements Serializable {
}
}
+ /**
+ * 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();
@@ -125,19 +211,41 @@ public class CommandParameter implements Serializable {
}
}
}
-
+
+ /**
+ *
+ * @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)) {
@@ -147,6 +255,11 @@ public class CommandParameter implements Serializable {
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");
@@ -156,14 +269,30 @@ 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));
+ StringBuilder completeValue = new StringBuilder((value==null ? "" : value));
+ boolean first=true;
for (Expression expr : expressions) {
- completeValue.append(" and " + expr.toString());
+ if (value == null && first) {
+ first = false;
+ completeValue.append(expr.toString());
+ } else {
+ completeValue.append(" AND " + expr.toString());
+ }
}
return completeValue.toString();
}
@@ -184,6 +313,12 @@ 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.trace("Copying parameter '"+ name + "' for modification");
CommandParameter newParam = new CommandParameter(name);