1 package org.apache.fulcrum.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.List;
23
24 import org.apache.fulcrum.intake.IntakeException;
25 import org.apache.fulcrum.intake.model.Field;
26 import org.apache.fulcrum.intake.model.Group;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class FieldReference
63 {
64 public static enum Comparison
65 {
66
67 LT ("less-than"),
68
69
70 GT ("greater-than"),
71
72
73 LTE ("less-than-or-equal"),
74
75
76 GTE ("greater-than-or-equal");
77
78 private final String text;
79
80 private Comparison(String text)
81 {
82 this.text = text;
83 }
84
85 @Override
86 public String toString()
87 {
88 return text;
89 }
90
91 public static Comparison fromString(String string)
92 {
93 if (string != null)
94 {
95 for (Comparison c : Comparison.values())
96 {
97 if (string.equals(c.text))
98 {
99 return c;
100 }
101 }
102 }
103
104 return null;
105 }
106 }
107
108
109 private Comparison compare = null;
110
111
112 private String fieldName = null;
113
114
115 private String message = null;
116
117
118
119
120 public FieldReference()
121 {
122
123 }
124
125
126
127
128 public Comparison getComparison()
129 {
130 return compare;
131 }
132
133
134
135
136 public void setComparison(Comparison compare)
137 {
138 this.compare = compare;
139 }
140
141
142
143
144 public String getFieldName()
145 {
146 return fieldName;
147 }
148
149
150
151
152 public void setFieldName(String fieldName)
153 {
154 this.fieldName = fieldName;
155 }
156
157
158
159
160 public String getMessage()
161 {
162 return message;
163 }
164
165
166
167
168 public void setMessage(String message)
169 {
170 this.message = message;
171 }
172
173
174
175
176
177
178
179 public static Comparison getComparisonType(String key)
180 {
181 return Comparison.fromString(key);
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196 public static <T> void checkReferences(List<FieldReference> fieldReferences, CompareCallback<T> compareCallback,
197 T value, Group group)
198 throws ValidationException
199 {
200 for (FieldReference ref : fieldReferences)
201 {
202 boolean comp_true = true;
203
204 try
205 {
206 @SuppressWarnings("unchecked")
207 Field<T> refField = (Field<T>) group.get(ref.getFieldName());
208
209 if (refField.isSet())
210 {
211
212
213
214
215
216 if (!refField.isValidated())
217 {
218 refField.validate();
219 }
220
221 if (refField.isValid())
222 {
223 comp_true = compareCallback.compareValues(ref.getComparison(),
224 value,
225 refField.getValue());
226 }
227 }
228 }
229 catch (IntakeException e)
230 {
231 throw new ValidationException(ref.getMessage());
232 }
233
234 if (comp_true == false)
235 {
236 throw new ValidationException(ref.getMessage());
237 }
238 }
239 }
240 }