1 /**
2 * This file is part of the equanda project.
3 *
4 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7 *
8 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
9 * ANY KIND, either express or implied. See the License for the specific language governing rights and
10 * limitations under the License.
11 *
12 * Alternatively, the contents of this file may be used under the terms of
13 * either the GNU General Public License Version 2 or later (the "GPL"), or
14 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
15 * in which case the provisions of the GPL or the LGPL are applicable instead
16 * of those above. If you wish to allow use of your version of this file only
17 * under the terms of either the GPL or the LGPL, and not to allow others to
18 * use your version of this file under the terms of the MPL, indicate your
19 * decision by deleting the provisions above and replace them with the notice
20 * and other provisions required by the GPL or the LGPL. If you do not delete
21 * the provisions above, a recipient may use your version of this file under
22 * the terms of any one of the MPL, the GPL or the LGPL.
23 */
24
25 package org.equanda.tapestry5.translators;
26
27 import org.apache.tapestry5.*;
28 import org.apache.tapestry5.services.FormSupport;
29
30 import java.sql.Timestamp;
31
32 /**
33 * Tapestry translator for Timestamp values.
34 *
35 * @author <a href="mailto:joachim@progs.be">Joachim Van der Auwera</a>
36 */
37 public class TimestampTranslator
38 implements Translator<Timestamp>, FieldTranslator<Timestamp>
39 {
40 /**
41 * Parses blank values to null, otherwise parses the client value to a Timestamp
42 *
43 * @throws ValidationException if the clientValue can not be parsed
44 */
45 public Timestamp parseClient( Field field, String clientValue, String message )
46 throws ValidationException
47 {
48 if ( clientValue == null || clientValue.length() == 0 ) return null;
49 clientValue = clientValue.trim();
50 if ( clientValue.length() == 0 ) return null;
51
52 try
53 {
54 return new Timestamp( Timestamp.parse( clientValue.trim() ) );
55 }
56 catch ( NumberFormatException ex )
57 {
58 throw new ValidationException( message );
59 }
60 }
61
62 /**
63 * Converts null to the blank string, non-null to a string representation.
64 */
65 public String toClient( Timestamp value )
66 {
67 return value == null ? "" : value.toString();
68 }
69
70 public Class<Timestamp> getType() { return Timestamp.class; }
71
72 public String getName()
73 {
74 return "timestamp";
75 }
76
77 public String getMessageKey()
78 {
79 return "timestamp-format-exception";
80 }
81
82 public void render( Field field, String message, MarkupWriter markupWriter, FormSupport formSupport )
83 {
84 formSupport.addValidation( field, "timestamp", message, null );
85 }
86
87 public Timestamp parse( String clientValue )
88 throws ValidationException
89 {
90 return parseClient( null, clientValue, "Not parseable by TimestampTranslator" );
91 }
92
93 public void render( MarkupWriter markupWriter )
94 {
95 // @todo don't know what to do here
96 }
97 }