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.Iterator;
23 import java.util.List;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.turbine.services.intake.IntakeException;
28 import org.apache.turbine.services.intake.model.Field;
29 import org.apache.turbine.services.intake.model.Group;
30
31 /***
32 * Helper Class to manage relations between fields. The following
33 * comparisons are supported:
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 FieldReference
65 {
66 /*** a local logger */
67 protected static final Log log = LogFactory.getLog(FieldReference.class);
68
69 /*** Rule name for "<" comparison */
70 public static final String RANGE_LT = "less-than";
71
72 /*** Rule name for ">" comparison */
73 public static final String RANGE_GT = "greater-than";
74
75 /*** Rule name for "<=" comparison */
76 public static final String RANGE_LTE = "less-than-or-equal";
77
78 /*** Rule name for ">=" comparison */
79 public static final String RANGE_GTE = "greater-than-or-equal";
80
81 /*** Integer value for "<" comparison */
82 public static final int COMPARE_LT = 1;
83
84 /*** Integer value for ">" comparison */
85 public static final int COMPARE_GT = 2;
86
87 /*** Integer value for "<=" comparison */
88 public static final int COMPARE_LTE = 3;
89
90 /*** Integer value for ">=" comparison */
91 public static final int COMPARE_GTE = 4;
92
93 /*** Numeric comparison */
94 private int compare = 0;
95
96 /*** Name of referenced field */
97 private String fieldName = null;
98
99 /*** Error message */
100 private String message = null;
101
102 /***
103 * Constructor
104 */
105 public FieldReference()
106 {
107 }
108
109 /***
110 * @return the comparison type
111 */
112 public int getCompare()
113 {
114 return compare;
115 }
116
117 /***
118 * @param compare the comparison type to set
119 */
120 public void setCompare(int compare)
121 {
122 this.compare = compare;
123 }
124
125 /***
126 * @return the field name
127 */
128 public String getFieldName()
129 {
130 return fieldName;
131 }
132
133 /***
134 * @param fieldName the field name to set
135 */
136 public void setFieldName(String fieldName)
137 {
138 this.fieldName = fieldName;
139 }
140
141 /***
142 * @return the message
143 */
144 public String getMessage()
145 {
146 return message;
147 }
148
149 /***
150 * @param message the message to set
151 */
152 public void setMessage(String message)
153 {
154 this.message = message;
155 }
156
157 /***
158 * Map the comparison strings to their numeric counterparts
159 *
160 * @param key the
161 * @return
162 */
163 public static int getCompareType(String key)
164 {
165 int compareType = 0;
166
167 if (key.equals(RANGE_LT))
168 {
169 compareType = COMPARE_LT;
170 }
171 else if (key.equals(RANGE_LTE))
172 {
173 compareType = COMPARE_LTE;
174 }
175 else if (key.equals(RANGE_GT))
176 {
177 compareType = COMPARE_GT;
178 }
179 else if (key.equals(RANGE_GTE))
180 {
181 compareType = COMPARE_GTE;
182 }
183
184 return compareType;
185 }
186
187 /***
188 * Check the parsed value against the referenced fields
189 *
190 * @param fieldReferences List of field references to check
191 * @param compareCallback Callback to the actual compare operation
192 * @param value the parsed value of the related field
193 * @param group the group the related field belongs to
194 *
195 * @throws ValidationException
196 */
197 public static void checkReferences(List fieldReferences, CompareCallback compareCallback,
198 Object value, Group group)
199 throws ValidationException
200 {
201 for (Iterator i = fieldReferences.iterator(); i.hasNext();)
202 {
203 FieldReference ref = (FieldReference)i.next();
204 boolean comp_true = true;
205
206 try
207 {
208 Field refField = group.get(ref.getFieldName());
209
210 if (refField.isSet())
211 {
212
213
214
215
216
217 if (!refField.isValidated())
218 {
219 refField.validate();
220 }
221
222 if (refField.isValid())
223 {
224 try
225 {
226 comp_true = compareCallback.compareValues(ref.getCompare(),
227 value,
228 refField.getValue());
229 }
230 catch (ClassCastException e)
231 {
232 throw new IntakeException("Type mismatch comparing " +
233 value + " with " + refField.getValue(), e);
234 }
235 }
236 }
237 }
238 catch (IntakeException e)
239 {
240 log.error("Validate operation failed.", e);
241 throw new ValidationException(ref.getMessage());
242 }
243
244 if (comp_true == false)
245 {
246 throw new ValidationException(ref.getMessage());
247 }
248 }
249 }
250 }