Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Bean2DictionaryConverter |
|
| 9.4;9.4 |
1 | package org.kuali.student.common.dictionary.service.impl; | |
2 | ||
3 | import java.beans.BeanInfo; | |
4 | import java.beans.IntrospectionException; | |
5 | import java.beans.Introspector; | |
6 | import java.beans.PropertyDescriptor; | |
7 | import java.lang.reflect.ParameterizedType; | |
8 | import java.util.Date; | |
9 | import java.util.List; | |
10 | ||
11 | import org.kuali.student.common.dictionary.dto.DataType; | |
12 | import org.kuali.student.common.dictionary.dto.FieldDefinition; | |
13 | import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition; | |
14 | import org.kuali.student.common.dto.MetaInfo; | |
15 | ||
16 | ||
17 | public class Bean2DictionaryConverter | |
18 | { | |
19 | ||
20 | private Class<?> clazz; | |
21 | ||
22 | public Bean2DictionaryConverter (Class<?> clazz) | |
23 | 0 | { |
24 | 0 | this.clazz = clazz; |
25 | 0 | } |
26 | ||
27 | public ObjectStructureDefinition convert () | |
28 | { | |
29 | 0 | ObjectStructureDefinition os = new ObjectStructureDefinition (); |
30 | 0 | os.setName (clazz.getName ()); |
31 | BeanInfo beanInfo; | |
32 | try | |
33 | { | |
34 | 0 | beanInfo = Introspector.getBeanInfo (clazz); |
35 | } | |
36 | 0 | catch (IntrospectionException ex) |
37 | { | |
38 | 0 | throw new RuntimeException (ex); |
39 | 0 | } |
40 | 0 | os.setHasMetaData (calcHasMetaData (beanInfo)); |
41 | 0 | for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors ()) |
42 | { | |
43 | 0 | if ( ! MetaInfo.class.equals (pd.getPropertyType ()) |
44 | && ! Class.class.equals (pd.getPropertyType ()) | |
45 | && ! DictionaryConstants.ATTRIBUTES.equals (pd.getName ())) | |
46 | { | |
47 | 0 | os.getAttributes ().add (calcField (clazz, pd)); |
48 | } | |
49 | } | |
50 | 0 | return os; |
51 | } | |
52 | ||
53 | private boolean calcHasMetaData (BeanInfo beanInfo) | |
54 | { | |
55 | 0 | for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors ()) |
56 | { | |
57 | 0 | if (MetaInfo.class.equals (pd.getPropertyType ())) |
58 | { | |
59 | 0 | return true; |
60 | } | |
61 | // && ! DictionaryConstants.ATTRIBUTES.equals (pd.getName ())) | |
62 | } | |
63 | 0 | return false; |
64 | } | |
65 | ||
66 | private FieldDefinition calcField (Class<?> clazz, PropertyDescriptor pd) | |
67 | { | |
68 | 0 | FieldDefinition fd = new FieldDefinition (); |
69 | 0 | fd.setName (pd.getName ()); |
70 | 0 | Class<?> pt = pd.getPropertyType (); |
71 | 0 | if (List.class.equals (pt)) |
72 | { | |
73 | 0 | fd.setMaxOccurs (fd.UNBOUNDED); |
74 | try | |
75 | { | |
76 | 0 | pt = |
77 | (Class<?>) ((ParameterizedType) clazz.getDeclaredField (pd.getName ()).getGenericType ()).getActualTypeArguments ()[0]; | |
78 | } | |
79 | 0 | catch (NoSuchFieldException ex) |
80 | { | |
81 | 0 | throw new RuntimeException (ex); |
82 | } | |
83 | 0 | catch (SecurityException ex) |
84 | { | |
85 | 0 | throw new RuntimeException (ex); |
86 | 0 | } |
87 | } | |
88 | else | |
89 | { | |
90 | 0 | fd.setMaxOccurs (fd.SINGLE); |
91 | } | |
92 | 0 | fd.setDataType (calcDataType (pt)); |
93 | 0 | return fd; |
94 | } | |
95 | ||
96 | private DataType calcDataType (Class<?> pt) | |
97 | { | |
98 | 0 | if (int.class.equals (pt) || Integer.class.equals (pt)) |
99 | { | |
100 | 0 | return DataType.INTEGER; |
101 | } | |
102 | 0 | else if (long.class.equals (pt) || Long.class.equals (pt)) |
103 | { | |
104 | 0 | return DataType.LONG; |
105 | } | |
106 | 0 | else if (double.class.equals (pt) || Double.class.equals (pt)) |
107 | { | |
108 | 0 | return DataType.DOUBLE; |
109 | } | |
110 | 0 | else if (float.class.equals (pt) || Float.class.equals (pt)) |
111 | { | |
112 | 0 | return DataType.FLOAT; |
113 | } | |
114 | 0 | else if (boolean.class.equals (pt) || Boolean.class.equals (pt)) |
115 | { | |
116 | 0 | return DataType.BOOLEAN; |
117 | } | |
118 | 0 | else if (Date.class.equals (pt)) |
119 | { | |
120 | 0 | return DataType.DATE; |
121 | } | |
122 | 0 | else if (String.class.equals (pt)) |
123 | { | |
124 | 0 | return DataType.STRING; |
125 | } | |
126 | 0 | else if (List.class.equals (pt)) |
127 | { | |
128 | 0 | throw new RuntimeException ("Can't have a list of lists, List<List<?>>"); |
129 | } | |
130 | 0 | else if (Enum.class.isAssignableFrom (pt)) |
131 | { | |
132 | 0 | return DataType.STRING; |
133 | } | |
134 | 0 | else if (Object.class.equals (pt)) |
135 | { | |
136 | 0 | return DataType.STRING; |
137 | } | |
138 | 0 | else if (pt.getName ().startsWith ("org.kuali.student.")) |
139 | { | |
140 | 0 | return DataType.COMPLEX; |
141 | } | |
142 | else | |
143 | { | |
144 | 0 | throw new RuntimeException ("unknown/unhandled type of object in bean " + pt.getName ()); |
145 | } | |
146 | } | |
147 | } | |
148 |