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 java.math.BigDecimal;
23
24 import org.apache.torque.om.NumberKey;
25 import org.apache.turbine.services.intake.IntakeException;
26 import org.apache.turbine.services.intake.validator.NumberKeyValidator;
27 import org.apache.turbine.services.intake.xmlmodel.XmlField;
28
29 /***
30 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
31 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
32 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
33 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
34 * @version $Id: NumberKeyField.java 646288 2008-04-09 11:51:16Z tv $
35 */
36 public class NumberKeyField
37 extends Field
38 {
39
40 /***
41 * Constructor.
42 *
43 * @param field xml field definition object
44 * @param group xml group definition object
45 * @throws IntakeException thrown by superclass
46 */
47 public NumberKeyField(XmlField field, Group group)
48 throws IntakeException
49 {
50 super(field, group);
51 }
52
53 /***
54 * Sets the default value for a NumberKey field
55 *
56 * @param prop Parameter for the default values
57 */
58 public void setDefaultValue(String prop)
59 {
60 if (prop == null)
61 {
62 return;
63 }
64
65 defaultValue = new NumberKey(prop);
66 }
67
68 /***
69 * Set the empty Value. This value is used if Intake
70 * maps a field to a parameter returned by the user and
71 * the corresponding field is either empty (empty string)
72 * or non-existant.
73 *
74 * @param prop The value to use if the field is empty.
75 */
76 public void setEmptyValue(String prop)
77 {
78 emptyValue = null;
79
80 if (prop == null)
81 {
82 return;
83 }
84
85 emptyValue = new NumberKey(prop);
86 }
87
88 /***
89 * A suitable validator.
90 *
91 * @return A suitable validator
92 */
93 protected String getDefaultValidator()
94 {
95 return NumberKeyValidator.class.getName();
96 }
97
98 /***
99 * Sets the value of the field from data in the parser.
100 */
101 protected void doSetValue()
102 {
103 if (isMultiValued)
104 {
105 BigDecimal[] inputs = parser.getBigDecimals(getKey());
106 NumberKey[] values = new NumberKey[inputs.length];
107
108 for (int i = 0; i < inputs.length; i++)
109 {
110 values[i] = inputs[i] == null
111 ? (NumberKey) getEmptyValue()
112 : new NumberKey(inputs[i]);
113 }
114
115 setTestValue(values);
116 }
117 else
118 {
119 BigDecimal value = parser.getBigDecimal(getKey());
120 if (value == null)
121 {
122 setTestValue(getEmptyValue());
123 }
124 else
125 {
126 setTestValue(new NumberKey(value));
127 }
128 }
129 }
130 }