1 package org.apache.turbine.services.intake.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.text.DateFormat;
20 import java.text.ParseException;
21
22 import java.util.Date;
23
24 import org.apache.commons.lang.StringUtils;
25
26 import org.apache.turbine.services.intake.IntakeException;
27 import org.apache.turbine.services.intake.validator.DateStringValidator;
28 import org.apache.turbine.services.intake.xmlmodel.XmlField;
29 import org.apache.turbine.util.TurbineRuntimeException;
30
31 /***
32 * Field for date inputs as free form text. The parsing of date strings
33 * is dependent on any rules that are defined, so this field will expect that
34 * any validator will be (or extend) DateStringValidator.
35 *
36 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
37 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
38 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
39 * @version $Id: DateStringField.java 264148 2005-08-29 14:21:04Z henning $
40 */
41 public class DateStringField
42 extends Field
43 {
44 /*** date format. Fallback if no validator is defined */
45 private static DateFormat df;
46
47 static
48 {
49 df = DateFormat.getInstance();
50 df.setLenient(true);
51 }
52
53 /***
54 * Constructor.
55 *
56 * @param field xml field definition object
57 * @param group xml group definition object
58 * @throws IntakeException thrown by superclass
59 */
60 public DateStringField(XmlField field, Group group)
61 throws IntakeException
62 {
63 super(field, group);
64 }
65
66 /***
67 * Sets the default value for a DateString field
68 *
69 * @param prop Parameter for the default values
70 */
71 public void setDefaultValue(String prop)
72 {
73 defaultValue = null;
74
75 if (prop == null)
76 {
77 return;
78 }
79
80 try
81 {
82 defaultValue = getDate(prop);
83 }
84 catch (ParseException e)
85 {
86 throw new TurbineRuntimeException("Could not parse " + prop
87 + " into a valid Date for the default value", e);
88 }
89 }
90
91 /***
92 * Set the empty Value. This value is used if Intake
93 * maps a field to a parameter returned by the user and
94 * the corresponding field is either empty (empty string)
95 * or non-existant.
96 *
97 * @param prop The value to use if the field is empty.
98 */
99 public void setEmptyValue(String prop)
100 {
101 emptyValue = null;
102
103 if (prop == null)
104 {
105 return;
106 }
107
108 try
109 {
110 emptyValue = getDate(prop);
111 }
112 catch (ParseException e)
113 {
114 throw new TurbineRuntimeException("Could not parse " + prop
115 + " into a valid Date for the empty value", e);
116 }
117 }
118
119 /***
120 * A suitable validator.
121 *
122 * @return "DateStringValidator"
123 */
124 protected String getDefaultValidator()
125 {
126 return DateStringValidator.class.getName();
127 }
128
129 /***
130 * Sets the value of the field from data in the parser.
131 */
132 protected void doSetValue()
133 {
134 if (isMultiValued)
135 {
136 String[] inputs = parser.getStrings(getKey());
137 Date[] values = new Date[inputs.length];
138 for (int i = 0; i < inputs.length; i++)
139 {
140 try
141 {
142 values[i] = StringUtils.isNotEmpty(inputs[i])
143 ? getDate(inputs[i]) : (Date) getEmptyValue();
144 }
145 catch (ParseException e)
146 {
147 values[i] = null;
148 }
149 }
150 setTestValue(values);
151 }
152 else
153 {
154 String val = parser.getString(getKey());
155 try
156 {
157 setTestValue(StringUtils.isNotEmpty(val) ? getDate(val) : (Date) getEmptyValue());
158 }
159 catch (ParseException e)
160 {
161 setTestValue(null);
162 }
163 }
164 }
165
166 /***
167 * Parses a test date string using the Validator if is exists and
168 * is an instance of DateStringValidator. Otherwise, DateFormat.parse()
169 * is used.
170 *
171 * @param dateString The string date to parse
172 * @return A <code>Date</code> object
173 * @throws ParseException The date could not be parsed.
174 */
175 private Date getDate(String dateString)
176 throws ParseException
177 {
178 Date date = null;
179
180 if (validator != null && validator instanceof DateStringValidator)
181 {
182 date = ((DateStringValidator) validator).parse(dateString);
183 }
184 else
185 {
186 date = df.parse(dateString);
187 }
188 return date;
189 }
190
191 /***
192 * returns a String representation
193 *
194 * @return a String representation
195 */
196 public String toString()
197 {
198 String s = null;
199 Object value = getValue();
200 if (value == null)
201 {
202 s = "";
203 }
204 else if (value instanceof String)
205 {
206 s = (String) value;
207 }
208 else if (validator != null && validator instanceof DateStringValidator)
209 {
210 s = ((DateStringValidator) validator).format((Date) value);
211 }
212 else
213 {
214 s = df.format((Date) value);
215 }
216 return s;
217 }
218 }