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: * DateExpression.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: import org.jfree.report.ResourceBundleFactory;
036: import org.jfree.report.function.AbstractExpression;
037:
038: /**
039: * The DateExpression can be used to construct a static date object. The Calendar object used as base is initialized to
040: * the current date. Fields that are not set, are ignored.
041: * <p/>
042: * To construct dates from values read from the data-row, use the {@link VariableDateExpression} instead.
043: *
044: * @author Thomas Morgner
045: */
046: public class DateExpression extends AbstractExpression {
047: /**
048: * A property holding the month of the year.
049: */
050: private Integer month;
051: /**
052: * A property holding the year.
053: */
054: private Integer year;
055: /**
056: * A property holding the hour of the day. This property uses the 24-hour system.
057: */
058: private Integer hour;
059: /**
060: * A property holding the minute.
061: */
062: private Integer minute;
063: /**
064: * A property holding the second.
065: */
066: private Integer second;
067: /**
068: * A property holding milli-seconds.
069: */
070: private Integer milliSecond;
071: /**
072: * A property holding time in milli-seconds since 01-01-1970.
073: */
074: private Long time;
075: /**
076: * A property holding time in seconds since 01-01-1970.
077: */
078: private Long epochTime;
079: /**
080: * A property holding the day of the week.
081: */
082: private Integer dayOfWeek;
083: /**
084: * A property holding the day of the year.
085: */
086: private Integer dayOfYear;
087: /**
088: * A property holding the day of the month.
089: */
090: private Integer dayOfMonth;
091: /**
092: * A property holding the day of the week in the month.
093: */
094: private Integer dayOfWeekInMonth;
095: /**
096: * A property holding the time-zone.
097: */
098: private TimeZone timeZone;
099: /**
100: * A property holding the week of the year.
101: */
102: private Integer weekOfYear;
103: /**
104: * A property holding the week of the month.
105: */
106: private Integer weekOfMonth;
107:
108: /**
109: * Default Constructor.
110: */
111: public DateExpression() {
112: }
113:
114: /**
115: * Returns the current time-zone.
116: *
117: * @return the time-zone or null, if none is set.
118: */
119: public TimeZone getTimeZone() {
120: return timeZone;
121: }
122:
123: /**
124: * Defines the timezone. If none is defined here, the locale's default timezone is used instead.
125: *
126: * @param timeZone the time-zone.
127: */
128: public void setTimeZone(final TimeZone timeZone) {
129: this .timeZone = timeZone;
130: }
131:
132: /**
133: * Returns the month property.
134: *
135: * @return the month property.
136: */
137: public Integer getMonth() {
138: return month;
139: }
140:
141: /**
142: * Defines the month property.
143: *
144: * @param month the month property.
145: */
146: public void setMonth(final Integer month) {
147: this .month = month;
148: }
149:
150: /**
151: * Returns the day property. This returns the day within the current month.
152: *
153: * @return the day of the month property.
154: */
155: public Integer getDay() {
156: return dayOfMonth;
157: }
158:
159: /**
160: * Defines the day property. This defines the day within the current month.
161: *
162: * @param day the day of the month property.
163: */
164: public void setDay(final Integer day) {
165: this .dayOfMonth = day;
166: }
167:
168: /**
169: * Returns the year property.
170: *
171: * @return the year property.
172: */
173: public Integer getYear() {
174: return year;
175: }
176:
177: /**
178: * Defines the year property.
179: *
180: * @param year the year property.
181: */
182: public void setYear(final Integer year) {
183: this .year = year;
184: }
185:
186: /**
187: * Returns the hour of the day property. This uses the 24-hour system.
188: *
189: * @return the hour property.
190: */
191: public Integer getHour() {
192: return hour;
193: }
194:
195: /**
196: * Defines the hour property. This uses the 24-hour system
197: *
198: * @param hour the hour property.
199: */
200: public void setHour(final Integer hour) {
201: this .hour = hour;
202: }
203:
204: /**
205: * Returns the minute property.
206: *
207: * @return the minute property.
208: */
209: public Integer getMinute() {
210: return minute;
211: }
212:
213: /**
214: * Defines the minute property.
215: *
216: * @param minute the minute property.
217: */
218: public void setMinute(final Integer minute) {
219: this .minute = minute;
220: }
221:
222: /**
223: * Returns the second property.
224: *
225: * @return the second property.
226: */
227: public Integer getSecond() {
228: return second;
229: }
230:
231: /**
232: * Defines the second property.
233: *
234: * @param second the second property.
235: */
236: public void setSecond(final Integer second) {
237: this .second = second;
238: }
239:
240: /**
241: * Returns the milli-second property.
242: *
243: * @return the milli-second property.
244: */
245: public Integer getMilliSecond() {
246: return milliSecond;
247: }
248:
249: /**
250: * Defines the year property.
251: *
252: * @param milliSecond the milli-seconds property.
253: */
254: public void setMilliSecond(final Integer milliSecond) {
255: this .milliSecond = milliSecond;
256: }
257:
258: /**
259: * Returns the time in milli-seconds since 01-Jan-1970.
260: * @return the time in milli-seconds since 01-Jan-1970.
261: */
262: public Long getTime() {
263: return time;
264: }
265:
266: /**
267: * Defines the time in milli-seconds since 01-Jan-1970.
268: * @param time the time in milli-seconds since 01-Jan-1970.
269: */
270: public void setTime(final Long time) {
271: this .time = time;
272: }
273:
274: /**
275: * Returns the time in seconds since 01-Jan-1970.
276: * @return the time in seconds since 01-Jan-1970.
277: */
278: public Long getEpochTime() {
279: return epochTime;
280: }
281:
282: /**
283: * Defines the time in seconds since 01-Jan-1970.
284: * @param epochTime the time in seconds since 01-Jan-1970.
285: */
286: public void setEpochTime(final Long epochTime) {
287: this .epochTime = epochTime;
288: }
289:
290: /**
291: * Returns the day of the week property.
292: *
293: * @return the day-of-the-week property.
294: */
295: public Integer getDayOfWeek() {
296: return dayOfWeek;
297: }
298:
299: /**
300: * Defines the day of the week property.
301: *
302: * @param dayOfWeek the day-of-the-week property.
303: */
304: public void setDayOfWeek(final Integer dayOfWeek) {
305: this .dayOfWeek = dayOfWeek;
306: }
307:
308: /**
309: * Returns the day of the year property.
310: *
311: * @return the day-of-the-year property.
312: */
313: public Integer getDayOfYear() {
314: return dayOfYear;
315: }
316:
317: /**
318: * Defines the day of the year property.
319: *
320: * @param dayOfYear the day-of-the-year property.
321: */
322: public void setDayOfYear(final Integer dayOfYear) {
323: this .dayOfYear = dayOfYear;
324: }
325:
326: /**
327: * Returns the day of the month property.
328: *
329: * @return the day-of-the-month property.
330: */
331: public Integer getDayOfMonth() {
332: return dayOfMonth;
333: }
334:
335: /**
336: * Defines the day of the month property.
337: *
338: * @param dayOfMonth the day-of-the-month property.
339: */
340: public void setDayOfMonth(final Integer dayOfMonth) {
341: this .dayOfMonth = dayOfMonth;
342: }
343:
344: /**
345: * Returns the day of the week in the month property.
346: *
347: * @return the day of the week in the month property.
348: */
349: public Integer getDayOfWeekInMonth() {
350: return dayOfWeekInMonth;
351: }
352:
353: /**
354: * Defines the day of the week in the month property.
355: *
356: * @param dayOfWeekInMonth the day of the week in the month property.
357: */
358: public void setDayOfWeekInMonth(final Integer dayOfWeekInMonth) {
359: this .dayOfWeekInMonth = dayOfWeekInMonth;
360: }
361:
362: /**
363: * Returns the week of the year property.
364: *
365: * @return the week of the year property.
366: */
367: public Integer getWeekOfYear() {
368: return weekOfYear;
369: }
370:
371: /**
372: * Defines the week of the year property.
373: *
374: * @param weekOfYear the week of the year property.
375: */
376: public void setWeekOfYear(final Integer weekOfYear) {
377: this .weekOfYear = weekOfYear;
378: }
379:
380: public Integer getWeekOfMonth() {
381: return weekOfMonth;
382: }
383:
384: public void setWeekOfMonth(final Integer weekOfMonth) {
385: this .weekOfMonth = weekOfMonth;
386: }
387:
388: /**
389: * Return the current expression value. <P> The value depends (obviously) on the expression implementation.
390: *
391: * @return the value of the function.
392: */
393: public Object getValue() {
394: final Calendar calendar = getCalendar();
395: configureCalendar(calendar);
396: return calendar.getTime();
397: }
398:
399: /**
400: * Configures the given Calendar instance by applying all defined fields to it.
401: *
402: * @param calendar the week of the year property.
403: */
404: protected void configureCalendar(final Calendar calendar) {
405: if (time != null) {
406: calendar.setTime(new Date(time.longValue()));
407: }
408: if (epochTime != null) {
409: calendar.setTime(new Date(epochTime.longValue() * 1000));
410: }
411: if (month != null) {
412: calendar.set(Calendar.MONTH, month.intValue());
413: }
414: if (dayOfMonth != null) {
415: calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth.intValue());
416: }
417: if (year != null) {
418: calendar.set(Calendar.YEAR, year.intValue());
419: }
420: if (hour != null) {
421: calendar.set(Calendar.HOUR_OF_DAY, hour.intValue());
422: }
423: if (minute != null) {
424: calendar.set(Calendar.MINUTE, minute.intValue());
425: }
426: if (second != null) {
427: calendar.set(Calendar.SECOND, second.intValue());
428: }
429: if (milliSecond != null) {
430: calendar.set(Calendar.MILLISECOND, milliSecond.intValue());
431: }
432: if (dayOfWeek != null) {
433: calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek.intValue());
434: }
435: if (dayOfYear != null) {
436: calendar.set(Calendar.DAY_OF_YEAR, dayOfYear.intValue());
437: }
438: if (dayOfWeekInMonth != null) {
439: calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH,
440: dayOfWeekInMonth.intValue());
441: }
442: if (weekOfYear != null) {
443: calendar.set(Calendar.WEEK_OF_YEAR, weekOfYear.intValue());
444: }
445: if (weekOfMonth != null) {
446: calendar
447: .set(Calendar.WEEK_OF_MONTH, weekOfMonth.intValue());
448: }
449: if (timeZone != null) {
450: calendar.setTimeZone(getTimeZone());
451: }
452: }
453:
454: /**
455: * Create a new calendar instance. This implementation uses Calendar.getInstance(..) to create the Calendar,
456: * and therefore the result depends on the locale of the report.
457: *
458: * @return the calendar.
459: */
460: protected Calendar getCalendar() {
461: final ResourceBundleFactory rf = getResourceBundleFactory();
462: return Calendar.getInstance(rf.getLocale());
463: }
464: }
|