View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.rice.krad.datadictionary.validation.result;
17  
18  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
19  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
20  
21  import java.util.Iterator;
22  import java.util.LinkedHashMap;
23  import java.util.Map;
24  
25  /**
26   * 
27   * @author Kuali Rice Team (rice.collab@kuali.org) 
28   */
29  public class DictionaryValidationResult {
30  	
31  	private Map<String, EntryValidationResult> entryValidationResultMap;
32  	private ErrorLevel errorLevel;
33  	
34  	private int numberOfErrors;
35  	private int numberOfWarnings;
36  	
37  	private Iterator<ConstraintValidationResult> iterator;
38  	
39  	public DictionaryValidationResult() {
40  		this.entryValidationResultMap = new LinkedHashMap<String, EntryValidationResult>();
41  		this.errorLevel = ErrorLevel.ERROR;
42  		this.numberOfErrors = 0;
43  		this.numberOfWarnings = 0;
44  	}
45  	
46  	public void addConstraintValidationResult(AttributeValueReader attributeValueReader, ConstraintValidationResult constraintValidationResult) {
47  		
48  		// Don't bother to store this if the error level of the constraint validation result is lower than the level this dictionary validation result is tracking
49  		if (constraintValidationResult.getStatus().getLevel() < errorLevel.getLevel())
50  			return;
51  		
52  		switch (constraintValidationResult.getStatus()) {
53  		case ERROR:
54  			numberOfErrors++;
55  			break;
56  		case WARN:
57  			numberOfWarnings++;
58  			break;
59  		default:
60  			// Do nothing
61  		}
62  		
63  		// Give the constraint a chance to override the entry and attribute name - important if the attribute name is not the same as the one in the attribute value reader!
64  		String entryName = constraintValidationResult.getEntryName();
65  		String attributeName = constraintValidationResult.getAttributeName();
66  		
67  		if (entryName == null)
68  			entryName = attributeValueReader.getEntryName();
69  		
70  		if (attributeName == null)
71  			attributeName = attributeValueReader.getAttributeName();
72  		
73  		constraintValidationResult.setEntryName(entryName);
74  		constraintValidationResult.setAttributeName(attributeName);
75  		
76  		getEntryValidationResult(entryName).getAttributeValidationResult(attributeName).addConstraintValidationResult(constraintValidationResult);
77  	}
78  	
79  	public ConstraintValidationResult addError(AttributeValueReader attributeValueReader, String constraintName, String errorKey, String... errorParameters) {
80  		ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
81  		constraintValidationResult.setError(errorKey, errorParameters);
82  		numberOfErrors++;
83  		return constraintValidationResult;
84  	}
85  	
86  	public ConstraintValidationResult addWarning(AttributeValueReader attributeValueReader, String constraintName, String errorKey, String... errorParameters) {
87  		if (errorLevel.getLevel() > ErrorLevel.WARN.getLevel())
88  			return new ConstraintValidationResult(constraintName, ErrorLevel.WARN);
89  		
90  		ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
91  		constraintValidationResult.setWarning(errorKey, errorParameters);
92  		numberOfWarnings++;
93  		return constraintValidationResult;
94  	}
95  
96  	public ConstraintValidationResult addSuccess(AttributeValueReader attributeValueReader, String constraintName) {
97  		if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
98  			return new ConstraintValidationResult(constraintName, ErrorLevel.OK);
99  		
100 		return getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
101 	}
102 	
103 	public ConstraintValidationResult addSkipped(AttributeValueReader attributeValueReader, String constraintName) {
104 		if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
105 			return new ConstraintValidationResult(constraintName, ErrorLevel.INAPPLICABLE);
106 		
107 		ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
108 		constraintValidationResult.setStatus(ErrorLevel.INAPPLICABLE);
109 		return constraintValidationResult;
110 	}
111 	
112 	public ConstraintValidationResult addNoConstraint(AttributeValueReader attributeValueReader, String constraintName) {
113 		if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
114 			return new ConstraintValidationResult(constraintName, ErrorLevel.NOCONSTRAINT);
115 		
116 		ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
117 		constraintValidationResult.setStatus(ErrorLevel.NOCONSTRAINT);
118 		return constraintValidationResult;
119 	}
120 	
121 	public Iterator<ConstraintValidationResult> iterator() {
122 		
123 //		if (iterator == null || iterator.hasNext() == false) {
124 			iterator = new Iterator<ConstraintValidationResult>() {
125 	
126 				private Iterator<EntryValidationResult> entryIterator;
127 				private Iterator<AttributeValidationResult> attributeIterator;
128 				private Iterator<ConstraintValidationResult> constraintIterator;
129 				
130 				@Override
131 				public boolean hasNext() {	
132 					Iterator<ConstraintValidationResult> currentConstraintIterator = getCurrentConstraintIterator();
133 					return currentConstraintIterator != null && currentConstraintIterator.hasNext();
134 				}
135 	
136 				@Override
137 				public ConstraintValidationResult next() {
138 					Iterator<ConstraintValidationResult> currentConstraintIterator = getCurrentConstraintIterator();
139 					return currentConstraintIterator != null ? currentConstraintIterator.next() : null;
140 				}
141 	
142 				@Override
143 				public void remove() {
144 					throw new RuntimeException("Can't remove from this iterator!");
145 				}
146 				
147 				private Iterator<ConstraintValidationResult> getCurrentConstraintIterator() {
148 					if (constraintIterator == null || constraintIterator.hasNext() == false) {
149 						Iterator<AttributeValidationResult> currentAttributeIterator = getCurrentAttributeIterator();
150 						if (currentAttributeIterator != null && currentAttributeIterator.hasNext()) {
151 							AttributeValidationResult currentAttributeValidationResult = currentAttributeIterator.next();
152 							constraintIterator = currentAttributeValidationResult.iterator();
153 						}
154 					}
155 					return constraintIterator;
156 				}
157 				
158 				private Iterator<AttributeValidationResult> getCurrentAttributeIterator() {
159 					if (attributeIterator == null || attributeIterator.hasNext() == false) {
160 						Iterator<EntryValidationResult> currentEntryIterator = getCurrentEntryIterator();
161 						if (currentEntryIterator != null && currentEntryIterator.hasNext()) {
162 							EntryValidationResult currentEntryValidationResult = currentEntryIterator.next();
163 							attributeIterator = currentEntryValidationResult.iterator();
164 						}
165 					}
166 					return attributeIterator;
167 				}
168 	
169 				private Iterator<EntryValidationResult> getCurrentEntryIterator() {
170 					if (entryIterator == null) // || entryIterator.hasNext() == false)
171 						entryIterator = entryValidationResultMap.values().iterator();
172 					return entryIterator;
173 				}
174 				
175 			};
176 //		}
177 		
178 		return iterator;
179 	}
180 	
181 	protected EntryValidationResult getEntryValidationResult(String entryName) {
182 		EntryValidationResult entryValidationResult = entryValidationResultMap.get(entryName);
183 		if (entryValidationResult == null) {
184 			entryValidationResult = new EntryValidationResult(entryName);
185 			entryValidationResultMap.put(entryName, entryValidationResult);
186 		}
187 		return entryValidationResult;
188 	}
189 	
190 	private ConstraintValidationResult getConstraintValidationResult(String entryName, String attributeName, String attributePath, String constraintName) {
191 		ConstraintValidationResult constraintValidationResult = getEntryValidationResult(entryName).getAttributeValidationResult(attributeName).getConstraintValidationResult(constraintName);
192 		constraintValidationResult.setEntryName(entryName);
193 		constraintValidationResult.setAttributeName(attributeName);
194 		constraintValidationResult.setAttributePath(attributePath);
195 		return constraintValidationResult;
196 	}
197 
198 	/**
199 	 * @return the errorLevel
200 	 */
201 	public ErrorLevel getErrorLevel() {
202 		return this.errorLevel;
203 	}
204 
205 	/**
206 	 * @param errorLevel the errorLevel to set
207 	 */
208 	public void setErrorLevel(ErrorLevel errorLevel) {
209 		this.errorLevel = errorLevel;
210 	}
211 
212 	/**
213 	 * @return the numberOfErrors
214 	 */
215 	public int getNumberOfErrors() {
216 		return this.numberOfErrors;
217 	}
218 
219 	/**
220 	 * @return the numberOfWarnings
221 	 */
222 	public int getNumberOfWarnings() {
223 		return this.numberOfWarnings;
224 	}
225 	
226 }