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