| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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.DataDictionaryEntry; |
| 20 | |
import org.kuali.rice.krad.datadictionary.DataDictionaryEntryBase; |
| 21 | |
import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException; |
| 22 | |
import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable; |
| 23 | |
import org.springframework.beans.BeanWrapper; |
| 24 | |
import org.springframework.beans.BeanWrapperImpl; |
| 25 | |
import org.springframework.beans.NullValueInNestedPathException; |
| 26 | |
|
| 27 | |
import java.beans.PropertyDescriptor; |
| 28 | |
import java.util.ArrayList; |
| 29 | |
import java.util.List; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 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 | 66 | public DictionaryObjectAttributeValueReader(Object object, String entryName, DataDictionaryEntry entry) { |
| 47 | 66 | this.object = object; |
| 48 | 66 | this.entry = entry; |
| 49 | 66 | this.entryName = entryName; |
| 50 | |
|
| 51 | 66 | if (object != null){ |
| 52 | 66 | beanWrapper = new BeanWrapperImpl(object); |
| 53 | |
} |
| 54 | 66 | } |
| 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 | 36 | 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 | 9 | AttributeDefinition attributeDefinition = entry != null ? entry.getAttributeDefinition(attrName) : null; |
| 90 | 9 | 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 | 38 | String path = ValidationUtils.buildPath(attributePath, attributeName); |
| 101 | 38 | return path != null ? path : ""; |
| 102 | |
} |
| 103 | |
|
| 104 | |
@Override |
| 105 | |
public Class<?> getType(String attrName) { |
| 106 | 34 | PropertyDescriptor propertyDescriptor = beanWrapper.getPropertyDescriptor(attrName); |
| 107 | |
|
| 108 | 34 | return propertyDescriptor.getPropertyType(); |
| 109 | |
} |
| 110 | |
|
| 111 | |
@SuppressWarnings("unchecked") |
| 112 | |
@Override |
| 113 | |
public <X> X getValue() throws AttributeValidationException { |
| 114 | 63 | Object value = getValue(attributeName); |
| 115 | 63 | return (X) value; |
| 116 | |
} |
| 117 | |
|
| 118 | |
@SuppressWarnings("unchecked") |
| 119 | |
@Override |
| 120 | |
public <X> X getValue(String attrName) throws AttributeValidationException { |
| 121 | 113 | X attributeValue = null; |
| 122 | |
|
| 123 | 113 | Exception e = null; |
| 124 | |
try { |
| 125 | 113 | attributeValue = (X) beanWrapper.getPropertyValue(attrName); |
| 126 | |
|
| 127 | 0 | } catch (IllegalArgumentException iae) { |
| 128 | 0 | e = iae; |
| 129 | 0 | } catch (NullValueInNestedPathException nvinp){ |
| 130 | |
|
| 131 | 113 | } |
| 132 | |
|
| 133 | 113 | if (e != null) |
| 134 | 0 | throw new AttributeValidationException("Unable to lookup attribute value by name (" + attrName + ") using introspection", e); |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | 113 | return attributeValue; |
| 146 | |
} |
| 147 | |
} |