1 package org.appfuse.util; 2 3 import java.beans.PropertyDescriptor ; 4 import java.util.Enumeration ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.LinkedHashMap ; 8 import java.util.List ; 9 import java.util.Map ; 10 import java.util.Properties ; 11 import java.util.ResourceBundle ; 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 32 public final class ConvertUtil { 33 35 private static Log log = LogFactory.getLog(ConvertUtil.class); 36 37 39 44 public static Map convertBundleToMap(ResourceBundle rb) { 45 Map map = new HashMap (); 46 47 for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) { 48 String key = (String ) keys.nextElement(); 49 map.put(key, rb.getString(key)); 50 } 51 52 return map; 53 } 54 55 public static Map convertListToMap(List list) { 56 Map map = new LinkedHashMap (); 57 58 for (Iterator 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 71 public static Properties convertBundleToProperties(ResourceBundle rb) { 72 Properties props = new Properties (); 73 74 for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) { 75 String key = (String ) keys.nextElement(); 76 props.put(key, rb.getString(key)); 77 } 78 79 return props; 80 } 81 82 89 public static Object populateObject(Object obj, ResourceBundle rb) { 90 try { 91 Map map = convertBundleToMap(rb); 92 93 BeanUtils.copyProperties(obj, map); 94 } catch (Exception e) { 95 e.printStackTrace(); 96 log.error("Exception occured populating object: " + e.getMessage()); 97 } 98 99 return obj; 100 } 101 102 112 public static Object getOpposingObject(Object o) throws ClassNotFoundException , 113 InstantiationException , 114 IllegalAccessException { 115 String 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 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 150 public static Object convert(Object o) throws Exception { 151 if (o == null) { 152 return null; 153 } 154 Object target = getOpposingObject(o); 155 BeanUtils.copyProperties(target, o); 156 return target; 157 } 158 159 167 public static Object convertLists(Object o) throws Exception { 168 if (o == null) { 169 return null; 170 } 171 172 Object target = null; 173 174 PropertyDescriptor [] origDescriptors = 175 PropertyUtils.getPropertyDescriptors(o); 176 177 for (int i = 0; i < origDescriptors.length; i++) { 178 String name = origDescriptors[i].getName(); 179 180 if (origDescriptors[i].getPropertyType().equals(List .class)) { 181 List list = (List ) PropertyUtils.getProperty(o, name); 182 for (int j=0; j < list.size(); j++) { 183 Object 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 |