Returns "" rather than "null" on missing value
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / commands / Expression.java
1 package com.indexdata.mkjsf.pazpar2.commands;\r
2 \r
3 import java.io.Serializable;\r
4 \r
5 import org.apache.log4j.Logger;\r
6 \r
7 /**\r
8  * Represents a complex command parameter value, in form of an expression with \r
9  * an equality operator\r
10  * <p>\r
11  * An expression consist of a left-of-operator field or key, an equality operator (= or ~), \r
12  * a right-of-operator value, and optionally a label describing the value for UI display.\r
13  * </p> \r
14  * <p>Examples:</p>\r
15  * <ul>\r
16  *  <li><code>pz:id=1234</code> "My Target"</li>\r
17  *  <li><code>category~libcatalog</code> "Library Catalogs"</li>\r
18  *  <li><code>author="Steinbeck, John"</code></li>\r
19  * </ul>\r
20  * @author Niels Erik\r
21  *\r
22  */\r
23 public class Expression implements Serializable {\r
24   \r
25   private static final long serialVersionUID = -751704027842027769L;\r
26   private static Logger logger = Logger.getLogger(Expression.class);\r
27   String leftEntity;\r
28   String operator;\r
29   String rightEntity;\r
30   String label;  \r
31   \r
32   /**\r
33    * Instantiates an expression with a label\r
34    * \r
35    * @param leftEntity left-of-operator field name (or 'key')\r
36    * @param operator an equality operator\r
37    * @param rightEntity right-of-operator value\r
38    * @param label to be used for display, for instance in a UI control that adds or removes the expression\r
39    *  from a command parameter\r
40    */\r
41   public Expression (String field, String operator, String value, String label) {\r
42     this.leftEntity = field;\r
43     this.operator = operator;\r
44     this.rightEntity = value;    \r
45     this.label = label;\r
46   }\r
47   \r
48   /**\r
49    * Instantiates an expression by parsing the provided expression string, which must be\r
50    * on the form {name}({=}or{~}){value}.\r
51    * <p>\r
52    * Currently only '=' and '~' are recognized as operators\r
53    * </p>\r
54    * \r
55    * @param expressionString\r
56    */\r
57   public Expression (String expressionString) {\r
58     String[] parts = expressionString.split("[=~]");\r
59     if (parts.length>0) {\r
60       this.leftEntity = parts[0];\r
61       this.operator = expressionString.contains("=") ? "=" : "~";\r
62     }\r
63     if (parts.length>1) {\r
64       this.rightEntity = parts[1];\r
65       this.label=rightEntity;\r
66     } else {\r
67       this.rightEntity = "";\r
68       this.label = "";\r
69     }\r
70   }\r
71   \r
72   /** \r
73    * Clones the expression\r
74    * \r
75    * @return a clone of this expression\r
76    */\r
77   public Expression copy() {\r
78     logger.trace("Copying " + this.toString());\r
79     return new Expression(leftEntity, operator, rightEntity, label);\r
80   }\r
81   \r
82   public String toString() {\r
83     return leftEntity + operator + rightEntity;\r
84   }\r
85   \r
86   /**\r
87    * Returns the label describing the value of the expression or,\r
88    * if no label was provided, the value itself.\r
89    * \r
90    * @return label or right-of-operator value if no label provided\r
91    */\r
92   public String getLabel() {\r
93     return label;\r
94   }\r
95   \r
96   /**\r
97    * Returns the left-of-operator field (or name or key).\r
98    * \r
99    * @return entity left of operator\r
100    */\r
101   public String getField () {\r
102     return leftEntity;\r
103   }\r
104   \r
105   /**\r
106    * Returns the operator \r
107    * \r
108    * @return the operator of the expression\r
109    */\r
110   public String getOperator() {\r
111     return operator;\r
112   }\r
113   \r
114   /**\r
115    * Returns the right-of-operator value of the expression\r
116    * \r
117    * @return entity right of operator\r
118    */\r
119   public String getValue() {\r
120     return rightEntity;\r
121   }\r
122   \r
123 }\r