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