1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.contract.model.validation; |
17 | |
|
18 | |
import org.kuali.student.contract.model.Constraint; |
19 | |
import org.kuali.student.contract.model.Dictionary; |
20 | |
import org.kuali.student.contract.model.DictionaryModel; |
21 | |
import org.kuali.student.contract.model.Field; |
22 | |
import org.kuali.student.contract.model.XmlType; |
23 | |
import org.kuali.student.contract.model.util.ModelFinder; |
24 | |
|
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Collection; |
27 | |
import java.util.HashSet; |
28 | |
import java.util.List; |
29 | |
import java.util.Set; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class DictionaryValidator implements ModelValidator |
36 | |
{ |
37 | |
|
38 | |
private Dictionary dict; |
39 | |
private DictionaryModel model; |
40 | |
private ModelFinder finder; |
41 | |
public DictionaryValidator (Dictionary dict, DictionaryModel model) |
42 | 0 | { |
43 | 0 | this.dict = dict; |
44 | 0 | this.model = model; |
45 | 0 | this.finder = new ModelFinder (model); |
46 | 0 | } |
47 | |
|
48 | |
private Collection errors; |
49 | |
|
50 | |
@Override |
51 | |
public Collection<String> validate () |
52 | |
{ |
53 | 0 | ConstraintValidator cv = new ConstraintValidator (dict.getInlineConstraint ()); |
54 | 0 | errors = cv.validate (); |
55 | 0 | validateForDuplicates (); |
56 | 0 | if (dict.getPrimitive ().equalsIgnoreCase (XmlType.COMPLEX)) |
57 | |
{ |
58 | 0 | for (Constraint cons : getAllConstraints ()) |
59 | |
{ |
60 | 0 | validateComplexConstraint (cons); |
61 | |
} |
62 | |
} |
63 | 0 | return errors; |
64 | |
} |
65 | |
|
66 | |
private List<Constraint> getAllConstraints () |
67 | |
{ |
68 | 0 | List<Constraint> list = getFieldNamedConstraints (); |
69 | 0 | Field field = findField (); |
70 | 0 | if (field != null) |
71 | |
{ |
72 | 0 | list.add (field.getInlineConstraint ()); |
73 | |
} |
74 | 0 | list.addAll (getDictionaryAdditionalConstraints ()); |
75 | 0 | list.add (dict.getInlineConstraint ()); |
76 | 0 | return list; |
77 | |
} |
78 | |
|
79 | |
private List<Constraint> getAllNamedConstraints () |
80 | |
{ |
81 | 0 | List<Constraint> list = getFieldNamedConstraints (); |
82 | 0 | list.addAll (getDictionaryAdditionalConstraints ()); |
83 | 0 | return list; |
84 | |
} |
85 | |
|
86 | |
|
87 | |
private List<Constraint> getFieldNamedConstraints () |
88 | |
{ |
89 | 0 | List<Constraint> list = new ArrayList (); |
90 | 0 | Field field = findField (); |
91 | 0 | if (field != null) |
92 | |
{ |
93 | 0 | for (String id : field.getConstraintIds ()) |
94 | |
{ |
95 | 0 | Constraint cons = findConstraint (id); |
96 | 0 | if (cons != null) |
97 | |
{ |
98 | 0 | list.add (cons); |
99 | |
} |
100 | 0 | } |
101 | |
} |
102 | 0 | return list; |
103 | |
} |
104 | |
|
105 | |
private List<Constraint> getDictionaryAdditionalConstraints () |
106 | |
{ |
107 | 0 | List<Constraint> list = new ArrayList (); |
108 | 0 | for (String id : dict.getAdditionalConstraintIds ()) |
109 | |
{ |
110 | 0 | Constraint cons = findConstraint (id); |
111 | 0 | if (cons != null) |
112 | |
{ |
113 | 0 | list.add (cons); |
114 | |
} |
115 | 0 | } |
116 | 0 | list.add (dict.getInlineConstraint ()); |
117 | 0 | return list; |
118 | |
} |
119 | |
|
120 | |
private Constraint findConstraint (String id) |
121 | |
{ |
122 | 0 | Constraint cons = new ModelFinder (this.model).findConstraint (id); |
123 | 0 | if (cons != null) |
124 | |
{ |
125 | 0 | return cons; |
126 | |
} |
127 | 0 | System.out.println ("id=["+ id + "]"); |
128 | 0 | if (id == null) |
129 | |
{ |
130 | 0 | System.out.println ("id is null"); |
131 | |
} |
132 | 0 | else if (id.equals ("")) |
133 | |
{ |
134 | 0 | System.out.println ("id is empty string"); |
135 | |
} |
136 | |
else |
137 | |
{ |
138 | 0 | int i = 0; |
139 | 0 | for (byte b : id.getBytes ()) |
140 | |
{ |
141 | 0 | i++; |
142 | 0 | System.out.println (i + ":" + b); |
143 | |
} |
144 | |
} |
145 | 0 | addError ("Dictionary constraint id, " + id + |
146 | |
" is not defined in the bank of constraints"); |
147 | 0 | return null; |
148 | |
} |
149 | |
|
150 | |
private Field findField () |
151 | |
{ |
152 | 0 | Field field = finder.findField (dict); |
153 | 0 | if (field != null) |
154 | |
{ |
155 | 0 | return field; |
156 | |
} |
157 | 0 | addError ("Dictionary with id , " + dict.getId () + |
158 | |
" does not have a corresponding field defined in the message structure."); |
159 | 0 | return null; |
160 | |
} |
161 | |
|
162 | |
private String getConstraintId (Constraint cons) |
163 | |
{ |
164 | 0 | if (cons.getId ().equals ("")) |
165 | |
{ |
166 | 0 | return cons.getKey (); |
167 | |
} |
168 | 0 | return cons.getId (); |
169 | |
} |
170 | |
|
171 | |
private void validateComplexConstraint (Constraint cons) |
172 | |
{ |
173 | 0 | if ( ! cons.getMinLength ().equals ("")) |
174 | |
{ |
175 | 0 | addError (getConstraintId (cons) + |
176 | |
" has a minLength which does not make sense on a complex field"); |
177 | |
} |
178 | 0 | if ( ! cons.getMaxLength ().equals ("")) |
179 | |
{ |
180 | 0 | addError (getConstraintId (cons) + |
181 | |
" has a maxLength which does not make sense on a complex field"); |
182 | |
} |
183 | 0 | if ( ! cons.getMinValue ().equals ("")) |
184 | |
{ |
185 | 0 | addError (getConstraintId (cons) + |
186 | |
" has a minValue which does not make sense on a complex field"); |
187 | |
} |
188 | 0 | if ( ! cons.getMaxValue ().equals ("")) |
189 | |
{ |
190 | 0 | addError (getConstraintId (cons) + |
191 | |
" has a maxValue which does not make sense on a complex field"); |
192 | |
} |
193 | 0 | if ( ! cons.getValidChars ().equals ("")) |
194 | |
{ |
195 | 0 | addError (getConstraintId (cons) + |
196 | |
" has validChars which does not make sense on a complex field"); |
197 | |
} |
198 | 0 | if ( ! cons.getLookup ().equals ("")) |
199 | |
{ |
200 | 0 | addError (getConstraintId (cons) + |
201 | |
" has a lookup which does not make sense on a complex field"); |
202 | |
} |
203 | 0 | } |
204 | |
|
205 | |
private void validateForDuplicates () |
206 | |
{ |
207 | 0 | Set<String> ids = new HashSet (); |
208 | 0 | for (Constraint cons : getAllNamedConstraints ()) |
209 | |
{ |
210 | 0 | if ( ! ids.add (cons.getId ())) |
211 | |
{ |
212 | |
|
213 | |
|
214 | 0 | if ( ! cons.getId ().equals ("optional")) |
215 | |
{ |
216 | 0 | addError ("Constraint with id of [" + cons.getId () + "] is duplicated"); |
217 | |
} |
218 | |
} |
219 | |
} |
220 | 0 | } |
221 | |
|
222 | |
private void addError (String msg) |
223 | |
{ |
224 | 0 | String error = "Error in dictionary entry: " + dict.getId () + ": " + msg; |
225 | 0 | if ( ! errors.contains (error)) |
226 | |
{ |
227 | 0 | errors.add (error); |
228 | |
} |
229 | 0 | } |
230 | |
|
231 | |
} |