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