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