View Javadoc

1   /**
2    * Copyright 2004-2013 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.opensource.org/licenses/ecl2.php
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  import org.kuali.student.contract.model.DictionaryModel;
20  import org.kuali.student.contract.model.Field;
21  import org.kuali.student.contract.model.XmlType;
22  import org.kuali.student.contract.model.util.ModelFinder;
23  
24  import java.util.Collection;
25  
26  /**
27   * This validates a single field definition
28   * @author nwright
29   */
30  public class FieldValidator implements ModelValidator {
31  
32      private Field field;
33      private DictionaryModel model;
34  
35      public FieldValidator(Field field, DictionaryModel sheet) {
36          this.field = field;
37          this.model = sheet;
38      }
39      private Collection errors;
40  
41      @Override
42      public Collection<String> validate() {
43          ConstraintValidator cv =
44                  new ConstraintValidator(field.getInlineConstraint());
45          errors = cv.validate();
46          if (field.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
47              validateComplexConstraint(field.getInlineConstraint());
48              for (String id : field.getConstraintIds()) {
49                  Constraint cons = findConstraint(id);
50                  if (cons != null) {
51                      validateComplexConstraint(cons);
52                  }
53              }
54          }
55          return errors;
56      }
57  
58      private Constraint findConstraint(String id) {
59          Constraint cons = new ModelFinder(model).findConstraint(id);
60          if (cons != null) {
61              return cons;
62          }
63          addError("Field constraint id, " + id
64                  + " is not defined in the bank of constraints");
65          return null;
66      }
67  
68      private void validateComplexConstraint(Constraint cons) {
69          if (!cons.getMinLength().equals("")) {
70              addError("A minLength is not allowed on a complex field");
71          }
72          if (!cons.getMaxLength().equals("")) {
73              addError("A maxLength is not allowed on a complex field");
74          }
75          if (!cons.getMinValue().equals("")) {
76              addError("A minValue is not allowed on a complex field");
77          }
78          if (!cons.getMaxValue().equals("")) {
79              addError("A maxValue is not allowed on a complex field");
80          }
81          if (!cons.getValidChars().equals("")) {
82              addError("A validChars is not allowed on a complex field");
83          }
84          if (!cons.getLookup().equals("")) {
85              addError("A lookup value is not allowed on a complex field");
86          }
87      }
88  
89      private void addError(String msg) {
90          String error = "Error in field " + field.getId() + ": " + msg;
91          if (!errors.contains(error)) {
92              errors.add(error);
93          }
94      }
95  }