1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.kuali.student.enrollment.lpr.mock; |
16 | |
|
17 | |
import java.beans.BeanInfo; |
18 | |
import java.beans.IntrospectionException; |
19 | |
import java.beans.Introspector; |
20 | |
import java.beans.PropertyDescriptor; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
import org.apache.log4j.Logger; |
25 | |
import org.kuali.student.common.infc.AttributeInfc; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class DataProvider implements DataProviderInfc { |
32 | |
|
33 | 0 | final static Logger LOG = Logger.getLogger(DataProvider.class); |
34 | |
private static final String DYNAMIC_ATTRIBUTE = "attributes"; |
35 | 0 | Map<String, Object> dataMap = null; |
36 | |
|
37 | 0 | public DataProvider() { |
38 | 0 | } |
39 | |
|
40 | |
|
41 | |
@Override |
42 | |
public String getPath() { |
43 | 0 | return ""; |
44 | |
} |
45 | |
|
46 | |
@SuppressWarnings("unchecked") |
47 | |
@Override |
48 | |
public void initialize(Object o) { |
49 | |
|
50 | 0 | dataMap = new HashMap<String, Object>(); |
51 | |
|
52 | 0 | Map<String, PropertyDescriptor> beanInfo = getBeanInfo(o.getClass()); |
53 | |
|
54 | 0 | for (String propName : beanInfo.keySet()) { |
55 | |
|
56 | 0 | PropertyDescriptor pd = beanInfo.get(propName); |
57 | 0 | Object value = null; |
58 | |
try { |
59 | 0 | value = pd.getReadMethod().invoke(o); |
60 | 0 | } catch (Exception e) { |
61 | 0 | LOG.warn("Method invokation failed", e); |
62 | 0 | } |
63 | |
|
64 | |
|
65 | 0 | if (DYNAMIC_ATTRIBUTE.equals(propName)) { |
66 | 0 | for (AttributeInfc attr: (List<AttributeInfc>) value) |
67 | |
{ |
68 | 0 | dataMap.put(attr.getKey(), attr.getValue()); |
69 | |
} |
70 | |
} else { |
71 | 0 | dataMap.put(propName, value); |
72 | |
} |
73 | 0 | } |
74 | 0 | } |
75 | |
|
76 | |
@Override |
77 | |
public String getObjectId() { |
78 | 0 | return (dataMap.containsKey("id") && null != dataMap.get("id")) ? dataMap.get("id").toString() : null; |
79 | |
} |
80 | |
|
81 | |
@Override |
82 | |
public Object getValue(String fieldKey) { |
83 | 0 | return dataMap.get(fieldKey); |
84 | |
} |
85 | |
|
86 | |
@Override |
87 | |
public Boolean hasField(String fieldKey) { |
88 | 0 | return dataMap.containsKey(fieldKey); |
89 | |
} |
90 | |
|
91 | |
private Map<String, PropertyDescriptor> getBeanInfo(Class<?> clazz) { |
92 | 0 | Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>(); |
93 | 0 | BeanInfo beanInfo = null; |
94 | |
try { |
95 | 0 | beanInfo = Introspector.getBeanInfo(clazz); |
96 | 0 | } catch (IntrospectionException e) { |
97 | 0 | throw new RuntimeException(e); |
98 | 0 | } |
99 | 0 | PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); |
100 | 0 | for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { |
101 | 0 | properties.put(propertyDescriptor.getName(), propertyDescriptor); |
102 | |
} |
103 | 0 | return properties; |
104 | |
} |
105 | |
} |