001: package org.julp.misc.search;
002:
003: import java.util.*;
004:
005: public class SearchCriteriaHolder implements java.io.Serializable,
006: Cloneable {
007:
008: public SearchCriteriaHolder() {
009: }
010:
011: protected String fieldLabel = null;
012: protected String fieldName = null;
013: protected String operator = "=";
014: protected String functions = null;
015: protected Object searchValue = null;
016: protected String booleanCondition = AND;
017:
018: public static final String LESS = "<";
019: public static final String LESS_OR_EQUALS = "<=";
020: public static final String EQUALS = "=";
021: public static final String LIKE = "LIKE";
022: public static final String NOT_LIKE = "NOT LIKE";
023: public static final String IN = "IN";
024: public static final String NOT_IN = "NOT IN";
025: public static final String NOT_EQUALS = "<>";
026: public static final String NOT_EXISTS = "NOT EXISTS";
027: public static final String EXISTS = "EXISTS";
028: public static final String BETWEEN = "BETWEEN";
029: public static final String NOT_BETWEEN = "NOT BETWEEN";
030: public static final String IS = "IS";
031: public static final String IS_NOT = "IS NOT";
032: public static final String GREATER_OR_EQUALS = ">=";
033: public static final String GREATER = ">";
034:
035: public static final String AND = "AND";
036: public static final String OR = "OR";
037: public static final String AND_NESTED_LOGIC_START = "AND (";
038: public static final String OR_NESTED_LOGIC_START = "OR (";
039: public static final String AND_NESTED_LOGIC_END = ") AND";
040: public static final String OR_NESTED_LOGIC_END = ") OR";
041: public static final String AND_NESTED_LOGIC = ") AND (";
042: public static final String OR_NESTED_LOGIC = ") OR (";
043: public static final String NESTED_LOGIC_END = ")";
044:
045: // hints for search using LIKE operator
046: public static final int STARTS_WITH = 0;
047: public static final int CONTAINS = 1;
048: public static final int ENDS_WITH = 2;
049: // default hint
050: protected int likeHint = STARTS_WITH;
051:
052: protected String operators = "<=NOT LIKE!=NOT IN<>NOT EXISTSNOT BETWEEN IS NOT >=";
053: protected Map searchValuesMap = null;
054: protected List searchValuesList = null;
055:
056: /** Getter for property fieldName.
057: * @return Value of property fieldName.
058: *
059: */
060: public java.lang.String getFieldName() {
061: return fieldName;
062: }
063:
064: /** Setter for property fieldName.
065: * @param fieldName New value of property fieldName.
066: *
067: */
068: public void setFieldName(java.lang.String fieldName) {
069: this .fieldName = fieldName;
070: }
071:
072: /** Getter for property operator.
073: * @return Value of property operator.
074: *
075: */
076: public java.lang.String getOperator() {
077: return operator;
078: }
079:
080: /** Setter for property operator.
081: * @param operator New value of property operator.
082: *
083: */
084: public void setOperator(java.lang.String operator) {
085: if (operators.indexOf(operator.toUpperCase()) == -1) {
086: throw new IllegalArgumentException("Invalid Operator: "
087: + operator);
088: }
089: this .operator = operator;
090: }
091:
092: /** Getter for property searchValue.
093: * @return Value of property searchValue.
094: *
095: */
096: public java.lang.Object getSearchValue() {
097: return searchValue;
098: }
099:
100: /** Setter for property searchValue.
101: * @param searchValue New value of property searchValue.
102: *
103: */
104: public void setSearchValue(java.lang.Object searchValue) {
105: this .searchValue = searchValue;
106: }
107:
108: /** Getter for property booleanCondition.
109: * @return Value of property booleanCondition.
110: *
111: */
112: public java.lang.String getBooleanCondition() {
113: return booleanCondition;
114: }
115:
116: /** Setter for property booleanCondition.
117: * @param booleanCondition New value of property booleanCondition.
118: *
119: */
120: public void setBooleanCondition(java.lang.String booleanCondition) {
121: if (booleanCondition == null) {
122: throw new IllegalArgumentException(
123: "Missing Boolean Condition. (AND/OR)");
124: }
125: String tmp = booleanCondition.toUpperCase();
126: if (booleanCondition.equalsIgnoreCase(AND) || tmp.equals(OR)
127: || tmp.equals(AND_NESTED_LOGIC_START)
128: || tmp.equals(AND_NESTED_LOGIC_END)
129: || tmp.equals(OR_NESTED_LOGIC_START)
130: || tmp.equals(NESTED_LOGIC_END)
131: || tmp.equals(OR_NESTED_LOGIC_END)
132: || tmp.equals(OR_NESTED_LOGIC)
133: || tmp.equals(AND_NESTED_LOGIC)) {
134: } else {
135: throw new IllegalArgumentException(
136: "Invalid Boolean Condition: \n\n"
137: + booleanCondition
138: + " \n\nOnly AND/OR must be used");
139: }
140: this .booleanCondition = booleanCondition;
141: }
142:
143: public String toString() {
144: StringBuffer sb = new StringBuffer();
145: String space = " ";
146: if (functions != null) {
147: sb.append(functions.replaceFirst("${}", fieldName));
148: } else {
149: sb.append(fieldName);
150: }
151: sb.append(space).append(operator).append(space);
152: sb.append(searchValue);
153: sb.append(space).append(booleanCondition).append(space);
154:
155: return sb.toString();
156: }
157:
158: /** Getter for property searchValuesMap.
159: * @return Value of property searchValuesMap.
160: *
161: */
162: public java.util.Map getSearchValuesMap() {
163: return searchValuesMap;
164: }
165:
166: /** Setter for property searchValuesMap.
167: * @param searchValuesMap New value of property searchValuesMap.
168: *
169: */
170: public void setSearchValuesMap(java.util.Map searchValuesMap) {
171: this .searchValuesMap = searchValuesMap;
172: }
173:
174: /** Getter for property fieldLabel.
175: * @return Value of property fieldLabel.
176: *
177: */
178: public java.lang.String getFieldLabel() {
179: return fieldLabel;
180: }
181:
182: /** Setter for property fieldLabel.
183: * @param fieldLabel New value of property fieldLabel.
184: *
185: */
186: public void setFieldLabel(java.lang.String fieldLabel) {
187: this .fieldLabel = fieldLabel;
188: }
189:
190: /**
191: * Getter for property searchValuesList.
192: * @return Value of property searchValuesList.
193: */
194: public java.util.List getSearchValuesList() {
195: return searchValuesList;
196: }
197:
198: /**
199: * Setter for property searchValuesList.
200: * @param searchValuesList New value of property searchValuesList.
201: */
202: public void setSearchValuesList(java.util.List searchValuesList) {
203: this .searchValuesList = searchValuesList;
204: }
205:
206: /**
207: * Getter for property likeHint.
208: * @return Value of property likeHint.
209: */
210: public int getLikeHint() {
211: return likeHint;
212: }
213:
214: /**
215: * Setter for property likeHint.
216: * @param likeHint New value of property likeHint.
217: */
218: public void setLikeHint(int likeHint) {
219: this .likeHint = likeHint;
220: }
221:
222: /**
223: * Getter for property functions.
224: * @return Value of property functions.
225: */
226: public java.lang.String getFunctions() {
227: return this .functions;
228: }
229:
230: /**
231: * Setter for property functions.
232: * @param functions New value of property functions.
233: * Example:
234: * Assume the value of the field is 'This is a test'
235: *
236: * SearchCriteriaHolder holder = new SearchCriteriaHolder();
237: * ...
238: * holder.setFunctions("CONCAT(UPPER(SUBSTR(${}, 10)), 'XYZ')" );
239: * ...
240: * result would be: TESTXYZ
241: */
242: public void setFunctions(java.lang.String functions) {
243: this.functions = functions;
244: }
245: }
|