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