Coverage Report - org.kuali.rice.krad.datadictionary.validation.processor.MustOccurConstraintProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
MustOccurConstraintProcessor
88%
31/35
76%
20/26
4.5
 
 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.processor;
 17  
 
 18  
 import org.kuali.rice.core.api.util.RiceKeyConstants;
 19  
 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
 20  
 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
 21  
 import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
 22  
 import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
 23  
 import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
 24  
 import org.kuali.rice.krad.datadictionary.validation.constraint.MustOccurConstraint;
 25  
 import org.kuali.rice.krad.datadictionary.validation.constraint.PrerequisiteConstraint;
 26  
 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
 27  
 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
 28  
 import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
 29  
 
 30  
 import java.util.List;
 31  
 
 32  
 /**
 33  
  * 
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org) 
 35  
  */
 36  9
 public class MustOccurConstraintProcessor extends BasePrerequisiteConstraintProcessor<MustOccurConstraint> {
 37  
 
 38  
         private static final String CONSTRAINT_NAME = "must occur constraint";
 39  
         
 40  
         /**
 41  
          * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#process(DictionaryValidationResult, Object, org.kuali.rice.krad.datadictionary.validation.capability.Constrainable, org.kuali.rice.krad.datadictionary.validation.AttributeValueReader)
 42  
          */
 43  
         @Override
 44  
         public ProcessorResult process(DictionaryValidationResult result,
 45  
                         Object value, MustOccurConstraint constraint, AttributeValueReader attributeValueReader)
 46  
                         throws AttributeValidationException {
 47  
 
 48  3
                 if (ValidationUtils.isNullOrEmpty(value))
 49  0
                         return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME));
 50  
                 
 51  
 
 52  3
                 ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME);
 53  
                                 
 54  3
                 if (!processMustOccurConstraint(constraintValidationResult, constraint, attributeValueReader)) {
 55  
                         // If the processing of this constraint was not successful then it's an error
 56  1
                     if (attributeValueReader.getAttributeName() == null){
 57  1
                         constraintValidationResult.setAttributeName(attributeValueReader.getPath());
 58  
                     } else{
 59  0
                         constraintValidationResult.setAttributeName(attributeValueReader.getAttributeName());
 60  0
                         constraintValidationResult.setAttributePath(attributeValueReader.getPath());
 61  
                     }
 62  1
                         constraintValidationResult.setError(RiceKeyConstants.ERROR_OCCURS);
 63  
                 } 
 64  
 
 65  
                 // Store the label key (if one exists) for this constraint on the constraint validation result so it can be shown later
 66  3
                 constraintValidationResult.setConstraintLabelKey(constraint.getLabelKey());
 67  
                 // Add it to the DictionaryValidationResult object
 68  3
                 result.addConstraintValidationResult(attributeValueReader, constraintValidationResult);
 69  
 
 70  3
                 return new ProcessorResult(constraintValidationResult);
 71  
 
 72  
         }
 73  
         
 74  
         @Override 
 75  
         public String getName() {
 76  12
                 return CONSTRAINT_NAME;
 77  
         }
 78  
 
 79  
         /**
 80  
          * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
 81  
          */
 82  
         @Override
 83  
         public Class<? extends Constraint> getConstraintType() {
 84  0
                 return MustOccurConstraint.class;
 85  
         }
 86  
         
 87  
     protected boolean processMustOccurConstraint(ConstraintValidationResult topLevelResult, MustOccurConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
 88  
 
 89  6
         boolean isSuccessful = false;
 90  6
         int trueCount = 0;
 91  
         
 92  6
         List<PrerequisiteConstraint> prerequisiteConstraints = constraint.getPrerequisiteConstraints();
 93  6
         if (prerequisiteConstraints != null) {
 94  6
                 for (PrerequisiteConstraint prerequisiteConstraint : prerequisiteConstraints) {
 95  9
                         ConstraintValidationResult constraintValidationResult = processPrerequisiteConstraint(prerequisiteConstraint, attributeValueReader);
 96  
                         // Add the result of each prerequisite constraint validation to the top level result object as a child
 97  9
                         topLevelResult.addChild(constraintValidationResult);
 98  9
                     trueCount += (constraintValidationResult.getStatus().getLevel() <= ErrorLevel.WARN.getLevel()) ? 1 : 0;
 99  9
                 }
 100  
         }
 101  
 
 102  6
         List<MustOccurConstraint> mustOccurConstraints = constraint.getMustOccurConstraints();
 103  6
         if (mustOccurConstraints != null) {
 104  3
                 for (MustOccurConstraint mustOccurConstraint : mustOccurConstraints) {
 105  
                         // Create a new constraint validation result for this must occur constraint and make it child of the top-level constraint, 
 106  
                         // then pass it in to the recursive call so that prerequisite constraints can be placed under it
 107  3
                         ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME);
 108  3
                         topLevelResult.addChild(constraintValidationResult);
 109  3
                     trueCount += (processMustOccurConstraint(constraintValidationResult, mustOccurConstraint, attributeValueReader)) ? 1 : 0;
 110  3
                 }
 111  
         }
 112  
 
 113  6
         int minimum = constraint.getMin() != null ? constraint.getMin().intValue() : 0;
 114  6
         int maximum = constraint.getMax() != null ? constraint.getMax().intValue() : 0;
 115  
         
 116  6
         isSuccessful = (trueCount >= minimum && trueCount <= maximum) ? true : false;
 117  
 
 118  6
         return isSuccessful;
 119  
     }
 120  
 
 121  
 }