1 package org.apache.turbine.services.intake.validator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.turbine.services.intake.IntakeException;
29 import org.apache.turbine.services.intake.model.Field;
30 import org.apache.turbine.services.intake.model.Group;
31
32 /***
33 * Validates an int field in dependency on another int field.
34 *
35 * <table>
36 * <tr>
37 * <th>Name</th><th>Valid Values</th><th>Default Value</th>
38 * </tr>
39 * <tr>
40 * <td>less-than</td>
41 * <td><name of other field></td>
42 * <td> </td>
43 * </tr>
44 * <tr>
45 * <td>greater-than</td>
46 * <td><name of other field></td>
47 * <td> </td>
48 * </tr>
49 * <tr>
50 * <td>less-than-or-equal</td>
51 * <td><name of other field></td>
52 * <td> </td>
53 * </tr>
54 * <tr>
55 * <td>greater-than-or-equal</td>
56 * <td><name of other field></td>
57 * <td> </td>
58 * </tr>
59 * </table>
60 *
61 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
62 * @version $Id: DateStringValidator.java 534527 2007-05-02 16:10:59Z tv $
63 */
64 public class IntegerRangeValidator
65 extends IntegerValidator
66 {
67 /*** List of FieldReferences for multiple comparisons */
68 List fieldReferences;
69
70 /*** Callback for the actual compare operation */
71 CompareCallback compareCallback;
72
73 public IntegerRangeValidator(final Map paramMap)
74 throws IntakeException
75 {
76 init(paramMap);
77 }
78
79 /***
80 * Default constructor
81 */
82 public IntegerRangeValidator()
83 {
84 super();
85 }
86
87 /***
88 * Constructor to use when initialising Object
89 *
90 * @param paramMap
91 * @throws InvalidMaskException
92 */
93 public void init(final Map paramMap)
94 throws InvalidMaskException
95 {
96 super.init(paramMap);
97
98 compareCallback = new CompareCallback()
99 {
100 /***
101 * Compare the given values using the compare operation provided
102 *
103 * @param compare type of compare operation
104 * @param thisValue value of this field
105 * @param refValue value of the reference field
106 *
107 * @return the result of the comparison
108 */
109 public boolean compareValues(int compare, Object thisValue, Object refValue)
110 throws ClassCastException
111 {
112 boolean result = true;
113
114 Integer thisInt = (Integer)thisValue;
115 Integer otherInt = (Integer)refValue;
116
117 switch (compare)
118 {
119 case FieldReference.COMPARE_LT:
120 result = thisInt.compareTo(otherInt) < 0;
121 break;
122
123 case FieldReference.COMPARE_LTE:
124 result = thisInt.compareTo(otherInt) <= 0;
125 break;
126
127 case FieldReference.COMPARE_GT:
128 result = thisInt.compareTo(otherInt) > 0;
129 break;
130
131 case FieldReference.COMPARE_GTE:
132 result = thisInt.compareTo(otherInt) >= 0;
133 break;
134 }
135
136 return result;
137 }
138 };
139
140 fieldReferences = new ArrayList(10);
141
142 for (Iterator i = paramMap.entrySet().iterator(); i.hasNext();)
143 {
144 Map.Entry entry = (Map.Entry)i.next();
145 String key = (String)entry.getKey();
146 Constraint constraint = (Constraint)entry.getValue();
147
148 int compare = FieldReference.getCompareType(key);
149
150 if (compare != 0)
151 {
152
153 FieldReference fieldref = new FieldReference();
154 fieldref.setCompare(compare);
155 fieldref.setFieldName(constraint.getValue());
156 fieldref.setMessage(constraint.getMessage());
157
158 fieldReferences.add(fieldref);
159 }
160 }
161
162 if (fieldReferences.isEmpty())
163 {
164 log.warn("No reference field rules have been found.");
165 }
166 }
167
168 /***
169 * Determine whether a testValue meets the criteria specified
170 * in the constraints defined for this validator
171 *
172 * @param testField a <code>Field</code> to be tested
173 * @exception ValidationException containing an error message if the
174 * testValue did not pass the validation tests.
175 */
176 public void assertValidity(final Field testField)
177 throws ValidationException
178 {
179 super.assertValidity(testField);
180
181 Group thisGroup = testField.getGroup();
182
183 if (testField.isMultiValued())
184 {
185 String[] stringValues = (String[])testField.getTestValue();
186
187 for (int i = 0; i < stringValues.length; i++)
188 {
189 assertValidity(stringValues[i], thisGroup);
190 }
191 }
192 else
193 {
194 String testValue = (String)testField.getTestValue();
195
196 assertValidity(testValue, thisGroup);
197 }
198 }
199
200 /***
201 * Determine whether a testValue meets the criteria specified
202 * in the constraints defined for this validator
203 *
204 * @param testValue a <code>String</code> to be tested
205 * @param group the group this field belongs to
206 *
207 * @exception ValidationException containing an error message if the
208 * testValue did not pass the validation tests.
209 */
210 public void assertValidity(final String testValue, final Group group)
211 throws ValidationException
212 {
213 if (required || StringUtils.isNotEmpty(testValue))
214 {
215 Integer testInt = new Integer(testValue);
216
217 try
218 {
219 FieldReference.checkReferences(fieldReferences, compareCallback,
220 testInt, group);
221 }
222 catch (ValidationException e)
223 {
224 errorMessage = e.getMessage();
225 throw e;
226 }
227 }
228 }
229 }