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.io.File; |
8 |
|
import java.io.FileNotFoundException; |
9 |
|
import java.io.FileOutputStream; |
10 |
|
import java.io.IOException; |
11 |
|
import java.io.OutputStream; |
12 |
|
import java.lang.reflect.ParameterizedType; |
13 |
|
import java.util.Date; |
14 |
|
import java.util.HashSet; |
15 |
|
import java.util.List; |
16 |
|
import java.util.Set; |
17 |
|
|
18 |
|
import org.kuali.student.common.dictionary.dto.FieldDefinition; |
19 |
|
import org.kuali.student.common.dto.Idable; |
20 |
|
import org.kuali.student.common.dto.MetaInfo; |
21 |
|
|
|
|
| 72.4% |
Uncovered Elements: 45 (163) |
Complexity: 51 |
Complexity Density: 0.47 |
|
22 |
|
public class DictionaryCreator |
23 |
|
{ |
24 |
|
|
25 |
|
private static final String OBJECT_STRUCTURE_CLASS = |
26 |
|
"objectStructureDefinition"; |
27 |
|
private static final String FIELD_DEFINITION_CLASS = |
28 |
|
FieldDefinition.class.getSimpleName (); |
29 |
|
private static final String ATTRIBUTES = "attributes"; |
30 |
|
private static final String NAME = "name"; |
31 |
|
private static final String HAS_META_DATA = "hasMetaData"; |
32 |
|
private static final String DATA_OBJECT_STRUCTURE = "dataObjectStructure"; |
33 |
|
private static final String BASE_INTEGER_REPEATING = "baseIntegerRepeating"; |
34 |
|
private static final String BASE_LONG_REPEATING = "baseLongRepeating"; |
35 |
|
private static final String BASE_DOUBLE_REPEATING = "baseDoubleRepeating"; |
36 |
|
private static final String BASE_FLOAT_REPEATING = "baseFloatRepeating"; |
37 |
|
private static final String BASE_BOOLEAN_REPEATING = "baseBooleanRepeating"; |
38 |
|
private static final String BASE_DATE_REPEATING = "baseDateRepeating"; |
39 |
|
private static final String BASE_STRING_REPEATING = "baseStringRepeating"; |
40 |
|
private static final String BASE_COMPLEX_REPEATING = "baseComplexRepeating"; |
41 |
|
private static final String BASE_INTEGER = "baseInteger"; |
42 |
|
private static final String BASE_LONG = "baseLong"; |
43 |
|
private static final String BASE_DOUBLE = "baseDouble"; |
44 |
|
private static final String BASE_FLOAT = "baseFloat"; |
45 |
|
private static final String BASE_BOOLEAN = "baseBoolean"; |
46 |
|
private static final String BASE_DATE = "baseDate"; |
47 |
|
private static final String BASE_STRING = "baseString"; |
48 |
|
private static final String BASE_COMPLEX = "baseComplex"; |
49 |
|
private static final String BASE_KUALI_ID = "baseKualiId"; |
50 |
|
private static final String BASE_KUALI_ORG_ID = "baseKualiOrgId"; |
51 |
|
private static final String BASE_KUALI_CLU_ID = "baseKualiCluId"; |
52 |
|
private static final String BASE_KUALI_PERSON_ID = "baseKualiPersonId"; |
53 |
|
private static final String BASE_KUALI_TYPE = "baseKualiType"; |
54 |
|
private static final String BASE_KUALI_STATE = "baseKualiState"; |
55 |
|
private static final String BASE_KUALI_EFFECTIVE_DATE = |
56 |
|
"baseKualiEffectiveDate"; |
57 |
|
private static final String BASE_KUALI_EXPIRATION_DATE = |
58 |
|
"baseKualiExpirationDate"; |
59 |
|
|
60 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 3 |
Complexity Density: 0.23 |
|
61 |
9
|
public void execute (Class<?> clazz, String outputFileName)... |
62 |
|
{ |
63 |
|
|
64 |
9
|
File file = new File (outputFileName); |
65 |
9
|
OutputStream os; |
66 |
9
|
try |
67 |
|
{ |
68 |
9
|
os = new FileOutputStream (file); |
69 |
|
} |
70 |
|
catch (FileNotFoundException ex) |
71 |
|
{ |
72 |
0
|
throw new IllegalArgumentException (ex); |
73 |
|
} |
74 |
9
|
StringBuffer s = new StringBuffer (); |
75 |
9
|
addSpringHeaderOpen (s); |
76 |
|
|
77 |
9
|
System.out.println (clazz.getName ()); |
78 |
9
|
addObjectStructure (clazz, s, new HashSet<Class<?>> ()); |
79 |
|
|
80 |
9
|
addSpringHeaderClose (s); |
81 |
9
|
try |
82 |
|
{ |
83 |
9
|
os.write (s.toString ().getBytes ()); |
84 |
|
} |
85 |
|
catch (IOException ex) |
86 |
|
{ |
87 |
0
|
throw new IllegalArgumentException (ex); |
88 |
|
} |
89 |
|
} |
90 |
|
|
|
|
| 91.4% |
Uncovered Elements: 3 (35) |
Complexity: 9 |
Complexity Density: 0.31 |
|
91 |
17
|
private void addObjectStructure (Class<?> clazz, StringBuffer s,... |
92 |
|
Set<Class<?>> processed) |
93 |
|
{ |
94 |
|
|
95 |
17
|
if (processed.contains (clazz)) |
96 |
|
{ |
97 |
0
|
return; |
98 |
|
} |
99 |
17
|
processed.add (clazz); |
100 |
|
|
101 |
|
|
102 |
17
|
s.append ("\n\n<!-- " + clazz.getSimpleName () + "-->"); |
103 |
17
|
s.append ("\n<bean id=\"" + clazz.getName () |
104 |
|
+ "-parent\" abstract=\"true\" parent=\"" + OBJECT_STRUCTURE_CLASS |
105 |
|
+ "\">"); |
106 |
17
|
addProperty (NAME, clazz.getName (), s); |
107 |
17
|
s.append ("\n<property name=\"" + ATTRIBUTES + "\">"); |
108 |
17
|
s.append ("\n<list>"); |
109 |
17
|
BeanInfo beanInfo; |
110 |
17
|
try |
111 |
|
{ |
112 |
17
|
beanInfo = Introspector.getBeanInfo (clazz); |
113 |
|
} |
114 |
|
catch (IntrospectionException ex) |
115 |
|
{ |
116 |
0
|
throw new IllegalArgumentException (ex); |
117 |
|
} |
118 |
17
|
boolean hasMetaData = false; |
119 |
17
|
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors ()) |
120 |
|
{ |
121 |
118
|
if ( ! MetaInfo.class.equals (pd.getPropertyType ()) && ! Class.class.equals ( |
122 |
|
pd.getPropertyType ()) && ! ATTRIBUTES.equals (pd.getName ())) |
123 |
|
{ |
124 |
87
|
String fieldName = clazz.getSimpleName ().substring (0, 1).toLowerCase () + clazz.getSimpleName ().substring ( |
125 |
|
1) + "." + pd.getName (); |
126 |
87
|
s.append ("\n<ref bean=\"" + fieldName + "\"/>"); |
127 |
|
} |
128 |
|
else |
129 |
|
{ |
130 |
31
|
hasMetaData = true; |
131 |
|
} |
132 |
|
} |
133 |
17
|
s.append ("\n</list>"); |
134 |
17
|
s.append ("\n</property>"); |
135 |
|
|
136 |
17
|
addProperty (HAS_META_DATA, String.valueOf (hasMetaData), s); |
137 |
17
|
s.append ("\n</bean>"); |
138 |
|
|
139 |
|
|
140 |
17
|
s.append ("\n<bean id=\"" + clazz.getName () + "\" parent=\"" |
141 |
|
+ clazz.getName () + "-parent\"/>"); |
142 |
|
|
143 |
17
|
Set<Class<?>> dependantStructures = new HashSet<Class<?>> (); |
144 |
17
|
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors ()) |
145 |
|
{ |
146 |
118
|
if ( ! MetaInfo.class.equals (pd.getPropertyType ()) && ! Class.class.equals ( |
147 |
|
pd.getPropertyType ()) && ! ATTRIBUTES.equals (pd.getName ())) |
148 |
|
{ |
149 |
87
|
dependantStructures.addAll (addField (clazz, pd, s, processed)); |
150 |
|
} |
151 |
|
} |
152 |
|
|
153 |
17
|
for (Class<?> dependantClass : dependantStructures) |
154 |
|
{ |
155 |
8
|
addObjectStructure (dependantClass, s, processed); |
156 |
|
} |
157 |
|
|
158 |
|
} |
159 |
|
|
|
|
| 61.2% |
Uncovered Elements: 40 (103) |
Complexity: 35 |
Complexity Density: 0.57 |
|
160 |
87
|
private Set<Class<?>> addField (Class<?> clazz, PropertyDescriptor pd,... |
161 |
|
StringBuffer s, Set<Class<?>> processed) |
162 |
|
{ |
163 |
87
|
Set<Class<?>> dependantStructures = new HashSet<Class<?>> (); |
164 |
|
|
165 |
|
|
166 |
87
|
String fieldName = clazz.getSimpleName ().substring (0, 1).toLowerCase () + clazz.getSimpleName ().substring ( |
167 |
|
1) + "." + pd.getName (); |
168 |
87
|
Class<?> pt = pd.getPropertyType (); |
169 |
87
|
String parentField = FIELD_DEFINITION_CLASS; |
170 |
87
|
boolean isComplex = false; |
171 |
87
|
if (clazz.isAssignableFrom (Idable.class) && "id".equals (pd.getName ())) |
172 |
|
{ |
173 |
0
|
parentField = BASE_KUALI_ID; |
174 |
|
} |
175 |
87
|
else if (pd.getName ().endsWith ("orgId")) |
176 |
|
{ |
177 |
0
|
parentField = BASE_KUALI_ORG_ID; |
178 |
|
} |
179 |
87
|
else if (pd.getName ().endsWith ("personId")) |
180 |
|
{ |
181 |
0
|
parentField = BASE_KUALI_PERSON_ID; |
182 |
|
} |
183 |
87
|
else if (pd.getName ().endsWith ("cluId")) |
184 |
|
{ |
185 |
0
|
parentField = BASE_KUALI_CLU_ID; |
186 |
|
} |
187 |
87
|
else if (List.class.equals (pt)) |
188 |
|
{ |
189 |
1
|
try |
190 |
|
{ |
191 |
1
|
pt = |
192 |
|
(Class<?>) ((ParameterizedType) clazz.getDeclaredField (pd.getName ()).getGenericType ()).getActualTypeArguments ()[0]; |
193 |
|
} |
194 |
|
catch (NoSuchFieldException ex) |
195 |
|
{ |
196 |
0
|
throw new IllegalArgumentException (ex); |
197 |
|
} |
198 |
|
catch (SecurityException ex) |
199 |
|
{ |
200 |
0
|
throw new IllegalArgumentException (ex); |
201 |
|
} |
202 |
1
|
if (int.class.equals (pt) || Integer.class.equals (pt)) |
203 |
|
{ |
204 |
0
|
parentField = BASE_INTEGER_REPEATING; |
205 |
|
} |
206 |
1
|
else if (long.class.equals (pt) || Long.class.equals (pt)) |
207 |
|
{ |
208 |
0
|
parentField = BASE_LONG_REPEATING; |
209 |
|
} |
210 |
1
|
else if (double.class.equals (pt) || Double.class.equals (pt)) |
211 |
|
{ |
212 |
0
|
parentField = BASE_DOUBLE_REPEATING; |
213 |
|
} |
214 |
1
|
else if (float.class.equals (pt) || Float.class.equals (pt)) |
215 |
|
{ |
216 |
0
|
parentField = BASE_FLOAT_REPEATING; |
217 |
|
} |
218 |
1
|
else if (boolean.class.equals (pt) || Boolean.class.equals (pt)) |
219 |
|
{ |
220 |
0
|
parentField = BASE_BOOLEAN_REPEATING; |
221 |
|
} |
222 |
1
|
else if (Date.class.equals (pt)) |
223 |
|
{ |
224 |
0
|
parentField = BASE_DATE_REPEATING; |
225 |
|
} |
226 |
1
|
else if (String.class.equals (pt)) |
227 |
|
{ |
228 |
1
|
parentField = BASE_STRING_REPEATING; |
229 |
|
} |
230 |
0
|
else if (List.class.equals (pt)) |
231 |
|
{ |
232 |
0
|
throw new RuntimeException ("Can't have a list of lists, List<List<?>> for property: " |
233 |
|
+ fieldName); |
234 |
|
} |
235 |
|
else |
236 |
|
{ |
237 |
0
|
parentField = BASE_COMPLEX_REPEATING; |
238 |
0
|
isComplex = true; |
239 |
0
|
dependantStructures.add (pt); |
240 |
|
} |
241 |
|
} |
242 |
|
else |
243 |
|
{ |
244 |
86
|
if (int.class.equals (pt) || Integer.class.equals (pt)) |
245 |
|
{ |
246 |
0
|
parentField = BASE_INTEGER; |
247 |
|
} |
248 |
86
|
else if (long.class.equals (pt) || Long.class.equals (pt)) |
249 |
|
{ |
250 |
0
|
parentField = BASE_LONG; |
251 |
|
} |
252 |
86
|
else if (double.class.equals (pt) || Double.class.equals (pt)) |
253 |
|
{ |
254 |
0
|
parentField = BASE_DOUBLE; |
255 |
|
} |
256 |
86
|
else if (float.class.equals (pt) || Float.class.equals (pt)) |
257 |
|
{ |
258 |
0
|
parentField = BASE_FLOAT; |
259 |
|
} |
260 |
86
|
else if (boolean.class.equals (pt) || Boolean.class.equals (pt)) |
261 |
|
{ |
262 |
0
|
parentField = BASE_BOOLEAN; |
263 |
|
} |
264 |
86
|
else if (Date.class.equals (pt)) |
265 |
|
{ |
266 |
18
|
parentField = BASE_DATE; |
267 |
|
} |
268 |
68
|
else if (String.class.equals (pt)) |
269 |
|
{ |
270 |
60
|
parentField = BASE_STRING; |
271 |
|
} |
272 |
|
else |
273 |
|
{ |
274 |
8
|
parentField = BASE_COMPLEX; |
275 |
8
|
isComplex = true; |
276 |
8
|
dependantStructures.add (pt); |
277 |
|
} |
278 |
|
} |
279 |
|
|
280 |
|
|
281 |
87
|
s.append ("\n\n<bean id=\"" + fieldName |
282 |
|
+ "-parent\" abstract=\"true\" parent=\"" + parentField + "\">"); |
283 |
87
|
addProperty (NAME, pd.getName (), s); |
284 |
87
|
if (isComplex) |
285 |
|
{ |
286 |
8
|
addPropertyRef (DATA_OBJECT_STRUCTURE, pt.getName (), s); |
287 |
|
} |
288 |
|
|
289 |
87
|
s.append ("\n</bean>"); |
290 |
|
|
291 |
|
|
292 |
87
|
s.append ("\n<bean id=\"" + fieldName + "\" parent=\"" + fieldName |
293 |
|
+ "-parent\"/>"); |
294 |
|
|
295 |
87
|
return dependantStructures; |
296 |
|
} |
297 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
298 |
121
|
private void addProperty (String propertyName, String propertyValue,... |
299 |
|
StringBuffer s) |
300 |
|
{ |
301 |
121
|
s.append ("\n<property name=\"" + propertyName + "\" value=\"" + propertyValue |
302 |
|
+ "\"/>"); |
303 |
|
} |
304 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
305 |
8
|
private static void addPropertyRef (String propertyName, String propertyValue,... |
306 |
|
StringBuffer s) |
307 |
|
{ |
308 |
8
|
s.append ("\n<property name=\"" + propertyName + "\" ref=\"" + propertyValue |
309 |
|
+ "\"/>"); |
310 |
|
} |
311 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
312 |
9
|
private void addSpringHeaderClose (StringBuffer s)... |
313 |
|
{ |
314 |
9
|
s.append ("\n</beans>"); |
315 |
|
} |
316 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
317 |
9
|
public void addSpringHeaderOpen (StringBuffer s)... |
318 |
|
{ |
319 |
9
|
s.append ("<beans xmlns=\"http://www.springframework.org/schema/beans\"" |
320 |
|
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" |
321 |
|
+ "xsi:schemaLocation=\"" |
322 |
|
+ "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" |
323 |
|
+ "\">"); |
324 |
9
|
s.append ("\n<import resource=\"classpath:ks-base-dictionary-context.xml\"/>"); |
325 |
|
} |
326 |
|
} |