Coverage Report - org.kuali.rice.kns.datadictionary.validation.SingleAttributeValueReader
 
Classes in this File Line Coverage Branch Coverage Complexity
SingleAttributeValueReader
0%
0/25
0%
0/22
2.4
 
 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.kns.datadictionary.validation;
 17  
 
 18  
 import java.util.List;
 19  
 
 20  
 import org.kuali.rice.kns.datadictionary.AttributeDefinition;
 21  
 import org.kuali.rice.kns.datadictionary.exception.AttributeValidationException;
 22  
 import org.kuali.rice.kns.datadictionary.validation.capability.Constrainable;
 23  
 import org.kuali.rice.kns.datadictionary.validation.constraint.DataTypeConstraint;
 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  0
         public SingleAttributeValueReader(Object value, String entryName, String attributeName, AttributeDefinition definition) {
 42  0
                 this.value = value;
 43  0
                 this.entryName = entryName;
 44  0
                 this.attributeName = attributeName;
 45  0
                 this.definition = definition;
 46  0
         }
 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  0
                 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.kns.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  0
                 return attributeName;
 83  
         }
 84  
 
 85  
         @Override
 86  
         public Class<?> getType(String selectedAttributeName) {
 87  0
                 Constrainable attributeDefinition = getDefinition(selectedAttributeName);
 88  
                 
 89  0
                 if (attributeDefinition != null && attributeDefinition instanceof DataTypeConstraint) {
 90  0
                         DataTypeConstraint dataTypeConstraint = (DataTypeConstraint)attributeDefinition;
 91  0
                         if (dataTypeConstraint.getDataType() != null)
 92  0
                                 return dataTypeConstraint.getDataType().getType();
 93  
                 }
 94  
                 
 95  
                 // Assuming we can reliably guess
 96  0
                 return value != null ? value.getClass() : null;
 97  
         }
 98  
 
 99  
         @Override
 100  
         public <X> X getValue() throws AttributeValidationException {
 101  0
                 return (X) value;
 102  
         }
 103  
         
 104  
         @Override
 105  
         public <X> X getValue(String attributeName) throws AttributeValidationException {
 106  0
                 Constrainable attributeDefinition = getDefinition(attributeName);
 107  
                 
 108  0
                 if (attributeDefinition != null)
 109  0
                         return (X) value;
 110  
                 
 111  0
                 return null;
 112  
         }
 113  
 
 114  
 }