1 /*****************************************************************************
5 * Creation date 01.03.2013
6 * @author Author: Roman Kozlov
8 *****************************************************************************/
10 package org.splat.conversion;
12 import java.text.ParseException;
13 import java.text.SimpleDateFormat;
14 import java.util.Date;
17 import org.apache.commons.lang3.StringUtils;
18 import org.apache.commons.lang3.time.DateUtils;
19 import org.apache.struts2.util.StrutsTypeConverter;
20 import org.splat.i18n.I18nUtils;
22 import com.opensymphony.xwork2.ActionContext;
25 * Date conversion methods for Struts 2.<BR>
26 * To enable the date conversion for all action classes put into the classpath the file <code>xwork-conversion.properties</code>
27 * containing the following line:<BR>
28 * <code>java.util.Date=org.splat.conversion.DateConverter</code><BR>
30 * To enable the date conversion for the specific Struts 2 action class, place the file named
31 * <code>{ActionClassName}-conversion.properties</code> to the package of the action class.<BR>
34 * class: <code>....CustomAction</code><BR>
35 * property file path: <code>classes/.../CustomAction-conversion.properties</code> <BR>
36 * This property file should contain a line for each form property.<BR>
39 * <code>createdAfter=org.splat.conversion.DateConverter<BR>
40 * filter.createdAfter=org.splat.conversion.DateConverter</code>
42 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
44 public class DateConverter extends StrutsTypeConverter {
49 @SuppressWarnings("unchecked")
51 public Object convertFromString(final Map context, final String[] values,
52 final Class toClass) { // NOPMD: extends StrutsTypeConverter
55 if (null != values && values.length > 0) {
58 if (StringUtils.isNotEmpty(value)) {
60 result = DateUtils.parseDateStrictly(values[0], I18nUtils
61 .getMessage(ActionContext.getContext().getLocale(),
64 // date can not be null so value is not in a good format
65 throw new ParseException("Bad format", 0);
67 } catch (ParseException e) {
68 throw new IllegalArgumentException(
69 "Cannot parse: " + values[0], e);
78 @SuppressWarnings("unchecked")
80 public String convertToString(final Map context, final Object o) { // NOPMD: extends StrutsTypeConverter
82 if (o instanceof Date) {
83 result = (new SimpleDateFormat(I18nUtils.getMessage(ActionContext
84 .getContext().getLocale(), "date.format"), ActionContext
85 .getContext().getLocale())).format((Date) o);
86 } else if (null == o) {
87 result = StringUtils.EMPTY;
89 result = o.toString();