Coverage Report - org.kuali.rice.krad.datadictionary.validation.SingleAttributeValueReader
 
Classes in this File Line Coverage Branch Coverage Complexity
SingleAttributeValueReader
68%
17/25
40%
9/22
2.4
 
 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;
 17  
 
 18  
 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
 19  
 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
 20  
 import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
 21  
 import org.kuali.rice.krad.datadictionary.validation.constraint.DataTypeConstraint;
 22  
 
 23  
 import java.util.List;
 24  
 
 25  
 
 26  
 /**
 27  
  * This class allows a single attribute value to be exposed to the validation service, along 
 28  
  * with some guidance about how that value should be interpreted, provided by the AttributeDefinition
 29  
  * that corresponds. It's a special AttributeValueReader since it explicitly doesn't expose any
 30  
  * other attribute values, so it should only be used when the underlying business object is not available
 31  
  * and we want to limit access to (for example) validation that requires only a single attribute value. 
 32  
  * This eliminates more complicated validation like 'this field is required when another field is filled in.'
 33  
  * 
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org) 
 35  
  */
 36  
 public class SingleAttributeValueReader extends BaseAttributeValueReader {
 37  
 
 38  
         private Object value;
 39  
         private AttributeDefinition definition;
 40  
         
 41  44
         public SingleAttributeValueReader(Object value, String entryName, String attributeName, AttributeDefinition definition) {
 42  44
                 this.value = value;
 43  44
                 this.entryName = entryName;
 44  44
                 this.attributeName = attributeName;
 45  44
                 this.definition = definition;
 46  44
         }
 47  
         
 48  
         @Override
 49  
         public Constrainable getDefinition(String attributeName) {
 50  
                 // Only return the definition if you have it, and if it's the definition for the passed attribute name
 51  91
                 return definition != null && definition.getName() != null && definition.getName().equals(attributeName) ? definition : null;
 52  
         }
 53  
         
 54  
         @Override
 55  
         public List<Constrainable> getDefinitions() {
 56  0
                 return null;
 57  
         }
 58  
         
 59  
         /**
 60  
          * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getEntry()
 61  
          */
 62  
         @Override
 63  
         public Constrainable getEntry() {
 64  0
                 return null;
 65  
         }
 66  
 
 67  
         @Override
 68  
         public String getLabel(String attributeName) {
 69  0
                 if (definition != null && definition.getName() != null && definition.getName().equals(attributeName))
 70  0
                         return definition.getLabel();
 71  
                 
 72  0
                 return attributeName;
 73  
         }
 74  
         
 75  
         @Override
 76  
         public Object getObject() {
 77  0
                 return null;
 78  
         }
 79  
         
 80  
         @Override
 81  
         public String getPath() {
 82  44
                 return attributeName;
 83  
         }
 84  
 
 85  
         @Override
 86  
         public Class<?> getType(String selectedAttributeName) {
 87  29
                 Constrainable attributeDefinition = getDefinition(selectedAttributeName);
 88  
                 
 89  29
                 if (attributeDefinition != null && attributeDefinition instanceof DataTypeConstraint) {
 90  29
                         DataTypeConstraint dataTypeConstraint = (DataTypeConstraint)attributeDefinition;
 91  29
                         if (dataTypeConstraint.getDataType() != null)
 92  0
                                 return dataTypeConstraint.getDataType().getType();
 93  
                 }
 94  
                 
 95  
                 // Assuming we can reliably guess
 96  29
                 return value != null ? value.getClass() : null;
 97  
         }
 98  
 
 99  
         @Override
 100  
         public <X> X getValue() throws AttributeValidationException {
 101  44
                 return (X) value;
 102  
         }
 103  
         
 104  
         @Override
 105  
         public <X> X getValue(String attributeName) throws AttributeValidationException {
 106  29
                 Constrainable attributeDefinition = getDefinition(attributeName);
 107  
                 
 108  29
                 if (attributeDefinition != null)
 109  29
                         return (X) value;
 110  
                 
 111  0
                 return null;
 112  
         }
 113  
 
 114  
 }