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   * This validates a single dictinoary entry
33   * @author nwright
34   */
35  public class DictionaryValidator implements ModelValidator {
36  
37      private Dictionary dict;
38      private DictionaryModel model;
39      private ModelFinder finder;
40  
41      public DictionaryValidator(Dictionary dict, DictionaryModel model) {
42          this.dict = dict;
43          this.model = model;
44          this.finder = new ModelFinder(model);
45      }
46      private Collection errors;
47  
48      @Override
49      public Collection<String> validate() {
50          ConstraintValidator cv = new ConstraintValidator(dict.getInlineConstraint());
51          errors = cv.validate();
52          validateForDuplicates();
53          if (dict.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
54              for (Constraint cons : getAllConstraints()) {
55                  validateComplexConstraint(cons);
56              }
57          }
58          return errors;
59      }
60  
61      private List<Constraint> getAllConstraints() {
62          List<Constraint> list = getFieldNamedConstraints();
63          Field field = findField();
64          if (field != null) {
65              list.add(field.getInlineConstraint());
66          }
67          list.addAll(getDictionaryAdditionalConstraints());
68          list.add(dict.getInlineConstraint());
69          return list;
70      }
71  
72      private List<Constraint> getAllNamedConstraints() {
73          List<Constraint> list = getFieldNamedConstraints();
74          list.addAll(getDictionaryAdditionalConstraints());
75          return list;
76      }
77  
78      private List<Constraint> getFieldNamedConstraints() {
79          List<Constraint> list = new ArrayList();
80          Field field = findField();
81          if (field != null) {
82              for (String id : field.getConstraintIds()) {
83                  Constraint cons = findConstraint(id);
84                  if (cons != null) {
85                      list.add(cons);
86                  }
87              }
88          }
89          return list;
90      }
91  
92      private List<Constraint> getDictionaryAdditionalConstraints() {
93          List<Constraint> list = new ArrayList();
94          for (String id : dict.getAdditionalConstraintIds()) {
95              Constraint cons = findConstraint(id);
96              if (cons != null) {
97                  list.add(cons);
98              }
99          }
100         list.add(dict.getInlineConstraint());
101         return list;
102     }
103 
104     private Constraint findConstraint(String id) {
105         Constraint cons = new ModelFinder(this.model).findConstraint(id);
106         if (cons != null) {
107             return cons;
108         }
109         System.out.println("id=[" + id + "]");
110         if (id == null) {
111             System.out.println("id is null");
112         } else if (id.equals("")) {
113             System.out.println("id is empty string");
114         } else {
115             int i = 0;
116             for (byte b : id.getBytes()) {
117                 i++;
118                 System.out.println(i + ":" + b);
119             }
120         }
121         addError("Dictionary constraint id, " + id
122                 + " is not defined in the bank of constraints");
123         return null;
124     }
125 
126     private Field findField() {
127         Field field = finder.findField(dict);
128         if (field != null) {
129             return field;
130         }
131         addError("Dictionary with id , " + dict.getId()
132                 + " does not have a corresponding field defined in the message structure.");
133         return null;
134     }
135 
136     private String getConstraintId(Constraint cons) {
137         if (cons.getId().equals("")) {
138             return cons.getKey();
139         }
140         return cons.getId();
141     }
142 
143     private void validateComplexConstraint(Constraint cons) {
144         if (!cons.getMinLength().equals("")) {
145             addError(getConstraintId(cons)
146                     + " has a minLength which does not make sense on a complex field");
147         }
148         if (!cons.getMaxLength().equals("")) {
149             addError(getConstraintId(cons)
150                     + " has a maxLength which does not make sense on a complex field");
151         }
152         if (!cons.getMinValue().equals("")) {
153             addError(getConstraintId(cons)
154                     + " has a minValue which does not make sense on a complex field");
155         }
156         if (!cons.getMaxValue().equals("")) {
157             addError(getConstraintId(cons)
158                     + " has a maxValue which does not make sense on a complex field");
159         }
160         if (!cons.getValidChars().equals("")) {
161             addError(getConstraintId(cons)
162                     + " has validChars which does not make sense on a complex field");
163         }
164         if (!cons.getLookup().equals("")) {
165             addError(getConstraintId(cons)
166                     + " has a lookup which does not make sense on a complex field");
167         }
168     }
169 
170     private void validateForDuplicates() {
171         Set<String> ids = new HashSet();
172         for (Constraint cons : getAllNamedConstraints()) {
173             if (!ids.add(cons.getId())) {
174                 // optional is OK to be duplicated because it is is used to override
175                 // the 'required' setting
176                 if (!cons.getId().equals("optional")) {
177                     addError("Constraint with id of [" + cons.getId() + "] is duplicated");
178                 }
179             }
180         }
181     }
182 
183     private void addError(String msg) {
184         String error = "Error in dictionary entry: " + dict.getId() + ": " + msg;
185         if (!errors.contains(error)) {
186             errors.add(error);
187         }
188     }
189 }