01: package org.vraptor.converter.joda;
02:
03: import org.joda.time.LocalDate;
04: import org.vraptor.LogicRequest;
05: import org.vraptor.converter.ConversionException;
06: import org.vraptor.converter.Converter;
07: import org.vraptor.converter.basic.LocaleCalendarDateConverter;
08:
09: import java.util.Calendar;
10:
11: /**
12: * <p/>
13: * Locale based joda-time {@link org.joda.time.LocalDate} converter. Uses the error key
14: * invalid_date if unable to parse its information.
15: * </p>
16: * <p/>
17: * Same semantics as {@link org.vraptor.converter.LocaleCalendarTimeConverter}.
18: * </p>
19: *
20: * @author Fabio Kung
21: */
22: public class LocalDateConverter implements Converter {
23: private final Converter internal = new LocaleCalendarDateConverter();
24:
25: public Object convert(String value, Class<?> type,
26: LogicRequest context) throws ConversionException {
27: if (value == null || value.equals("")) {
28: return null;
29: }
30:
31: Calendar converted = (Calendar) internal.convert(value,
32: Calendar.class, context);
33: try {
34: return LocalDate.fromCalendarFields(converted);
35: } catch (Exception e) {
36: throw new ConversionException("invalid_date",
37: "Unable to parse string " + value, e);
38: }
39: }
40:
41: public Class<?>[] getSupportedTypes() {
42: return new Class<?>[] { LocalDate.class };
43: }
44: }
|