| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.datadictionary.validation; |
| 17 | |
|
| 18 | |
import org.apache.commons.beanutils.PropertyUtils; |
| 19 | |
import org.apache.commons.lang.StringUtils; |
| 20 | |
import org.apache.log4j.Logger; |
| 21 | |
import org.kuali.rice.core.util.type.TypeUtils; |
| 22 | |
import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition; |
| 23 | |
import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition; |
| 24 | |
import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition; |
| 25 | |
import org.kuali.rice.krad.datadictionary.AttributeDefinition; |
| 26 | |
import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry; |
| 27 | |
import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException; |
| 28 | |
import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader; |
| 29 | |
import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable; |
| 30 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
| 31 | |
import org.kuali.rice.krad.service.PersistenceStructureService; |
| 32 | |
import org.kuali.rice.krad.util.ObjectUtils; |
| 33 | |
|
| 34 | |
import java.beans.PropertyDescriptor; |
| 35 | |
import java.lang.reflect.InvocationTargetException; |
| 36 | |
import java.lang.reflect.Method; |
| 37 | |
import java.util.HashMap; |
| 38 | |
import java.util.LinkedList; |
| 39 | |
import java.util.List; |
| 40 | |
import java.util.Map; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public class MaintenanceDocumentAttributeValueReader extends DictionaryObjectAttributeValueReader { |
| 48 | |
|
| 49 | |
protected Map<String, Class<?>> attributeTypeMap; |
| 50 | |
protected Map<String, Object> attributeValueMap; |
| 51 | |
protected Map<String, PropertyDescriptor> beanInfo; |
| 52 | |
|
| 53 | 0 | private final static Logger LOG = Logger.getLogger(MaintenanceDocumentAttributeValueReader.class); |
| 54 | |
|
| 55 | |
private List<Constrainable> attributeDefinitions; |
| 56 | |
private Map<String, AttributeDefinition> attributeDefinitionMap; |
| 57 | |
|
| 58 | |
public MaintenanceDocumentAttributeValueReader(Object object, String entryName, MaintenanceDocumentEntry entry, PersistenceStructureService persistenceStructureService) { |
| 59 | 0 | super(object, entryName, entry); |
| 60 | |
|
| 61 | 0 | if (object != null) |
| 62 | 0 | this.beanInfo = getBeanInfo(object.getClass()); |
| 63 | |
|
| 64 | 0 | this.attributeTypeMap = new HashMap<String, Class<?>>(); |
| 65 | 0 | this.attributeValueMap = new HashMap<String, Object>(); |
| 66 | |
|
| 67 | 0 | this.attributeDefinitions = new LinkedList<Constrainable>(); |
| 68 | 0 | this.attributeDefinitionMap = new HashMap<String, AttributeDefinition>(); |
| 69 | 0 | for (MaintainableSectionDefinition sectionDefinition : entry.getMaintainableSections()) { |
| 70 | 0 | List<? extends MaintainableItemDefinition> itemDefinitions = sectionDefinition.getMaintainableItems(); |
| 71 | |
|
| 72 | 0 | for (MaintainableItemDefinition itemDefinition : itemDefinitions) { |
| 73 | 0 | if (itemDefinition instanceof MaintainableFieldDefinition) { |
| 74 | 0 | String itemDefinitionName = itemDefinition.getName(); |
| 75 | 0 | AttributeDefinition attributeDefinition = KRADServiceLocatorWeb.getDataDictionaryService().getAttributeDefinition(object.getClass().getName(), itemDefinitionName); |
| 76 | |
|
| 77 | |
|
| 78 | 0 | boolean isAttributeDefined = attributeDefinition != null; |
| 79 | |
|
| 80 | 0 | if (isAttributeDefined) { |
| 81 | 0 | attributeDefinitions.add(attributeDefinition); |
| 82 | 0 | attributeDefinitionMap.put(itemDefinitionName, attributeDefinition); |
| 83 | 0 | PropertyDescriptor propertyDescriptor = beanInfo.get(itemDefinitionName); |
| 84 | 0 | Method readMethod = propertyDescriptor.getReadMethod(); |
| 85 | |
|
| 86 | |
try { |
| 87 | 0 | Object attributeValue = readMethod.invoke(object); |
| 88 | |
|
| 89 | 0 | if (attributeValue != null && StringUtils.isNotBlank(attributeValue.toString())) { |
| 90 | 0 | Class<?> propertyType = ObjectUtils.getPropertyType(object, itemDefinitionName, persistenceStructureService); |
| 91 | 0 | attributeTypeMap.put(itemDefinitionName, propertyType); |
| 92 | 0 | if (TypeUtils.isStringClass(propertyType) || TypeUtils.isIntegralClass(propertyType) || |
| 93 | |
TypeUtils.isDecimalClass(propertyType) || |
| 94 | |
TypeUtils.isTemporalClass(propertyType) || |
| 95 | |
TypeUtils.isBooleanClass(propertyType)) { |
| 96 | |
|
| 97 | 0 | if (!TypeUtils.isTemporalClass(propertyType)) { |
| 98 | 0 | attributeValueMap.put(itemDefinitionName, attributeValue); |
| 99 | |
} |
| 100 | |
} |
| 101 | |
} |
| 102 | 0 | } catch (IllegalArgumentException e) { |
| 103 | 0 | LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e); |
| 104 | 0 | } catch (IllegalAccessException e) { |
| 105 | 0 | LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e); |
| 106 | 0 | } catch (InvocationTargetException e) { |
| 107 | 0 | LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e); |
| 108 | 0 | } |
| 109 | |
|
| 110 | |
} |
| 111 | 0 | } |
| 112 | |
} |
| 113 | 0 | } |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
@Override |
| 120 | |
public Constrainable getDefinition(String attributeName) { |
| 121 | 0 | return attributeDefinitionMap != null ? attributeDefinitionMap.get(attributeName) : null; |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
@Override |
| 128 | |
public List<Constrainable> getDefinitions() { |
| 129 | 0 | return attributeDefinitions; |
| 130 | |
} |
| 131 | |
|
| 132 | |
@Override |
| 133 | |
public String getLabel(String attributeName) { |
| 134 | 0 | AttributeDefinition attributeDefinition = attributeDefinitionMap != null ? attributeDefinitionMap.get(attributeName) : null; |
| 135 | 0 | return attributeDefinition != null ? attributeDefinition.getLabel() : attributeName; |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
@Override |
| 142 | |
public Class<?> getType(String attributeName) { |
| 143 | 0 | return attributeTypeMap != null ? attributeTypeMap.get(attributeName) : null; |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
@Override |
| 150 | |
public <X> X getValue(String attributeName) throws AttributeValidationException { |
| 151 | 0 | return (X) attributeValueMap.get(attributeName); |
| 152 | |
} |
| 153 | |
|
| 154 | |
private Map<String, PropertyDescriptor> getBeanInfo(Class<?> clazz) { |
| 155 | 0 | final Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>(); |
| 156 | 0 | for (PropertyDescriptor propertyDescriptor : PropertyUtils.getPropertyDescriptors(clazz)) { |
| 157 | 0 | properties.put(propertyDescriptor.getName(), propertyDescriptor); |
| 158 | |
} |
| 159 | 0 | return properties; |
| 160 | |
} |
| 161 | |
|
| 162 | |
} |