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  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   * Validates the entire spreadsheet model
33   * @author nwright
34   */
35  public class DictionaryModelValidator implements ModelValidator {
36  
37      private DictionaryModel model;
38      private ModelFinder finder;
39  
40      public DictionaryModelValidator(DictionaryModel model) {
41          this.model = model;
42          this.finder = new ModelFinder(model);
43      }
44      List<String> errors;
45  
46      @Override
47      public Collection<String> validate() {
48          errors = new ArrayList();
49          validateConstraints();
50          validateFields();
51          validateDefaultDictionary();
52          validateStateOverrideDictionary();
53          checkForDuplicateDictionaryEntries();
54          return errors;
55      }
56  
57      private void validateConstraints() {
58          if (model.getConstraints().size() == 0) {
59              addError("No constraints found");
60          }
61          for (Constraint cons : model.getConstraints()) {
62              ConstraintValidator cv = new ConstraintValidator(cons);
63              errors.addAll(cv.validate());
64          }
65      }
66  
67      private void validateFields() {
68          if (model.getFields().size() == 0) {
69              addError("No fields found");
70          }
71          for (Field field : model.getFields()) {
72              FieldValidator fv = new FieldValidator(field, model);
73              errors.addAll(fv.validate());
74          }
75      }
76  
77      private void validateDefaultDictionary() {
78          if (finder.findDefaultDictionary().size() == 0) {
79              addError("No dictionary entries for the (default) state found");
80          }
81          for (Dictionary dict : finder.findDefaultDictionary()) {
82              DictionaryValidator dv = new DictionaryValidator(dict, model);
83              errors.addAll(dv.validate());
84          }
85      }
86  
87      private void validateStateOverrideDictionary() {
88  
89          if (finder.findStateOverrideDictionary().size() == 0) {
90              addError("No dictionary entries that override for the (default) state found");
91          }
92          for (Dictionary dict : finder.findStateOverrideDictionary()) {
93              DictionaryValidator dv = new DictionaryValidator(dict, model);
94              errors.addAll(dv.validate());
95          }
96      }
97  
98      private void checkForDuplicateDictionaryEntries() {
99          Set dups = new HashSet();
100         for (Dictionary dict : finder.findDefaultDictionary()) {
101             if (!dups.add(dict.getId())) {
102                 addError("Duplicate ID's found in dictionary: " + dict.getId());
103             }
104         }
105         for (Dictionary dict : finder.findStateOverrideDictionary()) {
106             if (!dups.add(dict.getId())) {
107                 addError("Duplicate ID's found in dictionary: " + dict.getId());
108             }
109         }
110     }
111 
112     private void addError(String msg) {
113         String error = "Error in overall spreadsheet: " + msg;
114         if (!errors.contains(error)) {
115             errors.add(error);
116         }
117     }
118 }