| 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 |  |   | 
  | 31 |  |   private Constraint cons; | 
  | 32 |  |   | 
  | 33 |  |   public ConstraintValidator (Constraint cons) | 
  | 34 | 0 |   { | 
  | 35 | 0 |    this.cons = cons; | 
  | 36 | 0 |   } | 
  | 37 |  |   | 
  | 38 |  |   private Collection errors; | 
  | 39 |  |   | 
  | 40 |  |   @Override | 
  | 41 |  |   public Collection<String> validate () | 
  | 42 |  |   { | 
  | 43 | 0 |    this.errors = new ArrayList (); | 
  | 44 | 0 |    validateMinMaxLength (); | 
  | 45 | 0 |    validateMinMaxOccurs (); | 
  | 46 | 0 |    validateMinMaxValue (); | 
  | 47 | 0 |    return this.errors; | 
  | 48 |  |   } | 
  | 49 |  |   | 
  | 50 |  |   private void validateMinMaxLength () | 
  | 51 |  |   { | 
  | 52 | 0 |    if ( ! cons.getMinLength ().equals ("")) | 
  | 53 |  |    { | 
  | 54 | 0 |     int min = parseIntError (cons.getMinLength (), "minLength"); | 
  | 55 | 0 |     if (min < 0) | 
  | 56 |  |     { | 
  | 57 | 0 |      this.addError ("minLength is less than zero"); | 
  | 58 |  |     } | 
  | 59 | 0 |     if ( ! cons.getMaxLength ().equals ("")) | 
  | 60 |  |     { | 
  | 61 | 0 |      int max = parseIntError (cons.getMaxLength (), "maxLength"); | 
  | 62 | 0 |      if (min < 0) | 
  | 63 |  |      { | 
  | 64 | 0 |       this.addError ("maxLength is less than zero"); | 
  | 65 |  |      } | 
  | 66 |  |   | 
  | 67 | 0 |      if (min > max) | 
  | 68 |  |      { | 
  | 69 | 0 |       addError ("minLength exceeds the maxLength"); | 
  | 70 |  |      } | 
  | 71 |  |     } | 
  | 72 |  |    } | 
  | 73 | 0 |   } | 
  | 74 |  |   | 
  | 75 |  |   private void validateMinMaxOccurs () | 
  | 76 |  |   { | 
  | 77 | 0 |    if ( ! cons.getMinOccurs ().equals ("")) | 
  | 78 |  |    { | 
  | 79 | 0 |     int min = parseIntError (cons.getMinOccurs (), "minOccurs"); | 
  | 80 | 0 |     if (min < 0) | 
  | 81 |  |     { | 
  | 82 | 0 |      this.addError ("minOccurs is less than zero"); | 
  | 83 |  |     } | 
  | 84 | 0 |     if ( ! cons.getMaxOccurs ().equals ("")) | 
  | 85 |  |     { | 
  | 86 | 0 |      int max = parseIntError (cons.getMaxOccurs (), "maxOccurs"); | 
  | 87 | 0 |      if (max < 0) | 
  | 88 |  |      { | 
  | 89 | 0 |       this.addError ("maxOccurs is less than zero"); | 
  | 90 |  |      } | 
  | 91 | 0 |      if (min > max) | 
  | 92 |  |      { | 
  | 93 | 0 |       addError ("minOccurs exceeds the maxOccurs"); | 
  | 94 |  |      } | 
  | 95 |  |     } | 
  | 96 |  |    } | 
  | 97 | 0 |   } | 
  | 98 |  |   | 
  | 99 |  |   private void validateMinMaxValue () | 
  | 100 |  |   { | 
  | 101 | 0 |    if ( ! cons.getMinValue ().equals ("")) | 
  | 102 |  |    { | 
  | 103 | 0 |     int min = parseIntError (cons.getMinValue (), "minValue"); | 
  | 104 | 0 |     if (min < 0) | 
  | 105 |  |     { | 
  | 106 | 0 |      this.addError ("minValue is less than zero"); | 
  | 107 |  |     } | 
  | 108 | 0 |     if ( ! cons.getMaxValue ().equals ("")) | 
  | 109 |  |     { | 
  | 110 | 0 |      int max = parseIntError (cons.getMaxValue (), "maxValue"); | 
  | 111 | 0 |      if (max < 0) | 
  | 112 |  |      { | 
  | 113 | 0 |       this.addError ("maxOccurs is less than zero"); | 
  | 114 |  |      } | 
  | 115 | 0 |      if (min > max) | 
  | 116 |  |      { | 
  | 117 | 0 |       addError ("minOccurs exceeds the maxOccurs"); | 
  | 118 |  |      } | 
  | 119 |  |     } | 
  | 120 |  |    } | 
  | 121 | 0 |   } | 
  | 122 |  |   | 
  | 123 |  |   private int parseIntError (String value, String field) | 
  | 124 |  |   { | 
  | 125 | 0 |    if (value.equals ("")) | 
  | 126 |  |    { | 
  | 127 | 0 |     return 0; | 
  | 128 |  |    } | 
  | 129 |  |    try | 
  | 130 |  |    { | 
  | 131 | 0 |     int val = Integer.parseInt (value); | 
  | 132 | 0 |     return val; | 
  | 133 |  |    } | 
  | 134 | 0 |    catch (NumberFormatException ex) | 
  | 135 |  |    { | 
  | 136 | 0 |     addError (field + " is [" + value + "] not an integer"); | 
  | 137 | 0 |     return 0; | 
  | 138 |  |    } | 
  | 139 |  |   } | 
  | 140 |  |   | 
  | 141 |  |   private void addError (String msg) | 
  | 142 |  |   { | 
  | 143 | 0 |    String id = cons.getId (); | 
  | 144 | 0 |    if (id.equals ("")) | 
  | 145 |  |    { | 
  | 146 | 0 |     id = cons.getKey (); | 
  | 147 |  |    } | 
  | 148 | 0 |    String error = "Error in constraint " + id + ": " + msg; | 
  | 149 | 0 |    if ( ! errors.contains (error)) | 
  | 150 |  |    { | 
  | 151 | 0 |     errors.add (error); | 
  | 152 |  |    } | 
  | 153 | 0 |   } | 
  | 154 |  |   | 
  | 155 |  |  } |