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.persistence.Criteria;
053: import org.jaffa.persistence.AtomicCriteria;
054: import org.jaffa.datatypes.DateTime;
055:
056: /** This is the helper class for all Finder Tx (transaction) classes.
057: * It is provided to support repetitive functions.
058: * @author GautamJ
059: */
060: public class FinderTx {
061:
062: /** This method will update the input criteria, based on the 'field' and 'name' inputs.
063: * It is invoked by the concrete class while building a Criteria object to query the database.
064: * @param field The criteria field which will have the operator and value(s).
065: * @param name The name of the field for which the criteria has been passed.
066: * @param criteria The Criteria object being built for the query, and which will be updated based on the 'field' and 'name' inputs.
067: */
068: public static void addCriteria(CriteriaField field, String name,
069: Criteria criteria) {
070: if (field != null) {
071: String operator = field.getOperator();
072: Object[] values = field.returnValuesAsObjectArray();
073: if (operator != null
074: && operator
075: .equals(CriteriaField.RELATIONAL_IS_NULL))
076: criteria.addCriteria(name, Criteria.RELATIONAL_IS_NULL);
077: else if (operator != null
078: && operator
079: .equals(CriteriaField.RELATIONAL_IS_NOT_NULL))
080: criteria.addCriteria(name,
081: Criteria.RELATIONAL_IS_NOT_NULL);
082: else if (values != null && values.length > 0) {
083: if (operator == null
084: || operator
085: .equals(CriteriaField.RELATIONAL_EQUALS))
086: criteria.addCriteria(name,
087: Criteria.RELATIONAL_EQUALS, values[0]);
088: else if (operator
089: .equals(CriteriaField.RELATIONAL_NOT_EQUALS))
090: criteria.addCriteria(name,
091: Criteria.RELATIONAL_NOT_EQUALS, values[0]);
092: else if (operator
093: .equals(CriteriaField.RELATIONAL_GREATER_THAN))
094: criteria
095: .addCriteria(name,
096: Criteria.RELATIONAL_GREATER_THAN,
097: values[0]);
098: else if (operator
099: .equals(CriteriaField.RELATIONAL_SMALLER_THAN))
100: criteria
101: .addCriteria(name,
102: Criteria.RELATIONAL_SMALLER_THAN,
103: values[0]);
104: else if (operator
105: .equals(CriteriaField.RELATIONAL_GREATER_THAN_EQUAL_TO))
106: criteria.addCriteria(name,
107: Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO,
108: values[0]);
109: else if (operator
110: .equals(CriteriaField.RELATIONAL_SMALLER_THAN_EQUAL_TO))
111: criteria.addCriteria(name,
112: Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO,
113: values[0]);
114: else if (operator
115: .equals(CriteriaField.RELATIONAL_BEGINS_WITH))
116: criteria.addCriteria(name,
117: Criteria.RELATIONAL_BEGINS_WITH, values[0]);
118: else if (operator
119: .equals(CriteriaField.RELATIONAL_ENDS_WITH))
120: criteria.addCriteria(name,
121: Criteria.RELATIONAL_ENDS_WITH, values[0]);
122: else if (operator.equals(CriteriaField.RELATIONAL_LIKE))
123: criteria.addCriteria(name,
124: Criteria.RELATIONAL_LIKE, values[0]);
125: else if (operator
126: .equals(CriteriaField.RELATIONAL_BETWEEN)) {
127: if (values.length > 0 && values[0] != null)
128: criteria
129: .addCriteria(
130: name,
131: Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO,
132: values[0]);
133: if (values.length > 1 && values[1] != null)
134: criteria
135: .addCriteria(
136: name,
137: Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO,
138: values[1]);
139: } else if (operator.equals(CriteriaField.RELATIONAL_IN)) {
140: if (values.length == 1) {
141: if (values[0] == null)
142: criteria.addCriteria(name,
143: Criteria.RELATIONAL_IS_NULL);
144: else
145: criteria.addCriteria(name,
146: Criteria.RELATIONAL_EQUALS,
147: values[0]);
148: } else {
149: AtomicCriteria atomic = new AtomicCriteria();
150: if (values[0] == null)
151: atomic.addCriteria(name,
152: Criteria.RELATIONAL_IS_NULL);
153: else
154: atomic.addCriteria(name,
155: Criteria.RELATIONAL_EQUALS,
156: values[0]);
157: for (int i = 1; i < values.length; i++) {
158: if (values[i] == null)
159: atomic.addOrCriteria(name,
160: Criteria.RELATIONAL_IS_NULL);
161: else
162: atomic.addOrCriteria(name,
163: Criteria.RELATIONAL_EQUALS,
164: values[i]);
165: }
166: criteria.addAtomic(atomic);
167: }
168: }
169: }
170: }
171: }
172:
173: /** This method will update the input criteria, based on the 'field' and 'name' inputs.
174: * It is invoked by the concrete class while building a Criteria object to query the database.
175: * @param field The criteria field which will have the operator and value(s).
176: * @param name The name of the field for which the criteria has been passed.
177: * @param criteria The Criteria object being built for the query, and which will be updated based on the 'field' and 'name' inputs.
178: */
179: public static void addCriteria(DateTimeCriteriaField field,
180: String name, Criteria criteria) {
181: if (field != null) {
182: String operator = field.getOperator();
183: Object[] values = field.returnValuesAsObjectArray();
184: if (operator != null
185: && operator
186: .equals(CriteriaField.RELATIONAL_IS_NULL))
187: criteria.addCriteria(name, Criteria.RELATIONAL_IS_NULL);
188: else if (operator != null
189: && operator
190: .equals(CriteriaField.RELATIONAL_IS_NOT_NULL))
191: criteria.addCriteria(name,
192: Criteria.RELATIONAL_IS_NOT_NULL);
193: else if (values != null && values.length > 0) {
194: if (operator == null
195: || operator
196: .equals(CriteriaField.RELATIONAL_EQUALS))
197: criteria.addAtomic(interpretDateTime(name,
198: Criteria.RELATIONAL_EQUALS,
199: (DateTime) values[0]));
200: else if (operator
201: .equals(CriteriaField.RELATIONAL_NOT_EQUALS))
202: criteria.addAtomic(interpretDateTime(name,
203: Criteria.RELATIONAL_NOT_EQUALS,
204: (DateTime) values[0]));
205: else if (operator
206: .equals(CriteriaField.RELATIONAL_GREATER_THAN))
207: criteria.addAtomic(interpretDateTime(name,
208: Criteria.RELATIONAL_GREATER_THAN,
209: (DateTime) values[0]));
210: else if (operator
211: .equals(CriteriaField.RELATIONAL_SMALLER_THAN))
212: criteria.addAtomic(interpretDateTime(name,
213: Criteria.RELATIONAL_SMALLER_THAN,
214: (DateTime) values[0]));
215: else if (operator
216: .equals(CriteriaField.RELATIONAL_GREATER_THAN_EQUAL_TO))
217: criteria.addAtomic(interpretDateTime(name,
218: Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO,
219: (DateTime) values[0]));
220: else if (operator
221: .equals(CriteriaField.RELATIONAL_SMALLER_THAN_EQUAL_TO))
222: criteria.addAtomic(interpretDateTime(name,
223: Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO,
224: (DateTime) values[0]));
225: else if (operator
226: .equals(CriteriaField.RELATIONAL_BEGINS_WITH))
227: criteria.addAtomic(interpretDateTime(name,
228: Criteria.RELATIONAL_BEGINS_WITH,
229: (DateTime) values[0]));
230: else if (operator
231: .equals(CriteriaField.RELATIONAL_ENDS_WITH))
232: criteria.addAtomic(interpretDateTime(name,
233: Criteria.RELATIONAL_ENDS_WITH,
234: (DateTime) values[0]));
235: else if (operator.equals(CriteriaField.RELATIONAL_LIKE))
236: criteria.addAtomic(interpretDateTime(name,
237: Criteria.RELATIONAL_LIKE,
238: (DateTime) values[0]));
239: else if (operator
240: .equals(CriteriaField.RELATIONAL_BETWEEN)) {
241: if (values.length > 0 && values[0] != null)
242: criteria
243: .addCriteria(
244: name,
245: Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO,
246: determineDateTimeLimit(
247: (DateTime) values[0],
248: true));
249: if (values.length > 1 && values[1] != null)
250: criteria
251: .addCriteria(
252: name,
253: Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO,
254: determineDateTimeLimit(
255: (DateTime) values[1],
256: false));
257: } else if (operator.equals(CriteriaField.RELATIONAL_IN)) {
258: if (values.length == 1) {
259: if (values[0] == null)
260: criteria.addCriteria(name,
261: Criteria.RELATIONAL_IS_NULL);
262: else
263: criteria.addAtomic(interpretDateTime(name,
264: Criteria.RELATIONAL_EQUALS,
265: (DateTime) values[0]));
266: } else {
267: AtomicCriteria atomic = new AtomicCriteria();
268: if (values[0] == null)
269: atomic.addCriteria(name,
270: Criteria.RELATIONAL_IS_NULL);
271: else
272: atomic.addAtomic(interpretDateTime(name,
273: Criteria.RELATIONAL_EQUALS,
274: (DateTime) values[0]));
275: for (int i = 1; i < values.length; i++) {
276: if (values[i] == null)
277: atomic.addOrCriteria(name,
278: Criteria.RELATIONAL_IS_NULL);
279: else
280: atomic.addOrAtomic(interpretDateTime(
281: name,
282: Criteria.RELATIONAL_EQUALS,
283: (DateTime) values[i]));
284: }
285: criteria.addAtomic(atomic);
286: }
287: }
288: }
289: }
290: }
291:
292: private static AtomicCriteria interpretDateTime(String name,
293: int operator, DateTime value) {
294: AtomicCriteria atomic = new AtomicCriteria();
295:
296: // determine the start and end of the day
297: DateTime dt1 = determineDateTimeLimit(value, true);
298: DateTime dt2 = determineDateTimeLimit(value, false);
299:
300: if (dt1.equals(dt2)) {
301: // an exact value has been passed !!!
302: atomic.addCriteria(name, operator, value);
303: } else {
304: // now perform datatime interpretation based on the operator
305: if (operator == Criteria.RELATIONAL_EQUALS) {
306: atomic.addCriteria(name,
307: Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO, dt1);
308: atomic.addCriteria(name,
309: Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO, dt2);
310: } else if (operator == Criteria.RELATIONAL_NOT_EQUALS) {
311: atomic.addCriteria(name,
312: Criteria.RELATIONAL_SMALLER_THAN, dt1);
313: atomic.addOrCriteria(name,
314: Criteria.RELATIONAL_GREATER_THAN, dt2);
315: } else if (operator == Criteria.RELATIONAL_GREATER_THAN) {
316: atomic.addCriteria(name,
317: Criteria.RELATIONAL_GREATER_THAN, dt2);
318: } else if (operator == Criteria.RELATIONAL_SMALLER_THAN) {
319: atomic.addCriteria(name,
320: Criteria.RELATIONAL_SMALLER_THAN, dt1);
321: } else if (operator == Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO) {
322: atomic.addCriteria(name,
323: Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO, dt2);
324: } else if (operator == Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO) {
325: atomic.addCriteria(name,
326: Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO, dt1);
327: } else {
328: // this should never happen
329: atomic.addCriteria(name, operator, value);
330: }
331: }
332: return atomic;
333: }
334:
335: private static DateTime determineDateTimeLimit(DateTime value,
336: boolean lowerLimit) {
337: DateTime dt = null;
338: int maxHours = 23, maxMinutes = 59, maxSeconds = 59, maxMillis = 999;
339:
340: if (value.getHourOfDay() == 0 && value.getMinute() == 0
341: && value.getSecond() == 0) {
342: if (lowerLimit)
343: dt = DateTime.addMilli(value, -(value.getMilli()));
344: else
345: dt = new DateTime(value.getYear(), value.getMonth(),
346: value.getDay(), maxHours, maxMinutes,
347: maxSeconds, maxMillis);
348: } else if (value.getMinute() == 0 && value.getSecond() == 0) {
349: if (lowerLimit)
350: dt = DateTime.addMilli(value, -(value.getMilli()));
351: else
352: dt = new DateTime(value.getYear(), value.getMonth(),
353: value.getDay(), value.getHourOfDay(),
354: maxMinutes, maxSeconds, maxMillis);
355: } else if (value.getSecond() == 0) {
356: if (lowerLimit)
357: dt = DateTime.addMilli(value, -(value.getMilli()));
358: else
359: dt = new DateTime(value.getYear(), value.getMonth(),
360: value.getDay(), value.getHourOfDay(), value
361: .getMinute(), maxSeconds, maxMillis);
362: } else {
363: dt = value;
364: }
365: return dt;
366: }
367: }
|