View Javadoc

1   /*
2    * Copyright 2009 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may	obtain a copy of the License at
7    *
8    * 	http://www.osedu.org/licenses/ECL-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This validates a constraint.
26   * @author nwright
27   */
28  public class ConstraintValidator implements ModelValidator {
29  
30      private Constraint cons;
31  
32      public ConstraintValidator(Constraint cons) {
33          this.cons = cons;
34      }
35      private Collection errors;
36  
37      @Override
38      public Collection<String> validate() {
39          this.errors = new ArrayList();
40          validateMinMaxLength();
41          validateMinMaxOccurs();
42          validateMinMaxValue();
43          return this.errors;
44      }
45  
46      private void validateMinMaxLength() {
47          if (!cons.getMinLength().equals("")) {
48              int min = parseIntError(cons.getMinLength(), "minLength");
49              if (min < 0) {
50                  this.addError("minLength is less than zero");
51              }
52              if (!cons.getMaxLength().equals("")) {
53                  int max = parseIntError(cons.getMaxLength(), "maxLength");
54                  if (min < 0) {
55                      this.addError("maxLength is less than zero");
56                  }
57  
58                  if (min > max) {
59                      addError("minLength exceeds the maxLength");
60                  }
61              }
62          }
63      }
64  
65      private void validateMinMaxOccurs() {
66          if (!cons.getMinOccurs().equals("")) {
67              int min = parseIntError(cons.getMinOccurs(), "minOccurs");
68              if (min < 0) {
69                  this.addError("minOccurs is less than zero");
70              }
71              if (!cons.getMaxOccurs().equals("")) {
72                  int max = parseIntError(cons.getMaxOccurs(), "maxOccurs");
73                  if (max < 0) {
74                      this.addError("maxOccurs is less than zero");
75                  }
76                  if (min > max) {
77                      addError("minOccurs exceeds the maxOccurs");
78                  }
79              }
80          }
81      }
82  
83      private void validateMinMaxValue() {
84          if (!cons.getMinValue().equals("")) {
85              int min = parseIntError(cons.getMinValue(), "minValue");
86              if (min < 0) {
87                  this.addError("minValue is less than zero");
88              }
89              if (!cons.getMaxValue().equals("")) {
90                  int max = parseIntError(cons.getMaxValue(), "maxValue");
91                  if (max < 0) {
92                      this.addError("maxOccurs is less than zero");
93                  }
94                  if (min > max) {
95                      addError("minOccurs exceeds the maxOccurs");
96                  }
97              }
98          }
99      }
100 
101     private int parseIntError(String value, String field) {
102         if (value.equals("")) {
103             return 0;
104         }
105         try {
106             int val = Integer.parseInt(value);
107             return val;
108         } catch (NumberFormatException ex) {
109             addError(field + " is [" + value + "] not an integer");
110             return 0;
111         }
112     }
113 
114     private void addError(String msg) {
115         String id = cons.getId();
116         if (id.equals("")) {
117             id = cons.getKey();
118         }
119         String error = "Error in constraint " + id + ": " + msg;
120         if (!errors.contains(error)) {
121             errors.add(error);
122         }
123     }
124 }