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
20
21
22 import org.apache.commons.lang.StringUtils;
23
24 import org.apache.turbine.services.intake.IntakeException;
25 import org.apache.turbine.services.intake.validator.LongValidator;
26 import org.apache.turbine.services.intake.xmlmodel.XmlField;
27
28 /***
29 * Processor for long fields.
30 *
31 * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
32 * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
33 * @version $Id: LongField.java 534527 2007-05-02 16:10:59Z tv $
34 */
35 public class LongField
36 extends Field
37 {
38
39 /***
40 * Constructor.
41 *
42 * @param field xml field definition object
43 * @param group xml group definition object
44 * @throws IntakeException thrown by superclass
45 */
46 public LongField(XmlField field, Group group)
47 throws IntakeException
48 {
49 super(field, group);
50 }
51
52 /***
53 * Sets the default value for an Long Field
54 *
55 * @param prop Parameter for the default values
56 */
57 public void setDefaultValue(String prop)
58 {
59 defaultValue = null;
60
61 if (prop == null)
62 {
63 return;
64 }
65
66 defaultValue = new Long(prop);
67 }
68
69 /***
70 * Set the empty Value. This value is used if Intake
71 * maps a field to a parameter returned by the user and
72 * the corresponding field is either empty (empty string)
73 * or non-existant.
74 *
75 * @param prop The value to use if the field is empty.
76 */
77 public void setEmptyValue(String prop)
78 {
79 emptyValue = null;
80
81 if (prop == null)
82 {
83 return;
84 }
85
86 emptyValue = new Long(prop);
87 }
88
89 /***
90 * Provides access to emptyValue such that the value returned will be
91 * acceptable as an argument parameter to Method.invoke. Subclasses
92 * that deal with primitive types should ensure that they return an
93 * appropriate value wrapped in the object wrapper class for the
94 * primitive type.
95 *
96 * @return the value to use when the field is empty or an Object that
97 * wraps the empty value for primitive types.
98 */
99 protected Object getSafeEmptyValue()
100 {
101 if (isMultiValued)
102 {
103 return new long[0];
104 }
105 else
106 {
107 return (null == getEmptyValue()) ? new Long(0l) : getEmptyValue();
108 }
109 }
110
111 /***
112 * A suitable validator.
113 *
114 * @return A suitable validator
115 */
116 protected String getDefaultValidator()
117 {
118 return LongValidator.class.getName();
119 }
120
121 /***
122 * Sets the value of the field from data in the parser.
123 */
124 protected void doSetValue()
125 {
126 if (isMultiValued)
127 {
128 String[] inputs = parser.getStrings(getKey());
129 long[] values = new long[inputs.length];
130 for (int i = 0; i < inputs.length; i++)
131 {
132 values[i] = StringUtils.isNotEmpty(inputs[i])
133 ? new Long(inputs[i]).longValue()
134 : ((Long) getEmptyValue()).longValue();
135 }
136 setTestValue(values);
137 }
138 else
139 {
140 String val = parser.getString(getKey());
141 setTestValue(StringUtils.isNotEmpty(val)
142 ? new Long(val) : (Long) getEmptyValue());
143 }
144 }
145
146 }