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.sql.Timestamp;
26  import java.text.DateFormat;
27  import java.text.SimpleDateFormat;
28  import java.util.Date;
29  
30  import org.apache.commons.beanutils.ConversionException;
31  import org.apache.commons.beanutils.Converter;
32  import org.apache.commons.lang.StringUtils;
33  
34  /**
35   * This class is converts a java.util.Date to a String and a String to a
36   * java.util.Date.
37   * 
38   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
39   */
40  public class DateConverter implements Converter {
41  
42      /**
43       * Convert a date to a String and a String to a Date
44       * 
45       * @param type
46       *                String, Date or Timestamp
47       * @param value
48       *                value to convert
49       * @return Converted value for property population
50       */
51      @SuppressWarnings("unchecked")
52      public Object convert(Class type, Object value) {
53  	if (value == null) {
54  	    return null;
55  	} else if (type == Timestamp.class) {
56  	    return convertToDate(type, value, DateUtil.getDateTimePattern());
57  	} else if (type == Date.class) {
58  	    return convertToDate(type, value, DateUtil.getDatePattern());
59  	} else if (type == String.class) {
60  	    return convertToString(type, value);
61  	}
62  
63  	throw new ConversionException("Could not convert "
64  		+ value.getClass().getName() + " to " + type.getName());
65      }
66  
67      /**
68       * Convert a String to a Date with the specified pattern.
69       * 
70       * @param type
71       *                String
72       * @param value
73       *                value of String
74       * @param pattern
75       *                date pattern to parse with
76       * @return Converted value for property population
77       */
78      @SuppressWarnings("unchecked")
79      protected Object convertToDate(Class type, Object value, String pattern) {
80  	DateFormat df = new SimpleDateFormat(pattern);
81  	if (value instanceof String) {
82  	    try {
83  		if (StringUtils.isEmpty(value.toString())) {
84  		    return null;
85  		}
86  
87  		Date date = df.parse((String) value);
88  		if (type.equals(Timestamp.class)) {
89  		    return new Timestamp(date.getTime());
90  		}
91  		return date;
92  	    } catch (Exception pe) {
93  		throw new ConversionException("Error converting String to Date");
94  	    }
95  	}
96  
97  	throw new ConversionException("Could not convert "
98  		+ value.getClass().getName() + " to " + type.getName());
99      }
100 
101     /**
102      * Convert a java.util.Date to a String
103      * 
104      * @param type
105      *                Date or Timestamp
106      * @param value
107      *                value to convert
108      * @return Converted value for property population
109      */
110     @SuppressWarnings("unchecked")
111     protected Object convertToString(Class type, Object value) {
112 
113 	if (value instanceof Date) {
114 	    DateFormat df = new SimpleDateFormat(DateUtil.getDatePattern());
115 	    if (value instanceof Timestamp) {
116 		df = new SimpleDateFormat(DateUtil.getDateTimePattern());
117 	    }
118 
119 	    try {
120 		return df.format(value);
121 	    } catch (Exception e) {
122 		throw new ConversionException("Error converting Date to String");
123 	    }
124 	} else {
125 	    return value.toString();
126 	}
127     }
128 }