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
54
55 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 super(object, entryName, entry);
62
63
64
65
66 this.attributeTypeMap = new HashMap<String, Class<?>>();
67 this.attributeValueMap = new HashMap<String, Object>();
68
69 this.attributeDefinitions = new LinkedList<Constrainable>();
70 this.attributeDefinitionMap = new HashMap<String, AttributeDefinition>();
71 for (MaintainableSectionDefinition sectionDefinition : entry.getMaintainableSections()) {
72 List<? extends MaintainableItemDefinition> itemDefinitions = sectionDefinition.getMaintainableItems();
73
74 for (MaintainableItemDefinition itemDefinition : itemDefinitions) {
75 if (itemDefinition instanceof MaintainableFieldDefinition) {
76 String itemDefinitionName = itemDefinition.getName();
77 AttributeDefinition attributeDefinition = KRADServiceLocatorWeb.getDataDictionaryService().getAttributeDefinition(object.getClass().getName(), itemDefinitionName);
78
79
80 boolean isAttributeDefined = attributeDefinition != null;
81
82 if (isAttributeDefined) {
83 attributeDefinitions.add(attributeDefinition);
84 attributeDefinitionMap.put(itemDefinitionName, attributeDefinition);
85 LOG.info("itemDefName: " + itemDefinitionName);
86 PropertyDescriptor propertyDescriptor = null;
87 try {
88 propertyDescriptor = PropertyUtils.getPropertyDescriptor(object, itemDefinitionName);
89 } catch (IllegalAccessException e) {
90 LOG.warn("Failed to find property description on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);
91 } catch (InvocationTargetException e) {
92 LOG.warn("Failed to find property description on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);
93 } catch (NoSuchMethodException e) {
94 LOG.warn("Failed to find property description on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);
95 }
96
97 Method readMethod = propertyDescriptor.getReadMethod();
98
99 try {
100 Object attributeValue = readMethod.invoke(object);
101
102 if (attributeValue != null && StringUtils.isNotBlank(attributeValue.toString())) {
103 Class<?> propertyType = ObjectUtils.getPropertyType(object, itemDefinitionName, persistenceStructureService);
104 attributeTypeMap.put(itemDefinitionName, propertyType);
105 if (TypeUtils.isStringClass(propertyType) || TypeUtils.isIntegralClass(propertyType) ||
106 TypeUtils.isDecimalClass(propertyType) ||
107 TypeUtils.isTemporalClass(propertyType) ||
108 TypeUtils.isBooleanClass(propertyType)) {
109
110 if (!TypeUtils.isTemporalClass(propertyType)) {
111 attributeValueMap.put(itemDefinitionName, attributeValue);
112 }
113 }
114 }
115 } catch (IllegalArgumentException e) {
116 LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);
117 } catch (IllegalAccessException e) {
118 LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);
119 } catch (InvocationTargetException e) {
120 LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);
121 }
122
123 }
124 }
125 }
126 }
127 }
128
129
130
131
132 @Override
133 public Constrainable getDefinition(String attributeName) {
134 return attributeDefinitionMap != null ? attributeDefinitionMap.get(attributeName) : null;
135 }
136
137
138
139
140 @Override
141 public List<Constrainable> getDefinitions() {
142 return attributeDefinitions;
143 }
144
145 @Override
146 public String getLabel(String attributeName) {
147 AttributeDefinition attributeDefinition = attributeDefinitionMap != null ? attributeDefinitionMap.get(attributeName) : null;
148 return attributeDefinition != null ? attributeDefinition.getLabel() : attributeName;
149 }
150
151
152
153
154 @Override
155 public Class<?> getType(String attributeName) {
156 return attributeTypeMap != null ? attributeTypeMap.get(attributeName) : null;
157 }
158
159
160
161
162 @Override
163 public <X> X getValue(String attributeName) throws AttributeValidationException {
164 return (X) attributeValueMap.get(attributeName);
165 }
166
167
168
169
170
171
172
173
174
175 }