Clover Coverage Report - KS Common 1.2-M6-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Sep 12 2011 05:50:56 EDT
../../../../../../img/srcFileCovDistChart2.png 29% of files have more coverage
185   506   114   5.61
120   340   0.62   16.5
33     3.45  
2    
 
  MetadataInterrogator       Line # 27 183 0% 112 282 15.6% 0.15568863
  MetadataInterrogator.ConstraintIds       Line # 29 2 0% 2 4 0% 0.0
 
  (2)
 
1    /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10    * software distributed under the License is distributed on an "AS IS"
11    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12    * or implied. See the License for the specific language governing
13    * permissions and limitations under the License.
14    */
15   
16    package org.kuali.student.common.assembly.data;
17   
18    import java.util.Date;
19    import java.util.List;
20   
21    import org.kuali.student.common.validator.DateParser;
22   
23    /**
24    *
25    * @author nwright
26    */
 
27    public class MetadataInterrogator {
28   
 
29    public enum ConstraintIds {
30    MULTI_LINE_TEXT("multi.line.text"), RICH_TEXT("rich.text");
31    private final String id;
32   
 
33  0 toggle private ConstraintIds(final String id) {
34  0 this.id = id;
35    }
36   
 
37  0 toggle public String getId() {
38  0 return this.id;
39    }
40    }
41   
42    private Metadata meta;
43   
 
44  25 toggle public MetadataInterrogator(Metadata meta) {
45  25 this.meta = meta;
46    }
47   
48    /**
49    * Returns true if the metadata contains the specified constraint
50    */
 
51  0 toggle public boolean hasConstraint(String id) {
52  0 return hasConstraint(meta, id);
53    }
54   
55   
56    /**
57    * checks if is required
58    *
59    * @return true if any minOccurs > 1
60    */
 
61  0 toggle public boolean isRequired() {
62  0 return isRequired(meta);
63    }
64   
65    /**
66    * get the largest min occurs value
67    *
68    * @return null if none specified
69    */
 
70  0 toggle public Integer getLargestMinOccurs() {
71  0 return getLargestMinOccurs(meta);
72    }
73   
74    /**
75    * checks if this field is a repeating field
76    *
77    * @return true if the smallest maxOccurs is > 1
78    */
 
79  0 toggle public boolean isRepeating() {
80  0 return isRepeating(meta);
81    }
82   
83    /**
84    * checks if this field is a repeating field
85    *
86    * @return true if the smallest maxOccurs is > 1
87    */
 
88  0 toggle public Integer getSmallestMaxOccurs() {
89  0 return getSmallestMaxOccurs(meta);
90    }
91   
92    /**
93    * get the largest min occurs value
94    *
95    * @return null if none specified
96    */
 
97  91 toggle public Integer getLargestMinLength() {
98  91 return getLargestMinLength(meta);
99    }
100   
 
101  69 toggle public Integer getSmallestMaxLength() {
102  69 return getSmallestMaxLength(meta);
103    }
104   
 
105  0 toggle public boolean isMultilined() {
106  0 return isMultilined(meta);
107    }
108   
109   
110    /**
111    * Returns true if the metadata contains the specified constraint
112    */
 
113  0 toggle public static boolean hasConstraint(Metadata meta, ConstraintIds id) {
114  0 return hasConstraint(meta, id.getId());
115    }
116    /**
117    * Returns true if the metadata contains the specified constraint
118    */
 
119  0 toggle public static boolean hasConstraint(Metadata meta, String id) {
120  0 boolean result = false;
121  0 if (meta != null && meta.getConstraints() != null) {
122  0 result = containsConstraint(meta.getConstraints(), id);
123    }
124  0 return result;
125    }
126   
 
127  0 toggle private static boolean containsConstraint(List<ConstraintMetadata> constraints,
128    String id) {
129  0 boolean result = false;
130  0 if (constraints != null) {
131  0 for (ConstraintMetadata con : constraints) {
132  0 if ((con.getId() != null && con.getId().equals(id))
133    || containsConstraint(con.getChildConstraints(), id)) {
134  0 result = true;
135  0 break;
136    }
137    }
138    }
139  0 return result;
140    }
141   
142    /**
143    * checks if is required
144    *
145    * @return true if any minOccurs > 1
146    */
 
147  8 toggle public static boolean isRequired(Metadata meta) {
148  8 if (meta == null) {
149  0 return false;
150    }
151  8 Integer largestMinOccurs = getLargestMinOccurs(meta);
152    // no min occurs specified so not required
153  8 if (largestMinOccurs == null) {
154  8 return false;
155    }
156  0 if (largestMinOccurs >= 1) {
157  0 return true;
158    }
159  0 return false;
160    }
161   
162    /**
163    * Use to determine if the field is required for the next state.
164    *
165    * @param meta
166    * @return
167    */
 
168  0 toggle public static boolean isRequiredForNextState(Metadata meta){
169  0 boolean required = false;
170   
171  0 if(meta == null){
172  0 return false;
173    }
174    //This flag is only set when using the new dictionary, in which case there should
175    //never be more than one constraint.
176  0 if (meta.getConstraints() != null && meta.getConstraints().size() == 1){
177  0 ConstraintMetadata constraint = meta.getConstraints().get(0);
178  0 required = constraint.isRequiredForNextState();
179    }
180   
181  0 return required;
182    }
183   
184    /**
185    * Use to determine if the field is required for the next state.
186    *
187    * @param meta
188    * @return
189    */
 
190  0 toggle public static String getNextState(Metadata meta){
191  0 String state = null;
192   
193  0 if(meta == null){
194  0 return state;
195    }
196    //This flag is only set when using the new dictionary, in which case there should
197    //never be more than one constraint.
198  0 if (meta.getConstraints() != null && meta.getConstraints().size() == 1){
199  0 ConstraintMetadata constraint = meta.getConstraints().get(0);
200  0 state = constraint.getNextState();
201    }
202   
203  0 return state;
204    }
205   
206    /**
207    * get the largest min occurs value
208    *
209    * @return null if none specified
210    */
 
211  11 toggle public static Integer getLargestMinOccurs(Metadata meta) {
212  11 if (meta == null) {
213  0 return null;
214    }
215  11 Integer largestMinOccurs = null;
216    // TODO: worry aboutg special validators
217    // TODO: worry about how this applies to non-strings?
218  11 for (ConstraintMetadata cons : meta.getConstraints()) {
219  0 if (cons.getMinOccurs() != null) {
220  0 if (largestMinOccurs == null) {
221  0 largestMinOccurs = cons.getMinOccurs();
222  0 } else if (cons.getMinOccurs() > largestMinOccurs) {
223  0 largestMinOccurs = cons.getMinOccurs();
224    }
225    }
226    }
227  11 return largestMinOccurs;
228    }
229   
230    /**
231    * checks if this field is a repeating field
232    *
233    * @return true if the smallest maxOccurs is > 1
234    */
 
235  0 toggle public static boolean isRepeating(Metadata meta) {
236  0 if (meta == null) {
237  0 return false;
238    }
239  0 Integer smallestMaxOccurs = getSmallestMaxOccurs(meta);
240  0 if (hasRepeatingConstraint(meta) ||
241    (smallestMaxOccurs != null && smallestMaxOccurs > 1)) {
242  0 return true;
243    }
244  0 return false;
245    }
246   
 
247  0 toggle private static boolean hasRepeatingConstraint(Metadata meta) {
248  0 boolean isRepeating = false;
249   
250  0 for (ConstraintMetadata cons : meta.getConstraints()) {
251  0 if ("repeating".equals(cons.getId())){
252  0 isRepeating = true;
253    }
254    }
255  0 return isRepeating;
256    }
257   
258    /**
259    * Returns the smallest max occurs
260    *
261    * @return
262    *
263    * Returns the smallest of all maxOccurs, if maxOccurs defined in constraints.
264    * Returns -1, indicating unbounded repeating field, if repeating constraint defined & maxOccurs not defined.
265    * Returns null, indicating that this is a non-repeating field.
266    */
 
267  3 toggle public static Integer getSmallestMaxOccurs(Metadata meta) {
268  3 if (meta == null) {
269  0 return null;
270    }
271  3 Integer smallestMaxOccurs = null;
272  3 boolean isRepeating = false;
273   
274  3 for (ConstraintMetadata cons : meta.getConstraints()) {
275  0 if ("repeating".equals(cons.getId())){
276  0 isRepeating = true;
277    }
278  0 if (cons.getMaxOccurs() != null) {
279  0 if (smallestMaxOccurs == null) {
280  0 smallestMaxOccurs = cons.getMaxOccurs();
281  0 } else if (cons.getMaxOccurs() < smallestMaxOccurs) {
282  0 smallestMaxOccurs = cons.getMaxOccurs();
283    }
284    }
285    }
286   
287  3 if (isRepeating && smallestMaxOccurs == null){
288  0 smallestMaxOccurs = -1;
289    }
290   
291  3 return smallestMaxOccurs;
292    }
293   
294    /**
295    * get the largest min occurs value
296    *
297    * @return null if none specified
298    */
 
299  96 toggle public static Integer getLargestMinLength(Metadata meta) {
300  96 if (meta == null) {
301  0 return null;
302    }
303  96 Integer largestMinLength = null;
304    // TODO: worry aboutg special validators
305    // TODO: worry about how this applies to non-strings?
306  96 for (ConstraintMetadata cons : meta.getConstraints()) {
307  96 if (cons.getMinLength() != null) {
308  93 if (largestMinLength == null) {
309  93 largestMinLength = cons.getMinLength();
310  0 } else if (cons.getMinLength() > largestMinLength) {
311  0 largestMinLength = cons.getMinLength();
312    }
313    }
314    }
315  96 return largestMinLength;
316    }
317   
 
318  74 toggle public static Integer getSmallestMaxLength(Metadata meta) {
319  74 if (meta == null) {
320  0 return null;
321    }
322  74 Integer smallestMaxLength = null;
323    // TODO: worry aboutg special validators
324    // TODO: worry about how this applies to non-strings?
325  74 for (ConstraintMetadata cons : meta.getConstraints()) {
326  74 if (cons.getMaxLength() != null) {
327  71 if (smallestMaxLength == null) {
328  71 smallestMaxLength = cons.getMaxLength();
329  0 } else if (cons.getMaxLength() < smallestMaxLength) {
330  0 smallestMaxLength = cons.getMaxLength();
331    }
332    }
333    }
334  74 return smallestMaxLength;
335    }
336   
 
337  0 toggle public static boolean isMultilined(Metadata meta) {
338  0 if (meta == null) {
339  0 return false;
340    }
341    // TODO: worry about hard coding ids
342  0 boolean result = (hasConstraint(meta, "rich.text") || hasConstraint(meta, "multi.line.text"))
343    && !(hasConstraint(meta, "single.line.text") || hasConstraint(meta, "code") || hasConstraint(meta, "no.linefeeds"));
344   
345    // TODO: Worry about hard coding the cut-off point
346  0 if (!result) {
347  0 Integer maxLength = getSmallestMaxLength(meta);
348  0 if (maxLength != null) {
349  0 if (maxLength > 150) {
350  0 result = true;
351    }
352    }
353    }
354  0 return result;
355    }
356   
357   
 
358  0 toggle public static Long getLargestMinValue(Metadata meta) {
359  0 if (meta == null) {
360  0 return null;
361    }
362  0 Long result = null;
363  0 for (ConstraintMetadata cons : meta.getConstraints()) {
364  0 Long min = tryParseLong(cons.getMinValue());
365  0 if (min != null) {
366  0 if (result == null || min > result) {
367  0 result = min;
368    }
369    }
370    }
371  0 return result;
372    }
373   
 
374  0 toggle public static Long getSmallestMaxValue(Metadata meta) {
375  0 if (meta == null) {
376  0 return null;
377    }
378  0 Long result = null;
379  0 for (ConstraintMetadata cons : meta.getConstraints()) {
380  0 Long max = tryParseLong(cons.getMaxValue());
381  0 if (max != null) {
382  0 if (result == null || max < result) {
383  0 result = max;
384    }
385    }
386    }
387  0 return result;
388    }
389   
390   
 
391  0 toggle public static Double getLargestMinValueDouble(Metadata meta) {
392  0 if (meta == null) {
393  0 return null;
394    }
395  0 Double result = null;
396  0 for (ConstraintMetadata cons : meta.getConstraints()) {
397  0 Double min = tryParseDouble(cons.getMinValue());
398  0 if (min != null) {
399  0 if (result == null || min > result) {
400  0 result = min;
401    }
402    }
403    }
404  0 return result;
405    }
406   
 
407  0 toggle public static Double getSmallestMaxValueDouble(Metadata meta) {
408  0 if (meta == null) {
409  0 return null;
410    }
411  0 Double result = null;
412  0 for (ConstraintMetadata cons : meta.getConstraints()) {
413  0 Double max = tryParseDouble(cons.getMaxValue());
414  0 if (max != null) {
415  0 if (result == null || max < result) {
416  0 result = max;
417    }
418    }
419    }
420  0 return result;
421    }
422   
 
423  0 toggle public static Date getLargestMinValueDate(Metadata meta, DateParser parser, Object dataValue) {
424  0 if (meta == null) {
425  0 return null;
426    }
427  0 Date result = null;
428  0 for (ConstraintMetadata cons : meta.getConstraints()) {
429  0 Date min = tryParseDate(cons.getMinValue(), parser);
430  0 if (min != null) {
431  0 if (result == null || min.getTime() > result.getTime()) {
432  0 result = min;
433    }
434    }
435    }
436  0 if (dataValue != null && dataValue instanceof Date){
437  0 if (result == null || (((Date)dataValue).getTime() > result.getTime())){
438  0 result = (Date)dataValue;
439    }
440    }
441  0 return result;
442    }
443   
 
444  0 toggle public static Date getSmallestMaxValueDate(Metadata meta, DateParser parser, Object dataValue) {
445  0 if (meta == null) {
446  0 return null;
447    }
448  0 Date result = null;
449  0 for (ConstraintMetadata cons : meta.getConstraints()) {
450  0 Date max = tryParseDate(cons.getMaxValue(), parser);
451  0 if (max != null) {
452  0 if (result == null || max.getTime() < result.getTime()) {
453  0 result = max;
454    }
455    }
456    }
457  0 if (dataValue != null && dataValue instanceof Date){
458  0 if (result==null || (((Date)dataValue).getTime() < result.getTime())){
459  0 result = (Date)dataValue;
460    }
461    }
462  0 return result;
463    }
464   
 
465  0 toggle private static Long tryParseLong(String s) {
466  0 Long result = null;
467   
468  0 if (s != null) {
469  0 try {
470  0 result = Long.valueOf(s);
471    } catch (Exception e) {
472    // do nothing
473    }
474    }
475   
476  0 return result;
477    }
478   
 
479  0 toggle private static Double tryParseDouble(String s) {
480  0 Double result = null;
481   
482  0 if (s != null) {
483  0 try {
484  0 result = Double.valueOf(s);
485    } catch (Exception e) {
486    // do nothing
487    }
488    }
489   
490  0 return result;
491    }
492   
 
493  0 toggle private static Date tryParseDate(String s, DateParser parser) {
494  0 Date result = null;
495   
496  0 if (s != null) {
497  0 try {
498  0 result = parser.parseDate(s);
499    } catch (Exception e) {
500    // do nothing
501    }
502    }
503   
504  0 return result;
505    }
506    }