Coverage Report - org.kuali.rice.krad.datadictionary.validation.processor.CollectionSizeConstraintProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionSizeConstraintProcessor
0%
0/21
0%
0/18
3.6
 
 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.processor;
 17  
 
 18  
 import java.util.Collection;
 19  
 
 20  
 import org.kuali.rice.core.util.RiceKeyConstants;
 21  
 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
 22  
 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
 23  
 import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
 24  
 import org.kuali.rice.krad.datadictionary.validation.ValidationUtils.Result;
 25  
 import org.kuali.rice.krad.datadictionary.validation.constraint.CollectionSizeConstraint;
 26  
 import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
 27  
 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
 28  
 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
 29  
 import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
 30  
 
 31  
 /**
 32  
  * This class validates attributes that are collection size constrained - ones that can only have between x and y number 
 33  
  * 
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org) 
 35  
  */
 36  0
 public class CollectionSizeConstraintProcessor implements CollectionConstraintProcessor<Collection<?>, CollectionSizeConstraint> {
 37  
 
 38  
         private static final String CONSTRAINT_NAME = "collection size constraint";
 39  
         
 40  
         /**
 41  
          * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#process(DictionaryValidationResult, Object, org.kuali.rice.krad.datadictionary.validation.capability.Validatable, org.kuali.rice.krad.datadictionary.validation.AttributeValueReader)
 42  
          */
 43  
         @Override
 44  
         public ProcessorResult process(DictionaryValidationResult result, Collection<?> collection, CollectionSizeConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
 45  
                 
 46  
                 // To accommodate the needs of other processors, the ConstraintProcessor.process() method returns a list of ConstraintValidationResult objects
 47  
                 // but since a definition that is collection size constrained only provides a single max and minimum, there is effectively a single constraint
 48  
                 // being imposed.
 49  0
         return new ProcessorResult(processSingleCollectionSizeConstraint(result, collection, constraint, attributeValueReader));
 50  
         }
 51  
 
 52  
         @Override 
 53  
         public String getName() {
 54  0
                 return CONSTRAINT_NAME;
 55  
         }
 56  
         
 57  
         /**
 58  
          * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
 59  
          */
 60  
         @Override
 61  
         public Class<? extends Constraint> getConstraintType() {
 62  0
                 return CollectionSizeConstraint.class;
 63  
         }
 64  
 
 65  
         /**
 66  
          * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#isOptional()
 67  
          */
 68  
         @Override
 69  
         public boolean isOptional() {
 70  0
                 return false;
 71  
         }
 72  
 
 73  
         protected ConstraintValidationResult processSingleCollectionSizeConstraint(DictionaryValidationResult result, Collection<?> collection, CollectionSizeConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
 74  0
                 Integer sizeOfCollection = Integer.valueOf(collection.size());
 75  
                 
 76  0
                 Integer maxOccurances = constraint.getMaximumNumberOfElements();
 77  0
                 Integer minOccurances = constraint.getMinimumNumberOfElements();
 78  
                 
 79  0
                 Result lessThanMax = ValidationUtils.isLessThanOrEqual(sizeOfCollection, maxOccurances);
 80  0
                 Result greaterThanMin = ValidationUtils.isGreaterThanOrEqual(sizeOfCollection, minOccurances);
 81  
 
 82  
                 // It's okay for one end of the range to be undefined - that's not an error. It's only an error if one of them is invalid 
 83  0
         if (lessThanMax != Result.INVALID && greaterThanMin != Result.INVALID) { 
 84  
                 // Of course, if they're both undefined then we didn't actually have a real constraint
 85  0
                 if (lessThanMax == Result.UNDEFINED && greaterThanMin == Result.UNDEFINED)
 86  0
                         return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME);
 87  
                 
 88  
                 // In this case, we've succeeded
 89  0
                 return result.addSuccess(attributeValueReader, CONSTRAINT_NAME);
 90  
         }
 91  
         
 92  0
                 String maxErrorParameter = maxOccurances != null ? maxOccurances.toString() : null;
 93  0
                 String minErrorParameter = minOccurances != null ? minOccurances.toString() : null;
 94  
         
 95  
         // If both comparisons happened then if either comparison failed we can show the end user the expected range on both sides.
 96  0
         if (lessThanMax != Result.UNDEFINED && greaterThanMin != Result.UNDEFINED) 
 97  0
                 return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_QUANTITY_RANGE, minErrorParameter, maxErrorParameter);
 98  
         // If it's the max comparison that fails, then just tell the end user what the max can be
 99  0
         else if (lessThanMax == Result.INVALID)
 100  0
                 return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_MAX_OCCURS, maxErrorParameter);
 101  
         // Otherwise, just tell them what the min can be
 102  
         else 
 103  0
                 return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_MIN_OCCURS, minErrorParameter);
 104  
         
 105  
         // Obviously the last else above is unnecessary, since anything after it is dead code, but keeping it seems clearer than dropping it
 106  
         }
 107  
         
 108  
 }