| 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 | |
private Dictionary dict; |
| 38 | |
private DictionaryModel model; |
| 39 | |
private ModelFinder finder; |
| 40 | |
|
| 41 | 0 | public DictionaryValidator(Dictionary dict, DictionaryModel model) { |
| 42 | 0 | this.dict = dict; |
| 43 | 0 | this.model = model; |
| 44 | 0 | this.finder = new ModelFinder(model); |
| 45 | 0 | } |
| 46 | |
private Collection errors; |
| 47 | |
|
| 48 | |
@Override |
| 49 | |
public Collection<String> validate() { |
| 50 | 0 | ConstraintValidator cv = new ConstraintValidator(dict.getInlineConstraint()); |
| 51 | 0 | errors = cv.validate(); |
| 52 | 0 | validateForDuplicates(); |
| 53 | 0 | if (dict.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) { |
| 54 | 0 | for (Constraint cons : getAllConstraints()) { |
| 55 | 0 | validateComplexConstraint(cons); |
| 56 | |
} |
| 57 | |
} |
| 58 | 0 | return errors; |
| 59 | |
} |
| 60 | |
|
| 61 | |
private List<Constraint> getAllConstraints() { |
| 62 | 0 | List<Constraint> list = getFieldNamedConstraints(); |
| 63 | 0 | Field field = findField(); |
| 64 | 0 | if (field != null) { |
| 65 | 0 | list.add(field.getInlineConstraint()); |
| 66 | |
} |
| 67 | 0 | list.addAll(getDictionaryAdditionalConstraints()); |
| 68 | 0 | list.add(dict.getInlineConstraint()); |
| 69 | 0 | return list; |
| 70 | |
} |
| 71 | |
|
| 72 | |
private List<Constraint> getAllNamedConstraints() { |
| 73 | 0 | List<Constraint> list = getFieldNamedConstraints(); |
| 74 | 0 | list.addAll(getDictionaryAdditionalConstraints()); |
| 75 | 0 | return list; |
| 76 | |
} |
| 77 | |
|
| 78 | |
private List<Constraint> getFieldNamedConstraints() { |
| 79 | 0 | List<Constraint> list = new ArrayList(); |
| 80 | 0 | Field field = findField(); |
| 81 | 0 | if (field != null) { |
| 82 | 0 | for (String id : field.getConstraintIds()) { |
| 83 | 0 | Constraint cons = findConstraint(id); |
| 84 | 0 | if (cons != null) { |
| 85 | 0 | list.add(cons); |
| 86 | |
} |
| 87 | 0 | } |
| 88 | |
} |
| 89 | 0 | return list; |
| 90 | |
} |
| 91 | |
|
| 92 | |
private List<Constraint> getDictionaryAdditionalConstraints() { |
| 93 | 0 | List<Constraint> list = new ArrayList(); |
| 94 | 0 | for (String id : dict.getAdditionalConstraintIds()) { |
| 95 | 0 | Constraint cons = findConstraint(id); |
| 96 | 0 | if (cons != null) { |
| 97 | 0 | list.add(cons); |
| 98 | |
} |
| 99 | 0 | } |
| 100 | 0 | list.add(dict.getInlineConstraint()); |
| 101 | 0 | return list; |
| 102 | |
} |
| 103 | |
|
| 104 | |
private Constraint findConstraint(String id) { |
| 105 | 0 | Constraint cons = new ModelFinder(this.model).findConstraint(id); |
| 106 | 0 | if (cons != null) { |
| 107 | 0 | return cons; |
| 108 | |
} |
| 109 | 0 | System.out.println("id=[" + id + "]"); |
| 110 | 0 | if (id == null) { |
| 111 | 0 | System.out.println("id is null"); |
| 112 | 0 | } else if (id.equals("")) { |
| 113 | 0 | System.out.println("id is empty string"); |
| 114 | |
} else { |
| 115 | 0 | int i = 0; |
| 116 | 0 | for (byte b : id.getBytes()) { |
| 117 | 0 | i++; |
| 118 | 0 | System.out.println(i + ":" + b); |
| 119 | |
} |
| 120 | |
} |
| 121 | 0 | addError("Dictionary constraint id, " + id |
| 122 | |
+ " is not defined in the bank of constraints"); |
| 123 | 0 | return null; |
| 124 | |
} |
| 125 | |
|
| 126 | |
private Field findField() { |
| 127 | 0 | Field field = finder.findField(dict); |
| 128 | 0 | if (field != null) { |
| 129 | 0 | return field; |
| 130 | |
} |
| 131 | 0 | addError("Dictionary with id , " + dict.getId() |
| 132 | |
+ " does not have a corresponding field defined in the message structure."); |
| 133 | 0 | return null; |
| 134 | |
} |
| 135 | |
|
| 136 | |
private String getConstraintId(Constraint cons) { |
| 137 | 0 | if (cons.getId().equals("")) { |
| 138 | 0 | return cons.getKey(); |
| 139 | |
} |
| 140 | 0 | return cons.getId(); |
| 141 | |
} |
| 142 | |
|
| 143 | |
private void validateComplexConstraint(Constraint cons) { |
| 144 | 0 | if (!cons.getMinLength().equals("")) { |
| 145 | 0 | addError(getConstraintId(cons) |
| 146 | |
+ " has a minLength which does not make sense on a complex field"); |
| 147 | |
} |
| 148 | 0 | if (!cons.getMaxLength().equals("")) { |
| 149 | 0 | addError(getConstraintId(cons) |
| 150 | |
+ " has a maxLength which does not make sense on a complex field"); |
| 151 | |
} |
| 152 | 0 | if (!cons.getMinValue().equals("")) { |
| 153 | 0 | addError(getConstraintId(cons) |
| 154 | |
+ " has a minValue which does not make sense on a complex field"); |
| 155 | |
} |
| 156 | 0 | if (!cons.getMaxValue().equals("")) { |
| 157 | 0 | addError(getConstraintId(cons) |
| 158 | |
+ " has a maxValue which does not make sense on a complex field"); |
| 159 | |
} |
| 160 | 0 | if (!cons.getValidChars().equals("")) { |
| 161 | 0 | addError(getConstraintId(cons) |
| 162 | |
+ " has validChars which does not make sense on a complex field"); |
| 163 | |
} |
| 164 | 0 | if (!cons.getLookup().equals("")) { |
| 165 | 0 | addError(getConstraintId(cons) |
| 166 | |
+ " has a lookup which does not make sense on a complex field"); |
| 167 | |
} |
| 168 | 0 | } |
| 169 | |
|
| 170 | |
private void validateForDuplicates() { |
| 171 | 0 | Set<String> ids = new HashSet(); |
| 172 | 0 | for (Constraint cons : getAllNamedConstraints()) { |
| 173 | 0 | if (!ids.add(cons.getId())) { |
| 174 | |
|
| 175 | |
|
| 176 | 0 | if (!cons.getId().equals("optional")) { |
| 177 | 0 | addError("Constraint with id of [" + cons.getId() + "] is duplicated"); |
| 178 | |
} |
| 179 | |
} |
| 180 | |
} |
| 181 | 0 | } |
| 182 | |
|
| 183 | |
private void addError(String msg) { |
| 184 | 0 | String error = "Error in dictionary entry: " + dict.getId() + ": " + msg; |
| 185 | 0 | if (!errors.contains(error)) { |
| 186 | 0 | errors.add(error); |
| 187 | |
} |
| 188 | 0 | } |
| 189 | |
} |