View Javadoc

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