View Javadoc

1   package com.panogenesis.util;
2   
3   import java.beans.PropertyDescriptor;
4   import java.util.Enumeration;
5   import java.util.HashMap;
6   import java.util.List;
7   import java.util.Map;
8   import java.util.Properties;
9   import java.util.ResourceBundle;
10  
11  import org.apache.commons.beanutils.BeanUtils;
12  import org.apache.commons.beanutils.PropertyUtils;
13  import org.apache.commons.lang.StringUtils;
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import com.panogenesis.model.BaseObject;
17  
18  
19  /***
20   * Utility class to convert one object to another.
21   * 
22   * <p>
23   * <a href="ConvertUtil.java.html"><i>View Source</i></a>
24   * </p>
25   * 
26   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
27   */
28  public final class ConvertUtil {
29      //~ Static fields/initializers =============================================
30  
31      private static Log log = LogFactory.getLog(ConvertUtil.class);
32  
33      //~ Methods ================================================================
34  
35      /***
36       * Method to convert a ResourceBundle to a Map object.
37       * @param rb a given resource bundle
38       * @return Map a populated map
39       */
40      public static Map convertBundleToMap(ResourceBundle rb) {
41          Map map = new HashMap();
42  
43          for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
44              String key = (String) keys.nextElement();
45              map.put(key, rb.getString(key));
46          }
47  
48          return map;
49      }
50  
51      /***
52       * Method to convert a ResourceBundle to a Properties object.
53       * @param rb a given resource bundle
54       * @return Properties a populated properties object
55       */
56      public static Properties convertBundleToProperties(ResourceBundle rb) {
57          Properties props = new Properties();
58  
59          for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
60              String key = (String) keys.nextElement();
61              props.put(key, rb.getString(key));
62          }
63  
64          return props;
65      }
66  
67      /***
68       * Convenience method used by tests to populate an object from a
69       * ResourceBundle
70       * @param obj an initialized object
71       * @param rb a resource bundle
72       * @return a populated object
73       */
74      public static Object populateObject(Object obj, ResourceBundle rb) {
75          try {
76              Map map = convertBundleToMap(rb);
77  
78              BeanUtils.copyProperties(obj, map);
79          } catch (Exception e) {
80              e.printStackTrace();
81              log.error("Exception occured populating object: " + e.getMessage());
82          }
83  
84          return obj;
85      }
86  
87      /***
88       * This method inspects a POJO or Form and figures out its pojo/form
89       * equivalent.
90       *
91       * @param o the object to inspect
92       * @return the Class of the persistable object
93       * @throws ClassNotFoundException
94       * @throws InstantiationException
95       * @throws IllegalAccessException
96       */
97      public static Object getOpposingObject(Object o) throws ClassNotFoundException,
98                                                  InstantiationException,
99                                                  IllegalAccessException {
100         String name = o.getClass().getName();
101         if (o instanceof BaseObject) {
102             if (log.isDebugEnabled()) {
103                 log.debug("getting form equivalent of pojo...");
104             }
105 
106             name = StringUtils.replace(name, "model", "webapp.form");
107             name += "Form";
108         } else {
109             if (log.isDebugEnabled()) {
110                 log.debug("getting pojo equivalent of form...");
111             }
112             name = StringUtils.replace(name, "webapp.form", "model");
113             name = name.substring(0, name.lastIndexOf("Form"));
114         }
115 
116         Class obj = Class.forName(name);
117 
118         if (log.isDebugEnabled()) {
119             log.debug("returning className: " + obj.getName());
120         }
121 
122         return obj.newInstance();
123     }
124 
125     /***
126      * Convenience method to convert a form to a POJO and back again
127      *
128      * @param o the object to tranfer properties from
129      * @return converted object
130      */
131     public static Object convert(Object o) throws Exception {
132         if (o == null) {
133         	return null;
134         }
135         Object target = getOpposingObject(o);
136         BeanUtils.copyProperties(target, o);
137         return target;
138     }
139 
140     /***
141      * Convenience method to convert Lists (in a Form) from POJOs to Forms.
142      * Also checks for and formats dates.
143      *
144      * @param o
145      * @return
146      * @throws Exception
147      */
148     public static Object convertLists(Object o) throws Exception {
149         if (o == null) {
150             return null;
151         }
152 
153         Object target = null;
154 
155         PropertyDescriptor[] origDescriptors =
156                 PropertyUtils.getPropertyDescriptors(o);
157 
158         for (int i = 0; i < origDescriptors.length; i++) {
159             String name = origDescriptors[i].getName();
160 
161             if (origDescriptors[i].getPropertyType().equals(List.class)) {
162                 List list = (List) PropertyUtils.getProperty(o, name);
163                 for (int j=0; j < list.size(); j++) {
164                     Object origin = list.get(j);
165                     target = convert(origin);
166                     list.set(j, target);
167                 }
168                 PropertyUtils.setProperty(o, name, list);
169             }
170         }
171         return o;
172     }
173 }