| 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 | |
|
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.Collection; |
| 22 | |
import java.util.List; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
public class ConstraintValidator implements ModelValidator { |
| 29 | |
|
| 30 | |
private Constraint cons; |
| 31 | |
|
| 32 | 0 | public ConstraintValidator(Constraint cons) { |
| 33 | 0 | this.cons = cons; |
| 34 | 0 | } |
| 35 | |
private Collection errors; |
| 36 | |
|
| 37 | |
@Override |
| 38 | |
public Collection<String> validate() { |
| 39 | 0 | this.errors = new ArrayList(); |
| 40 | 0 | validateMinMaxLength(); |
| 41 | 0 | validateMinMaxOccurs(); |
| 42 | 0 | validateMinMaxValue(); |
| 43 | 0 | return this.errors; |
| 44 | |
} |
| 45 | |
|
| 46 | |
private void validateMinMaxLength() { |
| 47 | 0 | if (!cons.getMinLength().equals("")) { |
| 48 | 0 | int min = parseIntError(cons.getMinLength(), "minLength"); |
| 49 | 0 | if (min < 0) { |
| 50 | 0 | this.addError("minLength is less than zero"); |
| 51 | |
} |
| 52 | 0 | if (!cons.getMaxLength().equals("")) { |
| 53 | 0 | int max = parseIntError(cons.getMaxLength(), "maxLength"); |
| 54 | 0 | if (min < 0) { |
| 55 | 0 | this.addError("maxLength is less than zero"); |
| 56 | |
} |
| 57 | |
|
| 58 | 0 | if (min > max) { |
| 59 | 0 | addError("minLength exceeds the maxLength"); |
| 60 | |
} |
| 61 | |
} |
| 62 | |
} |
| 63 | 0 | } |
| 64 | |
|
| 65 | |
private void validateMinMaxOccurs() { |
| 66 | 0 | if (!cons.getMinOccurs().equals("")) { |
| 67 | 0 | int min = parseIntError(cons.getMinOccurs(), "minOccurs"); |
| 68 | 0 | if (min < 0) { |
| 69 | 0 | this.addError("minOccurs is less than zero"); |
| 70 | |
} |
| 71 | 0 | if (!cons.getMaxOccurs().equals("")) { |
| 72 | 0 | int max = parseIntError(cons.getMaxOccurs(), "maxOccurs"); |
| 73 | 0 | if (max < 0) { |
| 74 | 0 | this.addError("maxOccurs is less than zero"); |
| 75 | |
} |
| 76 | 0 | if (min > max) { |
| 77 | 0 | addError("minOccurs exceeds the maxOccurs"); |
| 78 | |
} |
| 79 | |
} |
| 80 | |
} |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
private void validateMinMaxValue() { |
| 84 | 0 | if (!cons.getMinValue().equals("")) { |
| 85 | 0 | int min = parseIntError(cons.getMinValue(), "minValue"); |
| 86 | 0 | if (min < 0) { |
| 87 | 0 | this.addError("minValue is less than zero"); |
| 88 | |
} |
| 89 | 0 | if (!cons.getMaxValue().equals("")) { |
| 90 | 0 | int max = parseIntError(cons.getMaxValue(), "maxValue"); |
| 91 | 0 | if (max < 0) { |
| 92 | 0 | this.addError("maxOccurs is less than zero"); |
| 93 | |
} |
| 94 | 0 | if (min > max) { |
| 95 | 0 | addError("minOccurs exceeds the maxOccurs"); |
| 96 | |
} |
| 97 | |
} |
| 98 | |
} |
| 99 | 0 | } |
| 100 | |
|
| 101 | |
private int parseIntError(String value, String field) { |
| 102 | 0 | if (value.equals("")) { |
| 103 | 0 | return 0; |
| 104 | |
} |
| 105 | |
try { |
| 106 | 0 | int val = Integer.parseInt(value); |
| 107 | 0 | return val; |
| 108 | 0 | } catch (NumberFormatException ex) { |
| 109 | 0 | addError(field + " is [" + value + "] not an integer"); |
| 110 | 0 | return 0; |
| 111 | |
} |
| 112 | |
} |
| 113 | |
|
| 114 | |
private void addError(String msg) { |
| 115 | 0 | String id = cons.getId(); |
| 116 | 0 | if (id.equals("")) { |
| 117 | 0 | id = cons.getKey(); |
| 118 | |
} |
| 119 | 0 | String error = "Error in constraint " + id + ": " + msg; |
| 120 | 0 | if (!errors.contains(error)) { |
| 121 | 0 | errors.add(error); |
| 122 | |
} |
| 123 | 0 | } |
| 124 | |
} |