001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.components.finder;
051:
052: import org.jaffa.datatypes.DateTime;
053: import java.util.*;
054: import org.apache.log4j.Logger;
055: import org.jaffa.metadata.DateTimeFieldMetaData;
056: import org.jaffa.datatypes.Parser;
057: import org.jaffa.datatypes.exceptions.FormatDateTimeException;
058: import org.jaffa.util.StringHelper;
059:
060: /**
061: * This class will be used by the Finder components to hold a DateTime criteria.
062: */
063: public class DateTimeCriteriaField implements CriteriaField {
064:
065: private static final Logger log = Logger
066: .getLogger(DateTimeCriteriaField.class);
067: private String m_operator = null;
068: private List m_values = null;
069:
070: /** Adds a Criteria.
071: * @param operator the operator of the criteria.
072: * @param value the value of the criteria.
073: * @throws IllegalArgumentException if the operator is null. The exception will also be thrown if the value is null and the operator is neither 'IsNull' nor 'IsNotNull'.
074: */
075: public DateTimeCriteriaField(String operator, DateTime value)
076: throws IllegalArgumentException {
077: this (operator, value != null ? new DateTime[] { value } : null);
078: }
079:
080: /** Adds a Criteria.
081: * @param operator the operator of the criteria.
082: * @param values the value array of the criteria.
083: * @throws IllegalArgumentException if the operator is null. The exception will also be thrown if the value is null and the operator is neither 'IsNull' nor 'IsNotNull'.
084: */
085: public DateTimeCriteriaField(String operator, DateTime[] values)
086: throws IllegalArgumentException {
087: if (operator == null) {
088: String str = "DateTimeCriteriaField operator cannot be null";
089: log.error(str);
090: throw new IllegalArgumentException(str);
091: } else if (values == null || values.length == 0) {
092: if (!operator.equals(CriteriaField.RELATIONAL_IS_NULL)
093: && !operator
094: .equals(CriteriaField.RELATIONAL_IS_NOT_NULL)) {
095: String str = "DateTimeCriteriaField value cannot be null";
096: log.error(str);
097: throw new IllegalArgumentException(str);
098: }
099: }
100:
101: m_operator = operator;
102: if (values != null)
103: m_values = Arrays.asList(values);
104: }
105:
106: /** Getter for property operator.
107: * @return Value of property operator.
108: */
109: public String getOperator() {
110: return m_operator;
111: }
112:
113: /** Getter for property values.
114: * This basically invokes the getValues() method.
115: * @return An array of values for the Criteria.
116: */
117: public Object[] returnValuesAsObjectArray() {
118: return getValues();
119: }
120:
121: /** Getter for property values.
122: * @return An array of values for the Criteria.
123: */
124: public DateTime[] getValues() {
125: return m_values != null ? (DateTime[]) m_values
126: .toArray(new DateTime[0]) : null;
127: }
128:
129: /** Returns diagnostic information.
130: * @return diagnostic information.
131: */
132: public String toString() {
133: StringBuffer buf = new StringBuffer("<DateTimeCriteriaField>");
134: buf.append("<operator>");
135: if (m_operator != null)
136: buf.append(m_operator);
137: buf.append("</operator>");
138: if (m_values != null) {
139: for (Iterator i = m_values.iterator(); i.hasNext();) {
140: Object value = i.next();
141: buf.append("<value>");
142: if (value != null)
143: buf.append(value);
144: buf.append("</value>");
145: }
146: }
147: buf.append("</DateTimeCriteriaField>");
148: return buf.toString();
149: }
150:
151: /** This will generate a CriteriaField object based on the input parameters.
152: * @param operator The operator of the criteria.
153: * @param value The value for the criteria. Multiple values should be separated by comma.
154: * @param meta The FieldMetaData object to obtain the layout for parsing.
155: * @return a CriteriaField object based on the input parameters.
156: * @throws FormatDateTimeException if the value is incorrectly formatted.
157: */
158: public static DateTimeCriteriaField getDateTimeCriteriaField(
159: String operator, String value, DateTimeFieldMetaData meta)
160: throws FormatDateTimeException {
161: DateTimeCriteriaField criteriaField = null;
162: DateTime nullValue = null;
163:
164: if (value != null)
165: value = value.trim();
166:
167: if (value != null && value.length() > 0) {
168: List values = new ArrayList();
169: if (RELATIONAL_BETWEEN.equals(operator)
170: || RELATIONAL_IN.equals(operator)) {
171: // replace ",," with ", ,"
172: value = StringHelper.replace(value,
173: CONSECUTIVE_SEPARATORS,
174: CONSECUTIVE_SEPARATORS_WITH_SPACE);
175:
176: if (value
177: .startsWith(SEPARATOR_FOR_IN_BETWEEN_OPERATORS))
178: values.add(null);
179:
180: StringTokenizer tknzr = new StringTokenizer(value,
181: SEPARATOR_FOR_IN_BETWEEN_OPERATORS);
182: while (tknzr.hasMoreTokens())
183: parseAndAdd(tknzr.nextToken().trim(), meta, values);
184:
185: if (value.endsWith(SEPARATOR_FOR_IN_BETWEEN_OPERATORS))
186: values.add(null);
187: } else {
188: parseAndAdd(value, meta, values);
189: }
190: if (values.size() > 0)
191: criteriaField = new DateTimeCriteriaField(operator,
192: (DateTime[]) values.toArray(new DateTime[0]));
193: else
194: criteriaField = new DateTimeCriteriaField(operator,
195: nullValue);
196: } else
197: criteriaField = new DateTimeCriteriaField(operator,
198: nullValue);
199: return criteriaField;
200: }
201:
202: private static void parseAndAdd(String str,
203: DateTimeFieldMetaData meta, List values)
204: throws FormatDateTimeException {
205: try {
206: DateTime parsedValue = null;
207: if (str != null && str.length() > 0) {
208: if (meta != null)
209: parsedValue = Parser.parseDateTime(str, meta
210: .getLayout());
211: else
212: parsedValue = Parser.parseDateTime(str);
213: }
214: values.add(parsedValue);
215: } catch (FormatDateTimeException e) {
216: e.setField(meta != null ? meta.getLabelToken() : "");
217: throw e;
218: }
219: }
220:
221: /** Default constructor.
222: * NOTE: This was added to support tools, which create instances of this class by bean introspection. In all other cases, it is recommended to use the static methods to instantiate this class.
223: */
224: public DateTimeCriteriaField() {
225: m_operator = null;
226: m_values = null;
227: }
228:
229: /** Setter for the property operator.
230: * NOTE: This was added to support tools, which create instances of this class by bean introspection. In all other cases, it is recommended to use the static methods to instantiate this class.
231: * @param operator The value of the property operator.
232: */
233: public void setOperator(String operator) {
234: m_operator = operator;
235: }
236:
237: /** Setter for the property values.
238: * NOTE: This was added to support tools, which create instances of this class by bean introspection. In all other cases, it is recommended to use the static methods to instantiate this class.
239: * @param values The value of the property values.
240: */
241: public void setValues(DateTime[] values) {
242: if (values != null) {
243: m_values = new ArrayList();
244: for (int i = 0; i < values.length; i++) {
245: Object value = values[i];
246: m_values.add(value);
247: }
248: } else {
249: m_values = null;
250: }
251: }
252:
253: }
|