Coverage Report - org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult
 
Classes in this File Line Coverage Branch Coverage Complexity
DictionaryValidationResult
79%
51/64
52%
10/19
2.35
DictionaryValidationResult$1
95%
20/21
79%
19/24
2.35
DictionaryValidationResult$2
100%
1/1
N/A
2.35
 
 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  1
 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  39
         public DictionaryValidationResult() {
 40  39
                 this.entryValidationResultMap = new LinkedHashMap<String, EntryValidationResult>();
 41  39
                 this.errorLevel = ErrorLevel.ERROR;
 42  39
                 this.numberOfErrors = 0;
 43  39
                 this.numberOfWarnings = 0;
 44  39
         }
 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  11
                 if (constraintValidationResult.getStatus().getLevel() < errorLevel.getLevel())
 50  0
                         return;
 51  
                 
 52  11
                 switch (constraintValidationResult.getStatus()) {
 53  
                 case ERROR:
 54  7
                         numberOfErrors++;
 55  7
                         break;
 56  
                 case WARN:
 57  0
                         numberOfWarnings++;
 58  0
                         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  11
                 String entryName = constraintValidationResult.getEntryName();
 65  11
                 String attributeName = constraintValidationResult.getAttributeName();
 66  
                 
 67  11
                 if (entryName == null)
 68  11
                         entryName = attributeValueReader.getEntryName();
 69  
                 
 70  11
                 if (attributeName == null)
 71  11
                         attributeName = attributeValueReader.getAttributeName();
 72  
                 
 73  11
                 constraintValidationResult.setEntryName(entryName);
 74  11
                 constraintValidationResult.setAttributeName(attributeName);
 75  
                 
 76  11
                 getEntryValidationResult(entryName).getAttributeValidationResult(attributeName).addConstraintValidationResult(constraintValidationResult);
 77  11
         }
 78  
         
 79  
         public ConstraintValidationResult addError(AttributeValueReader attributeValueReader, String constraintName, String errorKey, String... errorParameters) {
 80  8
                 ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
 81  8
                 constraintValidationResult.setError(errorKey, errorParameters);
 82  8
                 numberOfErrors++;
 83  8
                 return constraintValidationResult;
 84  
         }
 85  
         
 86  
         public ConstraintValidationResult addWarning(AttributeValueReader attributeValueReader, String constraintName, String errorKey, String... errorParameters) {
 87  0
                 if (errorLevel.getLevel() > ErrorLevel.WARN.getLevel())
 88  0
                         return new ConstraintValidationResult(constraintName, ErrorLevel.WARN);
 89  
                 
 90  0
                 ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
 91  0
                 constraintValidationResult.setWarning(errorKey, errorParameters);
 92  0
                 numberOfWarnings++;
 93  0
                 return constraintValidationResult;
 94  
         }
 95  
 
 96  
         public ConstraintValidationResult addSuccess(AttributeValueReader attributeValueReader, String constraintName) {
 97  13
                 if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
 98  0
                         return new ConstraintValidationResult(constraintName, ErrorLevel.OK);
 99  
                 
 100  13
                 return getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
 101  
         }
 102  
         
 103  
         public ConstraintValidationResult addSkipped(AttributeValueReader attributeValueReader, String constraintName) {
 104  4
                 if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
 105  0
                         return new ConstraintValidationResult(constraintName, ErrorLevel.INAPPLICABLE);
 106  
                 
 107  4
                 ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
 108  4
                 constraintValidationResult.setStatus(ErrorLevel.INAPPLICABLE);
 109  4
                 return constraintValidationResult;
 110  
         }
 111  
         
 112  
         public ConstraintValidationResult addNoConstraint(AttributeValueReader attributeValueReader, String constraintName) {
 113  5
                 if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
 114  0
                         return new ConstraintValidationResult(constraintName, ErrorLevel.NOCONSTRAINT);
 115  
                 
 116  5
                 ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
 117  5
                 constraintValidationResult.setStatus(ErrorLevel.NOCONSTRAINT);
 118  5
                 return constraintValidationResult;
 119  
         }
 120  
         
 121  
         public Iterator<ConstraintValidationResult> iterator() {
 122  
                 
 123  
 //                if (iterator == null || iterator.hasNext() == false) {
 124  1
                         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  2
                                         Iterator<ConstraintValidationResult> currentConstraintIterator = getCurrentConstraintIterator();
 133  2
                                         return currentConstraintIterator != null && currentConstraintIterator.hasNext();
 134  
                                 }
 135  
         
 136  
                                 @Override
 137  
                                 public ConstraintValidationResult next() {
 138  1
                                         Iterator<ConstraintValidationResult> currentConstraintIterator = getCurrentConstraintIterator();
 139  1
                                         return currentConstraintIterator != null ? currentConstraintIterator.next() : null;
 140  
                                 }
 141  
         
 142  
                                 @Override
 143  
                                 public void remove() {
 144  0
                                         throw new RuntimeException("Can't remove from this iterator!");
 145  
                                 }
 146  
                                 
 147  
                                 private Iterator<ConstraintValidationResult> getCurrentConstraintIterator() {
 148  3
                                         if (constraintIterator == null || constraintIterator.hasNext() == false) {
 149  2
                                                 Iterator<AttributeValidationResult> currentAttributeIterator = getCurrentAttributeIterator();
 150  2
                                                 if (currentAttributeIterator != null && currentAttributeIterator.hasNext()) {
 151  1
                                                         AttributeValidationResult currentAttributeValidationResult = currentAttributeIterator.next();
 152  1
                                                         constraintIterator = currentAttributeValidationResult.iterator();
 153  
                                                 }
 154  
                                         }
 155  3
                                         return constraintIterator;
 156  
                                 }
 157  
                                 
 158  
                                 private Iterator<AttributeValidationResult> getCurrentAttributeIterator() {
 159  2
                                         if (attributeIterator == null || attributeIterator.hasNext() == false) {
 160  2
                                                 Iterator<EntryValidationResult> currentEntryIterator = getCurrentEntryIterator();
 161  2
                                                 if (currentEntryIterator != null && currentEntryIterator.hasNext()) {
 162  1
                                                         EntryValidationResult currentEntryValidationResult = currentEntryIterator.next();
 163  1
                                                         attributeIterator = currentEntryValidationResult.iterator();
 164  
                                                 }
 165  
                                         }
 166  2
                                         return attributeIterator;
 167  
                                 }
 168  
         
 169  
                                 private Iterator<EntryValidationResult> getCurrentEntryIterator() {
 170  2
                                         if (entryIterator == null) // || entryIterator.hasNext() == false)
 171  1
                                                 entryIterator = entryValidationResultMap.values().iterator();
 172  2
                                         return entryIterator;
 173  
                                 }
 174  
                                 
 175  
                         };
 176  
 //                }
 177  
                 
 178  1
                 return iterator;
 179  
         }
 180  
         
 181  
         protected EntryValidationResult getEntryValidationResult(String entryName) {
 182  41
                 EntryValidationResult entryValidationResult = entryValidationResultMap.get(entryName);
 183  41
                 if (entryValidationResult == null) {
 184  39
                         entryValidationResult = new EntryValidationResult(entryName);
 185  39
                         entryValidationResultMap.put(entryName, entryValidationResult);
 186  
                 }
 187  41
                 return entryValidationResult;
 188  
         }
 189  
         
 190  
         private ConstraintValidationResult getConstraintValidationResult(String entryName, String attributeName, String attributePath, String constraintName) {
 191  30
                 ConstraintValidationResult constraintValidationResult = getEntryValidationResult(entryName).getAttributeValidationResult(attributeName).getConstraintValidationResult(constraintName);
 192  30
                 constraintValidationResult.setEntryName(entryName);
 193  30
                 constraintValidationResult.setAttributeName(attributeName);
 194  30
                 constraintValidationResult.setAttributePath(attributePath);
 195  30
                 return constraintValidationResult;
 196  
         }
 197  
 
 198  
         /**
 199  
          * @return the errorLevel
 200  
          */
 201  
         public ErrorLevel getErrorLevel() {
 202  0
                 return this.errorLevel;
 203  
         }
 204  
 
 205  
         /**
 206  
          * @param errorLevel the errorLevel to set
 207  
          */
 208  
         public void setErrorLevel(ErrorLevel errorLevel) {
 209  38
                 this.errorLevel = errorLevel;
 210  38
         }
 211  
 
 212  
         /**
 213  
          * @return the numberOfErrors
 214  
          */
 215  
         public int getNumberOfErrors() {
 216  40
                 return this.numberOfErrors;
 217  
         }
 218  
 
 219  
         /**
 220  
          * @return the numberOfWarnings
 221  
          */
 222  
         public int getNumberOfWarnings() {
 223  31
                 return this.numberOfWarnings;
 224  
         }
 225  
         
 226  
 }