View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.util;
24  
25  import java.text.DateFormat;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  
29  import org.apache.commons.beanutils.ConversionException;
30  import org.apache.commons.lang.StringUtils;
31  
32  /**
33   * This class is converts a java.util.Date to a String and a String to a
34   * java.util.Date for use as a Timestamp. It is used by BeanUtils when copying
35   * properties.
36   * 
37   * @author <a href="mailto:dan@getrolling.com">Dan Kibler</a>
38   */
39  public class TimestampConverter extends DateConverter {
40      /**
41       * i18n-ized timestamp format - based on values in
42       * ApplicationResources.properties
43       */
44      public static final String TS_FORMAT = DateUtil.getDatePattern()
45  	    + " HH:mm:ss.S";
46  
47      /**
48       * Convert a String to a date
49       * 
50       * @param type
51       *                java.util.Date
52       * @param value
53       *                the String value
54       * @return a converted date
55       */
56      @SuppressWarnings("unchecked")
57      protected Object convertToDate(Class type, Object value) {
58  	DateFormat df = new SimpleDateFormat(TS_FORMAT);
59  	if (value instanceof String) {
60  	    try {
61  		if (StringUtils.isEmpty(value.toString())) {
62  		    return null;
63  		}
64  
65  		return df.parse((String) value);
66  	    } catch (Exception pe) {
67  		throw new ConversionException(
68  			"Error converting String to Timestamp");
69  	    }
70  	}
71  
72  	throw new ConversionException("Could not convert "
73  		+ value.getClass().getName() + " to " + type.getName());
74      }
75  
76      /**
77       * Convert from a java.util.Date to a String
78       * 
79       * @param type
80       *                java.lang.String
81       * @param value
82       *                the date instance
83       * @return string version of date using default date pattern
84       */
85      @SuppressWarnings("unchecked")
86      @Override
87      protected Object convertToString(Class type, Object value) {
88  	DateFormat df = new SimpleDateFormat(TS_FORMAT);
89  	if (value instanceof Date) {
90  	    try {
91  		return df.format(value);
92  	    } catch (Exception e) {
93  		throw new ConversionException(
94  			"Error converting Timestamp to String");
95  	    }
96  	}
97  
98  	return value.toString();
99      }
100 }