1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.rice.krad.uif.util;
17  
18  import org.kuali.rice.krad.datadictionary.parse.StringListConverter;
19  import org.kuali.rice.krad.datadictionary.parse.StringMapConverter;
20  import org.springframework.beans.BeanWrapper;
21  import org.springframework.beans.BeanWrapperImpl;
22  import org.springframework.beans.BeansException;
23  import org.springframework.core.convert.support.GenericConversionService;
24  
25  import java.beans.PropertyDescriptor;
26  import java.util.Map;
27  
28  
29  
30  
31  
32  
33  
34  public class ObjectPropertyUtils {
35  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ObjectPropertyUtils.class);
36  
37  	public static void copyPropertiesToObject(Map<String, String> properties, Object object) {
38  		for (Map.Entry<String, String> property : properties.entrySet()) {
39  			setPropertyValue(object, property.getKey(), property.getValue());
40  		}
41  	}
42  
43  	public static PropertyDescriptor[] getPropertyDescriptors(Object object) {
44  		return wrapObject(object).getPropertyDescriptors();
45  	}
46  
47  	public static Class<?> getPropertyType(Class<?> object, String propertyPath) {
48  		return new BeanWrapperImpl(object).getPropertyType(propertyPath);
49  	}
50  
51  	public static Class<?> getPropertyType(Object object, String propertyPath) {
52  		return wrapObject(object).getPropertyType(propertyPath);
53  	}
54  
55  	@SuppressWarnings("unchecked")
56  	public static <T extends Object> T getPropertyValue(Object object, String propertyPath) {
57  	    T result = null;
58  	    try {
59  	        result = (T) wrapObject(object).getPropertyValue(propertyPath);
60  	    } catch (RuntimeException e) {
61  	        throw new RuntimeException("Error getting property '" + propertyPath + "' from " + object, e);
62  	    }
63  	    return result;
64  	}
65  
66  	public static void initializeProperty(Object object, String propertyPath) {
67  		Class<?> propertyType = getPropertyType(object, propertyPath);
68          try {
69              setPropertyValue(object, propertyPath, propertyType.newInstance());
70          } catch (InstantiationException e) {
71              
72              setPropertyValue(object, propertyPath, null);
73          } catch (IllegalAccessException e) {
74              throw new RuntimeException("Unable to set new instance for property: " + propertyPath, e);
75          }
76      }
77  
78  	public static void setPropertyValue(Object object, String propertyPath, Object propertyValue) {
79  		wrapObject(object).setPropertyValue(propertyPath, propertyValue);
80  	}
81  
82  	public static void setPropertyValue(Object object, String propertyPath, Object propertyValue, boolean ignoreUnknown) {
83  		try {
84  			wrapObject(object).setPropertyValue(propertyPath, propertyValue);
85  		}
86  		catch (BeansException e) {
87  			
88  			if (!ignoreUnknown) {
89  				throw new RuntimeException(e);
90  			}
91  			if (LOG.isTraceEnabled()) {
92  				LOG.trace("Ignoring exception thrown during setting of property '" + propertyPath + "': "
93  						+ e.getLocalizedMessage());
94  			}
95  		}
96  	}
97  
98      public static boolean isReadableProperty(Object object, String propertyPath) {
99          return wrapObject(object).isReadableProperty(propertyPath);
100     }
101 
102     public static boolean isWritableProperty(Object object, String propertyPath) {
103         return wrapObject(object).isWritableProperty(propertyPath);
104     }
105 
106 	public static BeanWrapper wrapObject(Object object) {
107 		BeanWrapper beanWrapper = new BeanWrapperImpl(object);
108 		beanWrapper.setAutoGrowNestedPaths(true);
109 
110         GenericConversionService conversionService = new GenericConversionService();
111         conversionService.addConverter(new StringMapConverter());
112         conversionService.addConverter(new StringListConverter());
113         beanWrapper.setConversionService(conversionService);
114 
115 		return beanWrapper;
116 	}
117 
118 }