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