View Javadoc

1   /**
2    * Copyright 2005-2011 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.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  import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
21  
22  import java.util.Iterator;
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  
26  /**
27   * 
28   * @author Kuali Rice Team (rice.collab@kuali.org) 
29   */
30  public class DictionaryValidationResult {
31  	
32  	private Map<String, EntryValidationResult> entryValidationResultMap;
33  	private ErrorLevel errorLevel;
34  	
35  	private int numberOfErrors;
36  	private int numberOfWarnings;
37  	
38  	private Iterator<ConstraintValidationResult> iterator;
39  	
40  	public DictionaryValidationResult() {
41  		this.entryValidationResultMap = new LinkedHashMap<String, EntryValidationResult>();
42  		this.errorLevel = ErrorLevel.ERROR;
43  		this.numberOfErrors = 0;
44  		this.numberOfWarnings = 0;
45  	}
46  	
47  	public void addConstraintValidationResult(AttributeValueReader attributeValueReader, ConstraintValidationResult constraintValidationResult) {
48  		
49  		// 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
50  		if (constraintValidationResult.getStatus().getLevel() < errorLevel.getLevel())
51  			return;
52  		
53  		switch (constraintValidationResult.getStatus()) {
54  		case ERROR:
55  			numberOfErrors++;
56  			break;
57  		case WARN:
58  			numberOfWarnings++;
59  			break;
60  		default:
61  			// Do nothing
62  		}
63  		
64  		// 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!
65  		String entryName = constraintValidationResult.getEntryName();
66  		String attributeName = constraintValidationResult.getAttributeName();
67  		String attributePath = constraintValidationResult.getAttributePath();
68  		
69  		if (entryName == null){
70  			entryName = attributeValueReader.getEntryName();
71  		}
72  		
73  		if (attributeName == null){
74  			attributeName = attributeValueReader.getAttributeName();
75  		}
76  		
77  		if (attributePath == null){
78  		    attributePath = attributeValueReader.getPath();
79  		}
80  		
81  		constraintValidationResult.setEntryName(entryName);
82  		constraintValidationResult.setAttributeName(attributeName);
83  		constraintValidationResult.setAttributePath(attributePath);
84  		
85  		String entryKey = getEntryValdidationResultKey(entryName, attributePath);
86  		getEntryValidationResult(entryKey).getAttributeValidationResult(attributeName).addConstraintValidationResult(constraintValidationResult);
87  	}
88  	
89  	public ConstraintValidationResult addError(AttributeValueReader attributeValueReader, String constraintName, String errorKey, String... errorParameters) {
90  		ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
91  		constraintValidationResult.setError(errorKey, errorParameters);
92  		numberOfErrors++;
93  		return constraintValidationResult;
94  	}
95  	
96  	public ConstraintValidationResult addWarning(AttributeValueReader attributeValueReader, String constraintName, String errorKey, String... errorParameters) {
97  		if (errorLevel.getLevel() > ErrorLevel.WARN.getLevel())
98  			return new ConstraintValidationResult(constraintName, ErrorLevel.WARN);
99  		
100 		ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
101 		constraintValidationResult.setWarning(errorKey, errorParameters);
102 		numberOfWarnings++;
103 		return constraintValidationResult;
104 	}
105 
106 	public ConstraintValidationResult addSuccess(AttributeValueReader attributeValueReader, String constraintName) {
107 		if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
108 			return new ConstraintValidationResult(constraintName, ErrorLevel.OK);
109 		
110 		return getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
111 	}
112 	
113 	public ConstraintValidationResult addSkipped(AttributeValueReader attributeValueReader, String constraintName) {
114 		if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
115 			return new ConstraintValidationResult(constraintName, ErrorLevel.INAPPLICABLE);
116 		
117 		ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
118 		constraintValidationResult.setStatus(ErrorLevel.INAPPLICABLE);
119 		return constraintValidationResult;
120 	}
121 	
122 	public ConstraintValidationResult addNoConstraint(AttributeValueReader attributeValueReader, String constraintName) {
123 		if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
124 			return new ConstraintValidationResult(constraintName, ErrorLevel.NOCONSTRAINT);
125 		
126 		ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
127 		constraintValidationResult.setStatus(ErrorLevel.NOCONSTRAINT);
128 		return constraintValidationResult;
129 	}
130 	
131 	public Iterator<ConstraintValidationResult> iterator() {
132 		
133 //		if (iterator == null || iterator.hasNext() == false) {
134 			iterator = new Iterator<ConstraintValidationResult>() {
135 	
136 				private Iterator<EntryValidationResult> entryIterator;
137 				private Iterator<AttributeValidationResult> attributeIterator;
138 				private Iterator<ConstraintValidationResult> constraintIterator;
139 				
140 				@Override
141 				public boolean hasNext() {	
142 					Iterator<ConstraintValidationResult> currentConstraintIterator = getCurrentConstraintIterator();
143 					return currentConstraintIterator != null && currentConstraintIterator.hasNext();
144 				}
145 	
146 				@Override
147 				public ConstraintValidationResult next() {
148 					Iterator<ConstraintValidationResult> currentConstraintIterator = getCurrentConstraintIterator();
149 					return currentConstraintIterator != null ? currentConstraintIterator.next() : null;
150 				}
151 	
152 				@Override
153 				public void remove() {
154 					throw new RuntimeException("Can't remove from this iterator!");
155 				}
156 				
157 				private Iterator<ConstraintValidationResult> getCurrentConstraintIterator() {
158 					if (constraintIterator == null || constraintIterator.hasNext() == false) {
159 						Iterator<AttributeValidationResult> currentAttributeIterator = getCurrentAttributeIterator();
160 						if (currentAttributeIterator != null && currentAttributeIterator.hasNext()) {
161 							AttributeValidationResult currentAttributeValidationResult = currentAttributeIterator.next();
162 							constraintIterator = currentAttributeValidationResult.iterator();
163 						}
164 					}
165 					return constraintIterator;
166 				}
167 				
168 				private Iterator<AttributeValidationResult> getCurrentAttributeIterator() {
169 					if (attributeIterator == null || attributeIterator.hasNext() == false) {
170 						Iterator<EntryValidationResult> currentEntryIterator = getCurrentEntryIterator();
171 						if (currentEntryIterator != null && currentEntryIterator.hasNext()) {
172 							EntryValidationResult currentEntryValidationResult = currentEntryIterator.next();
173 							attributeIterator = currentEntryValidationResult.iterator();
174 						}
175 					}
176 					return attributeIterator;
177 				}
178 	
179 				private Iterator<EntryValidationResult> getCurrentEntryIterator() {
180 					if (entryIterator == null) // || entryIterator.hasNext() == false)
181 						entryIterator = entryValidationResultMap.values().iterator();
182 					return entryIterator;
183 				}
184 				
185 			};
186 //		}
187 		
188 		return iterator;
189 	}
190 	
191 	protected EntryValidationResult getEntryValidationResult(String entryName) {
192 		EntryValidationResult entryValidationResult = entryValidationResultMap.get(entryName);
193 		if (entryValidationResult == null) {
194 			entryValidationResult = new EntryValidationResult(entryName);
195 			entryValidationResultMap.put(entryName, entryValidationResult);
196 		}
197 		return entryValidationResult;
198 	}
199 	
200 	private ConstraintValidationResult getConstraintValidationResult(String entryName, String attributeName, String attributePath, String constraintName) {
201 	    String entryKey = getEntryValdidationResultKey(entryName, attributePath);
202 	    ConstraintValidationResult constraintValidationResult = getEntryValidationResult(entryKey).getAttributeValidationResult(attributeName).getConstraintValidationResult(constraintName);
203 		constraintValidationResult.setEntryName(entryName);
204 		constraintValidationResult.setAttributeName(attributeName);
205 		constraintValidationResult.setAttributePath(attributePath);
206 		return constraintValidationResult;
207 	}
208 		
209 	/**
210      * Returns the key to the EntryValidationResult entry in the EntryValidationResultMap.
211      * Most cases entry key will be the entryName, unless the attribute is part of a collection,
212      * in which case entry key will be suffixed with index of attribute's parent item.
213      * 
214      * @param entryName
215      * @param attributePath
216      * @return
217      */
218     private String getEntryValdidationResultKey(String entryName, String attributePath) {
219         if (attributePath.contains("[")){
220             return entryName + "[" + ValidationUtils.getLastPathIndex(attributePath) + "]";
221         } 
222         return entryName;
223     }
224 
225     /**
226 	 * @return the errorLevel
227 	 */
228 	public ErrorLevel getErrorLevel() {
229 		return this.errorLevel;
230 	}
231 
232 	/**
233 	 * @param errorLevel the errorLevel to set
234 	 */
235 	public void setErrorLevel(ErrorLevel errorLevel) {
236 		this.errorLevel = errorLevel;
237 	}
238 
239 	/**
240 	 * @return the numberOfErrors
241 	 */
242 	public int getNumberOfErrors() {
243 		return this.numberOfErrors;
244 	}
245 
246 	/**
247 	 * @return the numberOfWarnings
248 	 */
249 	public int getNumberOfWarnings() {
250 		return this.numberOfWarnings;
251 	}
252 	
253 }