KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > util > ConvertUtil


1 package org.appfuse.util;
2
3 import java.beans.PropertyDescriptor JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.LinkedHashMap JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.util.ResourceBundle JavaDoc;
12
13 import org.apache.commons.beanutils.BeanUtils;
14 import org.apache.commons.beanutils.PropertyUtils;
15 import org.apache.commons.lang.StringUtils;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.appfuse.model.BaseObject;
19 import org.appfuse.model.LabelValue;
20 import org.springframework.aop.support.AopUtils;
21
22
23 /**
24  * Utility class to convert one object to another.
25  *
26  * <p>
27  * <a HREF="ConvertUtil.java.htm"><i>View Source</i></a>
28  * </p>
29  *
30  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
31  */

32 public final class ConvertUtil {
33     //~ Static fields/initializers =============================================
34

35     private static Log log = LogFactory.getLog(ConvertUtil.class);
36
37     //~ Methods ================================================================
38

39     /**
40      * Method to convert a ResourceBundle to a Map object.
41      * @param rb a given resource bundle
42      * @return Map a populated map
43      */

44     public static Map JavaDoc convertBundleToMap(ResourceBundle JavaDoc rb) {
45         Map JavaDoc map = new HashMap JavaDoc();
46
47         for (Enumeration JavaDoc keys = rb.getKeys(); keys.hasMoreElements();) {
48             String JavaDoc key = (String JavaDoc) keys.nextElement();
49             map.put(key, rb.getString(key));
50         }
51
52         return map;
53     }
54     
55     public static Map JavaDoc convertListToMap(List JavaDoc list) {
56         Map JavaDoc map = new LinkedHashMap JavaDoc();
57         
58         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
59             LabelValue option = (LabelValue) it.next();
60             map.put(option.getLabel(), option.getValue());
61         }
62         
63         return map;
64     }
65
66     /**
67      * Method to convert a ResourceBundle to a Properties object.
68      * @param rb a given resource bundle
69      * @return Properties a populated properties object
70      */

71     public static Properties JavaDoc convertBundleToProperties(ResourceBundle JavaDoc rb) {
72         Properties JavaDoc props = new Properties JavaDoc();
73
74         for (Enumeration JavaDoc keys = rb.getKeys(); keys.hasMoreElements();) {
75             String JavaDoc key = (String JavaDoc) keys.nextElement();
76             props.put(key, rb.getString(key));
77         }
78
79         return props;
80     }
81
82     /**
83      * Convenience method used by tests to populate an object from a
84      * ResourceBundle
85      * @param obj an initialized object
86      * @param rb a resource bundle
87      * @return a populated object
88      */

89     public static Object JavaDoc populateObject(Object JavaDoc obj, ResourceBundle JavaDoc rb) {
90         try {
91             Map JavaDoc map = convertBundleToMap(rb);
92
93             BeanUtils.copyProperties(obj, map);
94         } catch (Exception JavaDoc e) {
95             e.printStackTrace();
96             log.error("Exception occured populating object: " + e.getMessage());
97         }
98
99         return obj;
100     }
101
102     /**
103      * This method inspects a POJO or Form and figures out its pojo/form
104      * equivalent.
105      *
106      * @param o the object to inspect
107      * @return the Class of the persistable object
108      * @throws ClassNotFoundException
109      * @throws InstantiationException
110      * @throws IllegalAccessException
111      */

112     public static Object JavaDoc getOpposingObject(Object JavaDoc o) throws ClassNotFoundException JavaDoc,
113                                                 InstantiationException JavaDoc,
114                                                 IllegalAccessException JavaDoc {
115         String JavaDoc name = o.getClass().getName();
116
117         if (o instanceof BaseObject) {
118             if (log.isDebugEnabled()) {
119                 log.debug("getting form equivalent of pojo...");
120             }
121
122             name = StringUtils.replace(name, ".model.", ".webapp.form.");
123             if (AopUtils.isCglibProxy(o)) {
124                 name = name.substring(0, name.indexOf("$$"));
125             }
126             name += "Form";
127         } else {
128             if (log.isDebugEnabled()) {
129                 log.debug("getting pojo equivalent of form...");
130             }
131             name = StringUtils.replace(name, ".webapp.form.", ".model.");
132             name = name.substring(0, name.lastIndexOf("Form"));
133         }
134         
135         Class JavaDoc obj = Class.forName(name);
136
137         if (log.isDebugEnabled()) {
138             log.debug("returning className: " + obj.getName());
139         }
140
141         return obj.newInstance();
142     }
143
144     /**
145      * Convenience method to convert a form to a POJO and back again
146      *
147      * @param o the object to tranfer properties from
148      * @return converted object
149      */

150     public static Object JavaDoc convert(Object JavaDoc o) throws Exception JavaDoc {
151         if (o == null) {
152             return null;
153         }
154         Object JavaDoc target = getOpposingObject(o);
155         BeanUtils.copyProperties(target, o);
156         return target;
157     }
158
159     /**
160      * Convenience method to convert Lists (in a Form) from POJOs to Forms.
161      * Also checks for and formats dates.
162      *
163      * @param o
164      * @return Object with converted lists
165      * @throws Exception
166      */

167     public static Object JavaDoc convertLists(Object JavaDoc o) throws Exception JavaDoc {
168         if (o == null) {
169             return null;
170         }
171
172         Object JavaDoc target = null;
173
174         PropertyDescriptor JavaDoc[] origDescriptors =
175                 PropertyUtils.getPropertyDescriptors(o);
176
177         for (int i = 0; i < origDescriptors.length; i++) {
178             String JavaDoc name = origDescriptors[i].getName();
179
180             if (origDescriptors[i].getPropertyType().equals(List JavaDoc.class)) {
181                 List JavaDoc list = (List JavaDoc) PropertyUtils.getProperty(o, name);
182                 for (int j=0; j < list.size(); j++) {
183                     Object JavaDoc origin = list.get(j);
184                     target = convert(origin);
185                     list.set(j, target);
186                 }
187                 PropertyUtils.setProperty(o, name, list);
188             }
189         }
190         return o;
191     }
192 }
193
Popular Tags