001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * VariableDateExpression.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function.date;
030:
031: import java.util.Calendar;
032: import java.util.Date;
033: import java.util.TimeZone;
034:
035: /**
036: * Constructs a new date by specifying the fields for the calendar either as static parameters or as parameters read
037: * from a field.
038: *
039: * @author Thomas Morgner
040: */
041: public class VariableDateExpression extends DateExpression {
042: /**
043: * The name of the field that contains the month.
044: */
045: private String monthField;
046: /**
047: * The name of the field that contains the year.
048: */
049: private String yearField;
050: /**
051: * The name of the field that contains the hour (using the 24-hours system).
052: */
053: private String hourField;
054: /**
055: * The name of the field that contains the minute.
056: */
057: private String minuteField;
058: /**
059: * The name of the field that contains the second.
060: */
061: private String secondField;
062: /**
063: * The name of the field that contains the milli-seconds.
064: */
065: private String milliSecondField;
066: /**
067: * The name of the field that contains the number of milliseconds since 01-Jan-1970.
068: */
069: private String timeField;
070: /**
071: * The name of the field that contains the day of the week.
072: */
073: private String dayOfWeekField;
074: /**
075: * The name of the field that contains the day of the year.
076: */
077: private String dayOfYearField;
078: /**
079: * The name of the field that contains the day of the month.
080: */
081: private String dayOfMonthField;
082: /**
083: * The name of the field that contains the day of the week in the current month.
084: */
085: private String dayOfWeekInMonthField;
086: /**
087: * The name of the field that contains the time-zone.
088: */
089: private String timeZoneField;
090: /**
091: * The name of the field that contains the week of the year.
092: */
093: private String weekOfYearField;
094: /**
095: * The name of the field that contains the week of the month.
096: */
097: private String weekOfMonthField;
098: /**
099: * The name of the field that contains the epoch time, that is the time in seconds since 01-Jan-1970.
100: */
101: private String epochTimeField;
102:
103: /**
104: * Default Constructor.
105: */
106: public VariableDateExpression() {
107: }
108:
109: /**
110: * Returns the name of the field that contains the epoch time, that is the time in seconds since 01-Jan-1970.
111: *
112: * @return a fieldname.
113: */
114: public String getEpochTimeField() {
115: return epochTimeField;
116: }
117:
118: /**
119: * Defines the name of the field that contains the epoch time, that is the time in seconds since 01-Jan-1970.
120: *
121: * @param epochTimeField a fieldname.
122: */
123: public void setEpochTimeField(final String epochTimeField) {
124: this .epochTimeField = epochTimeField;
125: }
126:
127: /**
128: * Returns the name of the field that contains the month.
129: *
130: * @return a fieldname.
131: */
132: public String getMonthField() {
133: return monthField;
134: }
135:
136: /**
137: * Defines the name of the field that contains the month.
138: *
139: * @param monthField a fieldname.
140: */
141: public void setMonthField(final String monthField) {
142: this .monthField = monthField;
143: }
144:
145: /**
146: * Returns the name of the field that contains the year.
147: *
148: * @return a fieldname.
149: */
150: public String getYearField() {
151: return yearField;
152: }
153:
154: /**
155: * Defines the name of the field that contains the year.
156: *
157: * @param yearField a fieldname.
158: */
159: public void setYearField(final String yearField) {
160: this .yearField = yearField;
161: }
162:
163: /**
164: * Returns the name of the field that contains the hour of the day (using the 24-hour system).
165: *
166: * @return a fieldname.
167: */
168: public String getHourField() {
169: return hourField;
170: }
171:
172: /**
173: * Defines the name of the field that contains the hour of the day.
174: *
175: * @param hourField a fieldname.
176: */
177: public void setHourField(final String hourField) {
178: this .hourField = hourField;
179: }
180:
181: /**
182: * Returns the name of the field that contains the minute.
183: *
184: * @return a fieldname.
185: */
186: public String getMinuteField() {
187: return minuteField;
188: }
189:
190: /**
191: * Defines the name of the field that contains the minute.
192: *
193: * @param minuteField a fieldname.
194: */
195: public void setMinuteField(final String minuteField) {
196: this .minuteField = minuteField;
197: }
198:
199: /**
200: * Returns the name of the field that contains the second.
201: *
202: * @return a fieldname.
203: */
204: public String getSecondField() {
205: return secondField;
206: }
207:
208: /**
209: * Defines the name of the field that contains the second.
210: *
211: * @param secondField a fieldname.
212: */
213: public void setSecondField(final String secondField) {
214: this .secondField = secondField;
215: }
216:
217: /**
218: * Returns the name of the field that contains the milliseconds.
219: *
220: * @return a fieldname.
221: */
222: public String getMilliSecondField() {
223: return milliSecondField;
224: }
225:
226: /**
227: * Defines the name of the field that contains the milliseconds.
228: *
229: * @param milliSecondField a fieldname.
230: */
231: public void setMilliSecondField(final String milliSecondField) {
232: this .milliSecondField = milliSecondField;
233: }
234:
235: /**
236: * Returns the name of the field that contains the time in milli-seconds since 01-Jan-1970.
237: *
238: * @return a fieldname.
239: */
240: public String getTimeField() {
241: return timeField;
242: }
243:
244: /**
245: * Defines the name of the field that contains the time in milli-seconds since 01-Jan-1970.
246: *
247: * @param timeField a fieldname.
248: */
249: public void setTimeField(final String timeField) {
250: this .timeField = timeField;
251: }
252:
253: /**
254: * Returns the name of the field that contains the day-of-the-week.
255: *
256: * @return a fieldname.
257: */
258: public String getDayOfWeekField() {
259: return dayOfWeekField;
260: }
261:
262: /**
263: * Defines the name of the field that contains the day of the week.
264: *
265: * @param dayOfWeekField a fieldname.
266: */
267: public void setDayOfWeekField(final String dayOfWeekField) {
268: this .dayOfWeekField = dayOfWeekField;
269: }
270:
271: /**
272: * Returns the name of the field that contains the day of the year.
273: *
274: * @return a fieldname.
275: */
276: public String getDayOfYearField() {
277: return dayOfYearField;
278: }
279:
280: /**
281: * Defines the name of the field that contains the day of the year.
282: *
283: * @param dayOfYearField a fieldname.
284: */
285: public void setDayOfYearField(final String dayOfYearField) {
286: this .dayOfYearField = dayOfYearField;
287: }
288:
289: /**
290: * Returns the name of the field that contains the day of the month.
291: *
292: * @return a fieldname.
293: */
294: public String getDayOfMonthField() {
295: return dayOfMonthField;
296: }
297:
298: /**
299: * Defines the name of the field that contains the day of the month.
300: *
301: * @param dayOfMonthField a fieldname.
302: */
303: public void setDayOfMonthField(final String dayOfMonthField) {
304: this .dayOfMonthField = dayOfMonthField;
305: }
306:
307: /**
308: * Returns the name of the field that contains the day of the week in the current month.
309: *
310: * @return a fieldname.
311: */
312: public String getDayOfWeekInMonthField() {
313: return dayOfWeekInMonthField;
314: }
315:
316: /**
317: * Defines the name of the field that contains the day of the week in the current month.
318: *
319: * @param dayOfWeekInMonthField a fieldname.
320: */
321: public void setDayOfWeekInMonthField(
322: final String dayOfWeekInMonthField) {
323: this .dayOfWeekInMonthField = dayOfWeekInMonthField;
324: }
325:
326: /**
327: * Returns the name of the field that contains the time-zone.
328: *
329: * @return a fieldname.
330: */
331: public String getTimeZoneField() {
332: return timeZoneField;
333: }
334:
335: /**
336: * Defines the name of the field that contains the time-zone.
337: *
338: * @param timeZoneField a fieldname.
339: */
340: public void setTimeZoneField(final String timeZoneField) {
341: this .timeZoneField = timeZoneField;
342: }
343:
344: /**
345: * Defines the name of the field that contains the week-of-the-year.
346: *
347: * @return a fieldname.
348: */
349: public String getWeekOfYearField() {
350: return weekOfYearField;
351: }
352:
353: /**
354: * Defines the name of the field that contains the week of the year.
355: *
356: * @param weekOfYearField a fieldname.
357: */
358: public void setWeekOfYearField(final String weekOfYearField) {
359: this .weekOfYearField = weekOfYearField;
360: }
361:
362: /**
363: * Defines the name of the field that contains the week of the month.
364: *
365: * @return a fieldname.
366: */
367: public String getWeekOfMonthField() {
368: return weekOfMonthField;
369: }
370:
371: /**
372: * Returns the name of the field that contains the week of the month.
373: *
374: * @param weekOfMonthField a fieldname.
375: */
376: public void setWeekOfMonthField(final String weekOfMonthField) {
377: this .weekOfMonthField = weekOfMonthField;
378: }
379:
380: /**
381: * Configures the given Calendar instance by applying all defined fields to it.
382: *
383: * @param calendar the week of the year property.
384: */
385: protected void configureCalendar(final Calendar calendar) {
386: // first add the hardcoded values, if any ...
387: super .configureCalendar(calendar);
388:
389: // then the variable values ..
390: if (timeField != null) {
391: final Object o = getDataRow().get(timeField);
392: if (o instanceof Number) {
393: final Number n = (Number) o;
394: calendar.setTime(new Date(n.longValue()));
395: } else if (o instanceof Date) {
396: final Date d = (Date) o;
397: calendar.setTime(d);
398: }
399: }
400:
401: if (epochTimeField != null) {
402: final Object o = getDataRow().get(epochTimeField);
403: if (o instanceof Number) {
404: final Number n = (Number) o;
405: calendar.setTime(new Date(n.longValue() * 1000));
406: }
407: }
408: if (monthField != null) {
409: trySetField(calendar, Calendar.MONTH, monthField);
410: }
411: if (dayOfMonthField != null) {
412: trySetField(calendar, Calendar.DAY_OF_MONTH,
413: dayOfMonthField);
414: }
415: if (yearField != null) {
416: trySetField(calendar, Calendar.YEAR, yearField);
417: }
418: if (hourField != null) {
419: trySetField(calendar, Calendar.HOUR_OF_DAY, hourField);
420: }
421: if (minuteField != null) {
422: trySetField(calendar, Calendar.MINUTE, minuteField);
423: }
424: if (secondField != null) {
425: trySetField(calendar, Calendar.SECOND, secondField);
426: }
427: if (milliSecondField != null) {
428: trySetField(calendar, Calendar.MILLISECOND,
429: milliSecondField);
430: }
431: if (dayOfWeekField != null) {
432: trySetField(calendar, Calendar.DAY_OF_WEEK, dayOfWeekField);
433: }
434: if (dayOfYearField != null) {
435: trySetField(calendar, Calendar.DAY_OF_YEAR, dayOfYearField);
436: }
437: if (dayOfWeekInMonthField != null) {
438: trySetField(calendar, Calendar.DAY_OF_WEEK_IN_MONTH,
439: dayOfWeekInMonthField);
440: }
441: if (weekOfMonthField != null) {
442: trySetField(calendar, Calendar.WEEK_OF_MONTH,
443: weekOfMonthField);
444: }
445: if (weekOfYearField != null) {
446: trySetField(calendar, Calendar.WEEK_OF_YEAR,
447: weekOfYearField);
448: }
449: if (timeZoneField != null) {
450: final Object o = getDataRow().get(getTimeZoneField());
451: if (o instanceof String) {
452: calendar.setTimeZone(TimeZone.getTimeZone((String) o));
453: } else if (o instanceof TimeZone) {
454: calendar.setTimeZone((TimeZone) o);
455: }
456: }
457: }
458:
459: /**
460: * A helper method tha tries to update a field from a column in the data-row. The calendar is only updated, if the
461: * field contains a Number.
462: *
463: * @param calendar the calendar that should be updated
464: * @param field the field as specified in the Calendar class
465: * @param column the data-row column from where to read the number
466: */
467: private void trySetField(final Calendar calendar, final int field,
468: final String column) {
469: if (column == null) {
470: return;
471: }
472: final Object o = getDataRow().get(column);
473: if (o instanceof Number == false) {
474: return;
475: }
476: final Number n = (Number) o;
477: calendar.set(field, n.intValue());
478: }
479: }
|