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