View Javadoc

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