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