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: TimestampFieldValueConvertor.java,v $
031: * Revision 1.3 2005/10/11 18:55:29 colinmacleod
032: * Fixed some checkstyle and javadoc issues.
033: *
034: * Revision 1.2 2005/10/02 14:06:32 colinmacleod
035: * Added/improved log4j logging.
036: *
037: * Revision 1.1 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.sql.Timestamp;
073: import java.util.Date;
074:
075: import com.ivata.mask.field.FieldValueConvertor;
076:
077: /**
078: * <p>
079: * Convert an object representing a timestamp to a string. This uses a string
080: * representation of the time in milliseconds to convert - it's useful for
081: * hidden fields (not for human-readable formats; use the
082: * <code>DateFieldValueConvertor</code> for those).
083: * </p>
084: *
085: * @since ivata masks 1.0 (2005-09-15)
086: * @author Colin MacLeod
087: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
088: * @version $Revision: 1.3 $
089: */
090: public class TimestampFieldValueConvertor extends FieldValueConvertor {
091: /**
092: * Logger for this class.
093: */
094: private static final Logger logger = Logger
095: .getLogger(TimestampFieldValueConvertor.class);
096:
097: /**
098: * Serialization version (for <code>Serializable</code> interface).
099: */
100: private static final long serialVersionUID = 1L;
101:
102: /**
103: * <p>
104: * Convert a date value from a string.
105: * </p>
106: *
107: * @param propertyClass
108: * exact class to be converted to.
109: * @param stringValue
110: * value to be converted.
111: * @return valid object value converted from a string.
112: * @see com.ivata.mask.field.FieldValueConvertor#convertFromString
113: */
114: public Object convertFromString(final Class propertyClass,
115: final String stringValue) {
116: if (logger.isDebugEnabled()) {
117: logger.debug("convertFromString(Class propertyClass = "
118: + propertyClass + ", String stringValue = "
119: + stringValue + ") - start");
120: }
121:
122: try {
123: Long time = new Long(stringValue);
124: Object returnObject = new Timestamp(time.longValue());
125: if (logger.isDebugEnabled()) {
126: logger
127: .debug("convertFromString - end - return value = "
128: + returnObject);
129: }
130: return returnObject;
131: } catch (NumberFormatException e) {
132: logger.error("convertFromString(Class, String)", e);
133:
134: throw new FieldValueException(e);
135: }
136: }
137:
138: /**
139: * <p>
140: * Convert an object representing a timestamp to a string.
141: * </p>
142: *
143: * @param objectValue
144: * object to be converted.
145: * @return string equivalent of the object provided.
146: */
147: protected String toString(final Object objectValue) {
148: if (logger.isDebugEnabled()) {
149: logger.debug("toString(Object objectValue = " + objectValue
150: + ") - start");
151: }
152:
153: if (objectValue == null) {
154: if (logger.isDebugEnabled()) {
155: logger
156: .debug("toString(Object) - end - return value = ");
157: }
158: return "";
159: }
160: Date date = (Date) objectValue;
161: String returnString = Long.toString(date.getTime());
162: if (logger.isDebugEnabled()) {
163: logger.debug("toString(Object) - end - return value = "
164: + returnString);
165: }
166: return returnString;
167: }
168: }
|