| 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 DictionaryModelValidator implements ModelValidator |
| 36 | |
{ |
| 37 | |
|
| 38 | |
private DictionaryModel model; |
| 39 | |
private ModelFinder finder; |
| 40 | |
|
| 41 | |
public DictionaryModelValidator (DictionaryModel model) |
| 42 | 0 | { |
| 43 | 0 | this.model = model; |
| 44 | 0 | this.finder = new ModelFinder (model); |
| 45 | 0 | } |
| 46 | |
|
| 47 | |
List<String> errors; |
| 48 | |
|
| 49 | |
@Override |
| 50 | |
public Collection<String> validate () |
| 51 | |
{ |
| 52 | 0 | errors = new ArrayList (); |
| 53 | 0 | validateConstraints (); |
| 54 | 0 | validateFields (); |
| 55 | 0 | validateDefaultDictionary (); |
| 56 | 0 | validateStateOverrideDictionary (); |
| 57 | 0 | checkForDuplicateDictionaryEntries (); |
| 58 | 0 | return errors; |
| 59 | |
} |
| 60 | |
|
| 61 | |
private void validateConstraints () |
| 62 | |
{ |
| 63 | 0 | if (model.getConstraints ().size () == 0) |
| 64 | |
{ |
| 65 | 0 | addError ("No constraints found"); |
| 66 | |
} |
| 67 | 0 | for (Constraint cons : model.getConstraints ()) |
| 68 | |
{ |
| 69 | 0 | ConstraintValidator cv = new ConstraintValidator (cons); |
| 70 | 0 | errors.addAll (cv.validate ()); |
| 71 | 0 | } |
| 72 | 0 | } |
| 73 | |
|
| 74 | |
private void validateFields () |
| 75 | |
{ |
| 76 | 0 | if (model.getFields ().size () == 0) |
| 77 | |
{ |
| 78 | 0 | addError ("No fields found"); |
| 79 | |
} |
| 80 | 0 | for (Field field : model.getFields ()) |
| 81 | |
{ |
| 82 | 0 | FieldValidator fv = new FieldValidator (field, model); |
| 83 | 0 | errors.addAll (fv.validate ()); |
| 84 | 0 | } |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
|
| 88 | |
private void validateDefaultDictionary () |
| 89 | |
{ |
| 90 | 0 | if (finder.findDefaultDictionary ().size () == 0) |
| 91 | |
{ |
| 92 | 0 | addError ("No dictionary entries for the (default) state found"); |
| 93 | |
} |
| 94 | 0 | for (Dictionary dict : finder.findDefaultDictionary ()) |
| 95 | |
{ |
| 96 | 0 | DictionaryValidator dv = new DictionaryValidator (dict, model); |
| 97 | 0 | errors.addAll (dv.validate ()); |
| 98 | 0 | } |
| 99 | 0 | } |
| 100 | |
|
| 101 | |
private void validateStateOverrideDictionary () |
| 102 | |
{ |
| 103 | |
|
| 104 | 0 | if (finder.findStateOverrideDictionary ().size () == 0) |
| 105 | |
{ |
| 106 | 0 | addError ("No dictionary entries that override for the (default) state found"); |
| 107 | |
} |
| 108 | 0 | for (Dictionary dict : finder.findStateOverrideDictionary ()) |
| 109 | |
{ |
| 110 | 0 | DictionaryValidator dv = new DictionaryValidator (dict, model); |
| 111 | 0 | errors.addAll (dv.validate ()); |
| 112 | 0 | } |
| 113 | 0 | } |
| 114 | |
|
| 115 | |
private void checkForDuplicateDictionaryEntries () |
| 116 | |
{ |
| 117 | 0 | Set dups = new HashSet (); |
| 118 | 0 | for (Dictionary dict : finder.findDefaultDictionary ()) |
| 119 | |
{ |
| 120 | 0 | if ( ! dups.add (dict.getId ())) |
| 121 | |
{ |
| 122 | 0 | addError ("Duplicate ID's found in dictionary: " + dict.getId ()); |
| 123 | |
} |
| 124 | |
} |
| 125 | 0 | for (Dictionary dict : finder.findStateOverrideDictionary ()) |
| 126 | |
{ |
| 127 | 0 | if ( ! dups.add (dict.getId ())) |
| 128 | |
{ |
| 129 | 0 | addError ("Duplicate ID's found in dictionary: " + dict.getId ()); |
| 130 | |
} |
| 131 | |
} |
| 132 | 0 | } |
| 133 | |
|
| 134 | |
private void addError (String msg) |
| 135 | |
{ |
| 136 | 0 | String error = "Error in overall spreadsheet: " + msg; |
| 137 | 0 | if ( ! errors.contains (error)) |
| 138 | |
{ |
| 139 | 0 | errors.add (error); |
| 140 | |
} |
| 141 | 0 | } |
| 142 | |
|
| 143 | |
} |