Coverage Report - org.kuali.rice.krad.datadictionary.validation.processor.ExistenceConstraintProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
ExistenceConstraintProcessor
81%
13/16
57%
8/14
2.8
 
 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.DataObjectEntry;
 20  
 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
 21  
 import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
 22  
 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
 23  
 import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
 24  
 import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
 25  
 import org.kuali.rice.krad.datadictionary.validation.constraint.ExistenceConstraint;
 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  
 /**
 31  
  * 
 32  
  * @author Kuali Rice Team (rice.collab@kuali.org) 
 33  
  */
 34  12
 public class ExistenceConstraintProcessor extends OptionalElementConstraintProcessor<ExistenceConstraint> {
 35  
 
 36  
         private static final String CONSTRAINT_NAME = "existence constraint";
 37  
         
 38  
         /**
 39  
          * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#process(DictionaryValidationResult, Object, org.kuali.rice.krad.datadictionary.validation.Validatable, org.kuali.rice.krad.datadictionary.validation.AttributeValueReader)
 40  
          */
 41  
         @Override
 42  
         public ProcessorResult process(DictionaryValidationResult result, Object value, ExistenceConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
 43  
 
 44  
                 // To accommodate the needs of other processors, the ConstraintProcessor.process() method returns a list of ConstraintValidationResult objects
 45  
                 // but since a definition that is existence constrained only provides a single isRequired field, there is effectively a single constraint
 46  
                 // being imposed.
 47  6
                 return new ProcessorResult(processSingleExistenceConstraint(result, value, constraint, attributeValueReader));
 48  
         }
 49  
 
 50  
         @Override 
 51  
         public String getName() {
 52  6
                 return CONSTRAINT_NAME;
 53  
         }
 54  
         
 55  
         /**
 56  
          * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
 57  
          */
 58  
         @Override
 59  
         public Class<? extends Constraint> getConstraintType() {
 60  0
                 return ExistenceConstraint.class;
 61  
         }
 62  
 
 63  
         protected ConstraintValidationResult processSingleExistenceConstraint(DictionaryValidationResult result, Object value, ExistenceConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
 64  
                 // If it's not set, then there's no constraint
 65  6
                 if (constraint.isRequired() == null)
 66  2
                         return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME);
 67  
                 
 68  4
                 if (constraint.isRequired().booleanValue() && !skipConstraint(attributeValueReader)) {
 69  
                         // If this attribute is required and the value is null then 
 70  2
                         if (ValidationUtils.isNullOrEmpty(value)) 
 71  1
                                 return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_REQUIRED);
 72  
                         
 73  1
                         return result.addSuccess(attributeValueReader, CONSTRAINT_NAME);
 74  
                 }
 75  
 
 76  2
                 return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);
 77  
         }
 78  
 
 79  
     /**
 80  
      * Checks to see if existence constraint should be skipped.  Required constraint should be skipped if it is an attribute of a complex
 81  
      * attribute and the complex attribute is not required.
 82  
      * 
 83  
      * @param attributeValueReader
 84  
      * @return
 85  
      */
 86  
     private boolean skipConstraint(AttributeValueReader attributeValueReader) {
 87  2
         boolean skipConstraint = false;
 88  2
         if (attributeValueReader instanceof DictionaryObjectAttributeValueReader){
 89  0
             DictionaryObjectAttributeValueReader dictionaryValueReader = (DictionaryObjectAttributeValueReader)attributeValueReader;
 90  0
             skipConstraint = dictionaryValueReader.isNestedAttribute() && dictionaryValueReader.isParentAttributeNull();
 91  
         }
 92  2
         return skipConstraint;
 93  
     }
 94  
         
 95  
         
 96  
 }