Coverage Report - org.kuali.rice.krad.datadictionary.validation.processor.BasePrerequisiteConstraintProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
BasePrerequisiteConstraintProcessor
76%
16/21
43%
7/16
10
 
 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 org.kuali.rice.core.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.PrerequisiteConstraint;
 25  
 import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
 26  
 
 27  
 import java.util.Collection;
 28  
 
 29  
 /**
 30  
  * 
 31  
  * @author Kuali Rice Team (rice.collab@kuali.org) 
 32  
  */
 33  16
 public abstract class BasePrerequisiteConstraintProcessor<C extends Constraint> extends MandatoryElementConstraintProcessor<C> {
 34  
         
 35  
         protected ConstraintValidationResult processPrerequisiteConstraint(PrerequisiteConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
 36  
 
 37  15
                 ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(getName());
 38  
                 
 39  15
                 if (constraint == null) {
 40  1
                         constraintValidationResult.setStatus(ErrorLevel.NOCONSTRAINT);
 41  1
                         return constraintValidationResult;
 42  
                 }
 43  
                         
 44  
                 
 45  
             // TODO: Does this code need to be able to look at more than just the other immediate members of the object? 
 46  14
         String attributeName = constraint.getAttributePath();
 47  
         
 48  14
         if (ValidationUtils.isNullOrEmpty(attributeName)) {
 49  0
                 throw new AttributeValidationException("Prerequisite constraints must include the name of the attribute that is required");
 50  
         }
 51  
         
 52  14
         Object value = attributeValueReader.getValue(attributeName);
 53  
 
 54  14
         boolean isSuccessful = true;
 55  
 
 56  14
         if (value instanceof java.lang.String) {
 57  14
                 isSuccessful = ValidationUtils.hasText((String) value);
 58  0
         } else if (value instanceof Collection) {
 59  0
                 isSuccessful = (((Collection<?>) value).size() > 0);
 60  
         } else {
 61  0
                 isSuccessful = (null != value) ? true : false;
 62  
         }
 63  
 
 64  
         
 65  
         
 66  14
         if (!isSuccessful) {                
 67  9
                 String label = attributeValueReader.getLabel(attributeName); 
 68  9
                 if (label != null)
 69  0
                         attributeName = label;
 70  
                 
 71  9
                 constraintValidationResult.setError(RiceKeyConstants.ERROR_REQUIRES_FIELD, attributeName);
 72  
         } 
 73  
         
 74  14
         return constraintValidationResult;
 75  
     }
 76  
 
 77  
 }