View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Utility methods to get/set property values and working with objects
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   * @see org.springframework.beans.BeanWrapper
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              // just set the value to null
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  			// only throw exception if they have indicated to not ignore unknown
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 }