1 package org.kuali.student.r1.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.Field;
8 import java.lang.reflect.ParameterizedType;
9 import java.util.ArrayList;
10 import java.util.Date;
11 import java.util.List;
12
13 import org.kuali.student.common.spring.AnnotationUtils;
14 import org.kuali.student.r1.common.dictionary.dto.DataType;
15 import org.kuali.student.r1.common.dictionary.dto.FieldDefinition;
16 import org.kuali.student.r1.common.dictionary.dto.ObjectStructureDefinition;
17 import org.kuali.student.r2.common.dto.MetaInfo;
18 import org.kuali.student.r2.common.infc.Meta;
19
20
21 @Deprecated
22 public class Bean2DictionaryConverter
23 {
24
25 private Class<?> clazz;
26
27 public Bean2DictionaryConverter (Class<?> clazz)
28 {
29 this.clazz = clazz;
30 }
31
32 public ObjectStructureDefinition convert ()
33 {
34 ObjectStructureDefinition os = new ObjectStructureDefinition ();
35 os.setName (clazz.getName ());
36 BeanInfo beanInfo;
37 try
38 {
39 beanInfo = Introspector.getBeanInfo (clazz);
40 }
41 catch (IntrospectionException ex)
42 {
43 throw new RuntimeException (ex);
44 }
45 os.setHasMetaData (calcHasMetaData (beanInfo));
46 for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors ())
47 {
48 boolean skipOver = false;
49
50
51 if (pd.getReadMethod() != null && AnnotationUtils.doesMethodHierarchyContainAnnotation(pd.getReadMethod(), Deprecated.class))
52 skipOver = true;
53
54 if (MetaInfo.class.equals (pd.getPropertyType ()) || Class.class.equals (pd.getPropertyType ()) || DictionaryConstants.ATTRIBUTES.equals (pd.getName ()))
55 skipOver = true;
56
57 if (!skipOver)
58 {
59 os.getAttributes ().add (calcField (clazz, pd));
60 }
61 }
62 return os;
63 }
64
65
66 private boolean calcHasMetaData (BeanInfo beanInfo)
67 {
68 for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors ())
69 {
70
71
72 if (Meta.class.isAssignableFrom(pd.getPropertyType()))
73 {
74 return true;
75 }
76
77 }
78 return false;
79 }
80
81 private FieldDefinition calcField (Class<?> clazz, PropertyDescriptor pd)
82 {
83 FieldDefinition fd = new FieldDefinition ();
84 String propDescriptorName = pd.getName ();
85 fd.setName (propDescriptorName);
86 Class<?> pt = pd.getPropertyType ();
87 if (List.class.equals (pt))
88 {
89 fd.setMaxOccurs (fd.UNBOUNDED);
90 try
91 {
92 ArrayList<Field> allFieldsOnClass = new ArrayList<Field>();
93
94 allFieldsOnClass = getAllFields(allFieldsOnClass, clazz);
95 Field declaredFields = findField(propDescriptorName, allFieldsOnClass);
96 ParameterizedType propGenericType = (ParameterizedType) declaredFields.getGenericType ();
97 pt =
98 (Class<?>) propGenericType.getActualTypeArguments ()[0];
99 }
100 catch (SecurityException ex)
101 {
102 throw new RuntimeException (ex);
103 }
104 }
105 else
106 {
107 fd.setMaxOccurs (fd.SINGLE);
108 }
109 fd.setDataType (calcDataType (pt));
110 return fd;
111 }
112
113 private DataType calcDataType (Class<?> pt)
114 {
115 if (int.class.equals (pt) || Integer.class.equals (pt))
116 {
117 return DataType.INTEGER;
118 }
119 else if (long.class.equals (pt) || Long.class.equals (pt))
120 {
121 return DataType.LONG;
122 }
123 else if (double.class.equals (pt) || Double.class.equals (pt))
124 {
125 return DataType.DOUBLE;
126 }
127 else if (float.class.equals (pt) || Float.class.equals (pt))
128 {
129 return DataType.FLOAT;
130 }
131 else if (boolean.class.equals (pt) || Boolean.class.equals (pt))
132 {
133 return DataType.BOOLEAN;
134 }
135 else if (Date.class.equals (pt))
136 {
137 return DataType.DATE;
138 }
139 else if (String.class.equals (pt))
140 {
141 return DataType.STRING;
142 }
143 else if (List.class.equals (pt))
144 {
145 throw new RuntimeException ("Can't have a list of lists, List<List<?>>");
146 }
147 else if (Enum.class.isAssignableFrom (pt))
148 {
149 return DataType.STRING;
150 }
151 else if (Object.class.equals (pt))
152 {
153 return DataType.STRING;
154 }
155 else if (pt.getName ().startsWith ("org.kuali.student."))
156 {
157 return DataType.COMPLEX;
158 }
159 else
160 {
161 throw new RuntimeException ("unknown/unhandled type of object in bean " + pt.getName ());
162 }
163 }
164
165 public ArrayList<Field> getAllFields(ArrayList<Field> fields, Class<?> type) {
166 for (Field field: type.getDeclaredFields()) {
167 fields.add(field);
168 }
169
170 if (type.getSuperclass() != null) {
171 fields = getAllFields(fields, type.getSuperclass());
172 }
173
174 return fields;
175 }
176
177 public Field findField(String fieldName, ArrayList<Field> fields) {
178 for (Field field : fields) {
179 if (field.getName().equals(fieldName)) {
180 return field;
181 }
182 }
183 return null;
184 }
185 }
186