001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.render;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.text.DateFormat;
023: import java.util.Collection;
024: import java.util.Date;
025: import java.util.Locale;
026: import java.util.Map;
027:
028: import de.finix.contelligent.CallData;
029: import de.finix.contelligent.content.DateContent;
030: import de.finix.contelligent.exception.ContelligentException;
031: import de.finix.contelligent.logging.LoggingService;
032:
033: public class DateRenderer implements Renderer {
034: final static org.apache.log4j.Logger log = LoggingService
035: .getLogger(DateRenderer.class);
036:
037: private final static String DATE_FORMAT = "dateFormat";
038:
039: private final static String TIME_FORMAT = "timeFormat";
040:
041: private final static int HIDE = 99;
042:
043: private int defaultDateStyle = DateFormat.DEFAULT;
044:
045: private int defaultTimeStyle = DateFormat.DEFAULT;
046:
047: private DateContent content;
048:
049: private Collection sensitiveCategories;
050:
051: public DateRenderer(DateContent content) {
052: this .content = content;
053: this .sensitiveCategories = content.getSensitiveCategories();
054: }
055:
056: public DateRenderer(DateContent content, int dateStyle,
057: int timeStyle) {
058: this .content = content;
059: this .sensitiveCategories = content.getSensitiveCategories();
060: this .defaultDateStyle = dateStyle;
061: this .defaultTimeStyle = timeStyle;
062: }
063:
064: public Collection getSensitiveCategories() {
065: return sensitiveCategories;
066: }
067:
068: public ParameterDescription[] getParameterDescription() {
069: ParameterDescription dateFormat = new ParameterDescription(
070: DATE_FORMAT, "Select the rendering format of the date",
071: false, true, new String[] { "hide", "short", "medium",
072: "long", "full" });
073: ParameterDescription timeFormat = new ParameterDescription(
074: TIME_FORMAT, "Select the rendering format of the time",
075: false, true, new String[] { "hide", "short", "medium",
076: "long", "full" });
077: return new ParameterDescription[] { dateFormat, timeFormat };
078: }
079:
080: private int parseDateFormat(String format) {
081: if (format.equals("short")) {
082: return DateFormat.SHORT;
083: } else if (format.equals("medium")) {
084: return DateFormat.MEDIUM;
085: } else if (format.equals("long")) {
086: return DateFormat.LONG;
087: } else if (format.equals("full")) {
088: return DateFormat.FULL;
089: }
090: return HIDE;
091: }
092:
093: public void render(Writer writer, Map parameterMap,
094: CallData callData) throws ContelligentException,
095: IOException {
096: int dateStyle = defaultDateStyle;
097: int timeStyle = defaultTimeStyle;
098: Date date = content.getDate(callData);
099: if (date != null) {
100: Locale locale = callData.getSystem().getCategoryManager()
101: .getLocale(callData);
102: if (parameterMap != null) {
103: if (parameterMap.containsKey(DATE_FORMAT)) {
104: String[] format = (String[]) parameterMap
105: .get(DATE_FORMAT);
106: dateStyle = parseDateFormat(format[0]);
107: }
108: if (parameterMap.containsKey(TIME_FORMAT)) {
109: String[] format = (String[]) parameterMap
110: .get(TIME_FORMAT);
111: timeStyle = parseDateFormat(format[0]);
112: }
113: }
114: DateFormat formatter;
115: if (dateStyle == HIDE) {
116: formatter = DateFormat.getTimeInstance(timeStyle,
117: locale);
118: } else if (timeStyle == HIDE) {
119: formatter = DateFormat.getDateInstance(dateStyle,
120: locale);
121: } else {
122: formatter = DateFormat.getDateTimeInstance(dateStyle,
123: timeStyle, locale);
124: }
125: writer.write(formatter.format(date));
126: } else {
127: log.warn("Date not set!");
128: }
129: }
130: }
|