]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/conversion/DateConverter.java
Salome HOME
Search actions have been improved. Searching by dates has been implemented.
[tools/siman.git] / Workspace / Siman / src / org / splat / conversion / DateConverter.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   01.03.2013
6  * @author         Author: Roman Kozlov
7  * @version        Revision: 
8  *****************************************************************************/
9
10 package org.splat.conversion;
11
12 import java.text.ParseException;
13 import java.text.SimpleDateFormat;
14 import java.util.Date;
15 import java.util.Map;
16
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;
21
22 import com.opensymphony.xwork2.ActionContext;
23
24 /**
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>
29  * <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>
32  * 
33  * For example: <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>
37  * <BR>
38  * For example: <BR>
39  * <code>createdAfter=org.splat.conversion.DateConverter<BR>
40  * filter.createdAfter=org.splat.conversion.DateConverter</code>
41  * 
42  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
43  */
44 public class DateConverter extends StrutsTypeConverter {
45
46         /**
47          * {@inheritDoc}
48          */
49         @SuppressWarnings("unchecked")
50         @Override
51         public Object convertFromString(final Map context, final String[] values,
52                         final Class toClass) { // NOPMD: extends StrutsTypeConverter
53                 Object result = null;
54                 String value = null;
55                 if (null != values && values.length > 0) {
56                         value = values[0];
57                 }
58                 if (StringUtils.isNotEmpty(value)) {
59                         try {
60                                 result = DateUtils.parseDateStrictly(values[0], I18nUtils
61                                                 .getMessage(ActionContext.getContext().getLocale(),
62                                                                 "date.format"));
63                                 if (result == null) {
64                                         // date can not be null so value is not in a good format
65                                         throw new ParseException("Bad format", 0);
66                                 }
67                         } catch (ParseException e) {
68                                 throw new IllegalArgumentException(
69                                                 "Cannot parse: " + values[0], e);
70                         }
71                 }
72                 return result;
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         @SuppressWarnings("unchecked")
79         @Override
80         public String convertToString(final Map context, final Object o) { // NOPMD: extends StrutsTypeConverter
81                 String result;
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;
88                 } else {
89                         result = o.toString();
90                 }
91                 return result;
92         }
93
94 }