Coverage Report - org.kuali.rice.kns.datadictionary.validation.DictionaryObjectAttributeValueReader
 
Classes in this File Line Coverage Branch Coverage Complexity
DictionaryObjectAttributeValueReader
0%
0/40
0%
0/16
2.182
 
 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.beans.PropertyDescriptor;
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.kuali.rice.kns.datadictionary.AttributeDefinition;
 23  
 import org.kuali.rice.kns.datadictionary.DataDictionaryEntry;
 24  
 import org.kuali.rice.kns.datadictionary.DataDictionaryEntryBase;
 25  
 import org.kuali.rice.kns.datadictionary.exception.AttributeValidationException;
 26  
 import org.kuali.rice.kns.datadictionary.validation.capability.Constrainable;
 27  
 import org.springframework.beans.BeanWrapper;
 28  
 import org.springframework.beans.BeanWrapperImpl;
 29  
 import org.springframework.beans.NullValueInNestedPathException;
 30  
 
 31  
 /**
 32  
  * This class allows a dictionary object to expose information about its fields / attributes, including the values of
 33  
  * those fields, with some guidance from the DataDictionaryEntry object. 
 34  
  * 
 35  
  * @author Kuali Rice Team (rice.collab@kuali.org) 
 36  
  */
 37  
 public class DictionaryObjectAttributeValueReader extends BaseAttributeValueReader {
 38  
 
 39  
         protected Object object;
 40  
         protected DataDictionaryEntry entry;
 41  
 
 42  
         protected BeanWrapper beanWrapper;
 43  
         
 44  
         private String attributePath;
 45  
         
 46  0
         public DictionaryObjectAttributeValueReader(Object object, String entryName, DataDictionaryEntry entry) {
 47  0
                 this.object = object;
 48  0
                 this.entry = entry;
 49  0
                 this.entryName = entryName;
 50  
 
 51  0
                 if (object != null){
 52  0
                         beanWrapper = new BeanWrapperImpl(object);
 53  
                 }                
 54  0
         }
 55  
         
 56  
         public DictionaryObjectAttributeValueReader(Object object, String entryName, DataDictionaryEntry entry, String attributePath) {
 57  0
                 this(object, entryName, entry);
 58  0
                 this.attributePath = attributePath;
 59  0
         }
 60  
         
 61  
         @Override
 62  
         public Constrainable getDefinition(String attrName) {
 63  0
                 return entry != null ? entry.getAttributeDefinition(attrName) : null;
 64  
         }
 65  
         
 66  
         @Override
 67  
         public List<Constrainable> getDefinitions() {
 68  0
                 if (entry instanceof DataDictionaryEntryBase) {
 69  0
                         DataDictionaryEntryBase entryBase = (DataDictionaryEntryBase)entry;
 70  0
                         List<Constrainable> definitions = new ArrayList<Constrainable>();
 71  0
                         List<AttributeDefinition> attributeDefinitions = entryBase.getAttributes();
 72  0
                         definitions.addAll(attributeDefinitions);
 73  0
                         return definitions;
 74  
                 }
 75  
                 
 76  0
                 return null;
 77  
         }
 78  
         
 79  
         @Override
 80  
         public Constrainable getEntry() {
 81  0
                 if (entry instanceof Constrainable)
 82  0
                         return (Constrainable) entry;
 83  
                         
 84  0
                 return null;
 85  
         }
 86  
         
 87  
         @Override
 88  
         public String getLabel(String attrName) {
 89  0
                 AttributeDefinition attributeDefinition = entry != null ? entry.getAttributeDefinition(attrName) : null;
 90  0
                 return attributeDefinition != null ? attributeDefinition.getLabel()  : attrName;
 91  
         }
 92  
 
 93  
         @Override
 94  
         public Object getObject() {
 95  0
                 return this.object;
 96  
         }
 97  
         
 98  
         @Override
 99  
         public String getPath() {
 100  0
                 String path = ValidationUtils.buildPath(attributePath, attributeName);
 101  0
                 return path != null ? path : "";
 102  
         }
 103  
 
 104  
         @Override
 105  
         public Class<?> getType(String attrName) {
 106  0
                 PropertyDescriptor propertyDescriptor = beanWrapper.getPropertyDescriptor(attrName);
 107  
                 
 108  0
                 return propertyDescriptor.getPropertyType();
 109  
         }
 110  
         
 111  
         @SuppressWarnings("unchecked")
 112  
         @Override
 113  
         public <X> X getValue() throws AttributeValidationException {
 114  0
                 Object value = getValue(attributeName);
 115  0
                 return (X) value;
 116  
         }
 117  
         
 118  
         @SuppressWarnings("unchecked")
 119  
         @Override
 120  
         public <X> X getValue(String attrName) throws AttributeValidationException {
 121  0
                 X attributeValue = null;
 122  
                 
 123  0
                 Exception e = null;
 124  
                 try {
 125  0
                         attributeValue = (X) beanWrapper.getPropertyValue(attrName);
 126  
 
 127  0
                 } catch (IllegalArgumentException iae) {
 128  0
                         e = iae;
 129  0
                 } catch (NullValueInNestedPathException nvinp){
 130  
                         //just return null
 131  0
                 }
 132  
                 
 133  0
                 if (e != null)
 134  0
                         throw new AttributeValidationException("Unable to lookup attribute value by name (" + attrName + ") using introspection", e);
 135  
                 
 136  
                 
 137  
                 //                        JLR : KS has code to handle dynamic attributes -- not sure whether this is really needed anymore if we're actually relying on types
 138  
                 //            // Extract dynamic attributes
 139  
                 //            if(DYNAMIC_ATTRIBUTE.equals(propName)) {
 140  
                 //                dataMap.putAll((Map<String, String>)value);
 141  
                 //            } else {
 142  
                 //                                dataMap.put(propName, value);
 143  
                 //            }
 144  
                 
 145  0
                 return attributeValue;
 146  
         }
 147  
 }