001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: DateFieldValueConvertor.java,v $
031: * Revision 1.8 2005/10/11 18:55:29 colinmacleod
032: * Fixed some checkstyle and javadoc issues.
033: *
034: * Revision 1.7 2005/10/02 14:06:32 colinmacleod
035: * Added/improved log4j logging.
036: *
037: * Revision 1.6 2005/09/16 13:39:02 colinmacleod
038: * Created new convertor class to handle timestamps.
039: *
040: * Revision 1.5 2005/09/14 12:40:29 colinmacleod
041: * Added handling for Timestamp instances.
042: *
043: * Revision 1.4 2005/04/11 12:27:01 colinmacleod
044: * Added preliminary support for filters.
045: * Added FieldValueConvertor factor interface
046: * to split off value convertors for reuse.
047: *
048: * Revision 1.3 2005/04/09 18:04:15 colinmacleod
049: * Changed copyright text to GPL v2 explicitly.
050: *
051: * Revision 1.2 2005/01/06 22:13:21 colinmacleod
052: * Moved up a version number.
053: * Changed copyright notices to 2005.
054: * Updated the documentation:
055: * - started working on multiproject:site docu.
056: * - changed the logo.
057: * Added checkstyle and fixed LOADS of style issues.
058: * Added separate thirdparty subproject.
059: * Added struts (in web), util and webgui (in webtheme) from ivata op.
060: *
061: * Revision 1.1 2004/12/29 20:07:06 colinmacleod
062: * Renamed subproject masks to mask.
063: *
064: * Revision 1.1.1.1 2004/05/16 20:40:32 colinmacleod
065: * Ready for 0.1 release
066: * -----------------------------------------------------------------------------
067: */
068: package com.ivata.mask.field.date;
069:
070: import org.apache.log4j.Logger;
071:
072: import java.text.DateFormat;
073: import java.text.ParseException;
074: import java.text.SimpleDateFormat;
075: import java.util.Date;
076:
077: import com.ivata.mask.field.FieldValueConvertor;
078:
079: /**
080: * <p>
081: * Convert an object representing a date to a string.
082: * </p>
083: *
084: * @since ivata masks 0.1 (2004-04-14)
085: * @author Colin MacLeod
086: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
087: * @version $Revision: 1.8 $
088: */
089: public class DateFieldValueConvertor extends FieldValueConvertor {
090: /**
091: * Logger for this class.
092: */
093: private static final Logger logger = Logger
094: .getLogger(DateFieldValueConvertor.class);
095:
096: /**
097: * Serialization version (for <code>Serializable</code> interface).
098: */
099: private static final long serialVersionUID = 1L;
100: /**
101: * <p>
102: * This format does all the hard work!
103: * </p>
104: */
105: private DateFormat format;
106:
107: /**
108: * <p>
109: * Construct a new value convertor from the format pattern supplied.
110: * </p>
111: *
112: * @param pattern
113: * number format pattern to use to convert the number. See
114: * {@link java.text.SimpleDateFormat}.
115: */
116: public DateFieldValueConvertor(final String pattern) {
117: format = new SimpleDateFormat(pattern);
118: }
119:
120: /**
121: * <p>
122: * Convert an object representing a date to a string.
123: * </p>
124: *
125: * @param objectValue
126: * object to be converted.
127: * @return string equivalent of the object provided.
128: */
129: protected String toString(final Object objectValue) {
130: if (logger.isDebugEnabled()) {
131: logger.debug("toString(Object objectValue = " + objectValue
132: + ") - start");
133: }
134:
135: if (objectValue == null) {
136: if (logger.isDebugEnabled()) {
137: logger
138: .debug("toString(Object) - end - return value = ");
139: }
140: return "";
141: }
142: Date date = (Date) objectValue;
143: String returnString = format.format(date);
144: if (logger.isDebugEnabled()) {
145: logger.debug("toString(Object) - end - return value = "
146: + returnString);
147: }
148: return returnString;
149: }
150:
151: /**
152: * <p>
153: * Convert a date value from a string.
154: * </p>
155: *
156: * @param propertyClass
157: * exact class to be converted to.
158: * @param stringValue
159: * value to be converted.
160: * @return valid object value converted from a string.
161: * @see com.ivata.mask.field.FieldValueConvertor#convertFromString
162: */
163: public Object convertFromString(final Class propertyClass,
164: final String stringValue) {
165: if (logger.isDebugEnabled()) {
166: logger.debug("convertFromString(Class propertyClass = "
167: + propertyClass + ", String stringValue = "
168: + stringValue + ") - start");
169: }
170:
171: try {
172: Date returnDate = format.parse(stringValue);
173: // assume it is a plain old date
174:
175: if (logger.isDebugEnabled()) {
176: logger
177: .debug("convertFromString - end - return value = "
178: + returnDate);
179: }
180: return returnDate;
181: } catch (ParseException e) {
182: logger.error("convertFromString(Class, String)", e);
183:
184: throw new FieldValueException(e);
185: }
186: }
187: }
|