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