Coverage Report - org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult
 
Classes in this File Line Coverage Branch Coverage Complexity
DictionaryValidationResult
80%
59/73
56%
13/23
2.429
DictionaryValidationResult$1
95%
20/21
79%
19/24
2.429
DictionaryValidationResult$2
100%
1/1
N/A
2.429
 
 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  1
 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  144
         public DictionaryValidationResult() {
 41  144
                 this.entryValidationResultMap = new LinkedHashMap<String, EntryValidationResult>();
 42  144
                 this.errorLevel = ErrorLevel.ERROR;
 43  144
                 this.numberOfErrors = 0;
 44  144
                 this.numberOfWarnings = 0;
 45  144
         }
 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  66
                 if (constraintValidationResult.getStatus().getLevel() < errorLevel.getLevel())
 51  0
                         return;
 52  
                 
 53  66
                 switch (constraintValidationResult.getStatus()) {
 54  
                 case ERROR:
 55  62
                         numberOfErrors++;
 56  62
                         break;
 57  
                 case WARN:
 58  0
                         numberOfWarnings++;
 59  0
                         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  66
                 String entryName = constraintValidationResult.getEntryName();
 66  66
                 String attributeName = constraintValidationResult.getAttributeName();
 67  66
                 String attributePath = constraintValidationResult.getAttributePath();
 68  
                 
 69  66
                 if (entryName == null){
 70  66
                         entryName = attributeValueReader.getEntryName();
 71  
                 }
 72  
                 
 73  66
                 if (attributeName == null){
 74  65
                         attributeName = attributeValueReader.getAttributeName();
 75  
                 }
 76  
                 
 77  66
                 if (attributePath == null){
 78  66
                     attributePath = attributeValueReader.getPath();
 79  
                 }
 80  
                 
 81  66
                 constraintValidationResult.setEntryName(entryName);
 82  66
                 constraintValidationResult.setAttributeName(attributeName);
 83  66
                 constraintValidationResult.setAttributePath(attributePath);
 84  
                 
 85  66
                 String entryKey = getEntryValdidationResultKey(entryName, attributePath);
 86  66
                 getEntryValidationResult(entryKey).getAttributeValidationResult(attributeName).addConstraintValidationResult(constraintValidationResult);
 87  66
         }
 88  
         
 89  
         public ConstraintValidationResult addError(AttributeValueReader attributeValueReader, String constraintName, String errorKey, String... errorParameters) {
 90  8
                 ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
 91  8
                 constraintValidationResult.setError(errorKey, errorParameters);
 92  8
                 numberOfErrors++;
 93  8
                 return constraintValidationResult;
 94  
         }
 95  
         
 96  
         public ConstraintValidationResult addWarning(AttributeValueReader attributeValueReader, String constraintName, String errorKey, String... errorParameters) {
 97  0
                 if (errorLevel.getLevel() > ErrorLevel.WARN.getLevel())
 98  0
                         return new ConstraintValidationResult(constraintName, ErrorLevel.WARN);
 99  
                 
 100  0
                 ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
 101  0
                 constraintValidationResult.setWarning(errorKey, errorParameters);
 102  0
                 numberOfWarnings++;
 103  0
                 return constraintValidationResult;
 104  
         }
 105  
 
 106  
         public ConstraintValidationResult addSuccess(AttributeValueReader attributeValueReader, String constraintName) {
 107  51
                 if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
 108  0
                         return new ConstraintValidationResult(constraintName, ErrorLevel.OK);
 109  
                 
 110  51
                 return getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
 111  
         }
 112  
         
 113  
         public ConstraintValidationResult addSkipped(AttributeValueReader attributeValueReader, String constraintName) {
 114  13
                 if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
 115  0
                         return new ConstraintValidationResult(constraintName, ErrorLevel.INAPPLICABLE);
 116  
                 
 117  13
                 ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
 118  13
                 constraintValidationResult.setStatus(ErrorLevel.INAPPLICABLE);
 119  13
                 return constraintValidationResult;
 120  
         }
 121  
         
 122  
         public ConstraintValidationResult addNoConstraint(AttributeValueReader attributeValueReader, String constraintName) {
 123  5
                 if (errorLevel.getLevel() > ErrorLevel.OK.getLevel())
 124  0
                         return new ConstraintValidationResult(constraintName, ErrorLevel.NOCONSTRAINT);
 125  
                 
 126  5
                 ConstraintValidationResult constraintValidationResult = getConstraintValidationResult(attributeValueReader.getEntryName(), attributeValueReader.getAttributeName(), attributeValueReader.getPath(), constraintName);
 127  5
                 constraintValidationResult.setStatus(ErrorLevel.NOCONSTRAINT);
 128  5
                 return constraintValidationResult;
 129  
         }
 130  
         
 131  
         public Iterator<ConstraintValidationResult> iterator() {
 132  
                 
 133  
 //                if (iterator == null || iterator.hasNext() == false) {
 134  1
                         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  2
                                         Iterator<ConstraintValidationResult> currentConstraintIterator = getCurrentConstraintIterator();
 143  2
                                         return currentConstraintIterator != null && currentConstraintIterator.hasNext();
 144  
                                 }
 145  
         
 146  
                                 @Override
 147  
                                 public ConstraintValidationResult next() {
 148  1
                                         Iterator<ConstraintValidationResult> currentConstraintIterator = getCurrentConstraintIterator();
 149  1
                                         return currentConstraintIterator != null ? currentConstraintIterator.next() : null;
 150  
                                 }
 151  
         
 152  
                                 @Override
 153  
                                 public void remove() {
 154  0
                                         throw new RuntimeException("Can't remove from this iterator!");
 155  
                                 }
 156  
                                 
 157  
                                 private Iterator<ConstraintValidationResult> getCurrentConstraintIterator() {
 158  3
                                         if (constraintIterator == null || constraintIterator.hasNext() == false) {
 159  2
                                                 Iterator<AttributeValidationResult> currentAttributeIterator = getCurrentAttributeIterator();
 160  2
                                                 if (currentAttributeIterator != null && currentAttributeIterator.hasNext()) {
 161  1
                                                         AttributeValidationResult currentAttributeValidationResult = currentAttributeIterator.next();
 162  1
                                                         constraintIterator = currentAttributeValidationResult.iterator();
 163  
                                                 }
 164  
                                         }
 165  3
                                         return constraintIterator;
 166  
                                 }
 167  
                                 
 168  
                                 private Iterator<AttributeValidationResult> getCurrentAttributeIterator() {
 169  2
                                         if (attributeIterator == null || attributeIterator.hasNext() == false) {
 170  2
                                                 Iterator<EntryValidationResult> currentEntryIterator = getCurrentEntryIterator();
 171  2
                                                 if (currentEntryIterator != null && currentEntryIterator.hasNext()) {
 172  1
                                                         EntryValidationResult currentEntryValidationResult = currentEntryIterator.next();
 173  1
                                                         attributeIterator = currentEntryValidationResult.iterator();
 174  
                                                 }
 175  
                                         }
 176  2
                                         return attributeIterator;
 177  
                                 }
 178  
         
 179  
                                 private Iterator<EntryValidationResult> getCurrentEntryIterator() {
 180  2
                                         if (entryIterator == null) // || entryIterator.hasNext() == false)
 181  1
                                                 entryIterator = entryValidationResultMap.values().iterator();
 182  2
                                         return entryIterator;
 183  
                                 }
 184  
                                 
 185  
                         };
 186  
 //                }
 187  
                 
 188  1
                 return iterator;
 189  
         }
 190  
         
 191  
         protected EntryValidationResult getEntryValidationResult(String entryName) {
 192  143
                 EntryValidationResult entryValidationResult = entryValidationResultMap.get(entryName);
 193  143
                 if (entryValidationResult == null) {
 194  141
                         entryValidationResult = new EntryValidationResult(entryName);
 195  141
                         entryValidationResultMap.put(entryName, entryValidationResult);
 196  
                 }
 197  143
                 return entryValidationResult;
 198  
         }
 199  
         
 200  
         private ConstraintValidationResult getConstraintValidationResult(String entryName, String attributeName, String attributePath, String constraintName) {
 201  77
             String entryKey = getEntryValdidationResultKey(entryName, attributePath);
 202  77
             ConstraintValidationResult constraintValidationResult = getEntryValidationResult(entryKey).getAttributeValidationResult(attributeName).getConstraintValidationResult(constraintName);
 203  77
                 constraintValidationResult.setEntryName(entryName);
 204  77
                 constraintValidationResult.setAttributeName(attributeName);
 205  77
                 constraintValidationResult.setAttributePath(attributePath);
 206  77
                 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  143
         if (attributePath.contains("[")){
 220  0
             return entryName + "[" + ValidationUtils.getLastPathIndex(attributePath) + "]";
 221  
         } 
 222  143
         return entryName;
 223  
     }
 224  
 
 225  
     /**
 226  
          * @return the errorLevel
 227  
          */
 228  
         public ErrorLevel getErrorLevel() {
 229  0
                 return this.errorLevel;
 230  
         }
 231  
 
 232  
         /**
 233  
          * @param errorLevel the errorLevel to set
 234  
          */
 235  
         public void setErrorLevel(ErrorLevel errorLevel) {
 236  143
                 this.errorLevel = errorLevel;
 237  143
         }
 238  
 
 239  
         /**
 240  
          * @return the numberOfErrors
 241  
          */
 242  
         public int getNumberOfErrors() {
 243  142
                 return this.numberOfErrors;
 244  
         }
 245  
 
 246  
         /**
 247  
          * @return the numberOfWarnings
 248  
          */
 249  
         public int getNumberOfWarnings() {
 250  133
                 return this.numberOfWarnings;
 251  
         }
 252  
         
 253  
 }