001 /** 002 * Copyright 2005-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.uif.util; 017 018 import org.kuali.rice.krad.datadictionary.parse.StringListConverter; 019 import org.kuali.rice.krad.datadictionary.parse.StringMapConverter; 020 import org.springframework.beans.BeanWrapper; 021 import org.springframework.beans.BeanWrapperImpl; 022 import org.springframework.beans.BeansException; 023 import org.springframework.core.convert.support.GenericConversionService; 024 025 import java.beans.PropertyDescriptor; 026 import java.util.Map; 027 028 /** 029 * Utility methods to get/set property values and working with objects 030 * 031 * @author Kuali Rice Team (rice.collab@kuali.org) 032 * @see org.springframework.beans.BeanWrapper 033 */ 034 public class ObjectPropertyUtils { 035 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ObjectPropertyUtils.class); 036 037 public static void copyPropertiesToObject(Map<String, String> properties, Object object) { 038 for (Map.Entry<String, String> property : properties.entrySet()) { 039 setPropertyValue(object, property.getKey(), property.getValue()); 040 } 041 } 042 043 public static PropertyDescriptor[] getPropertyDescriptors(Object object) { 044 return wrapObject(object).getPropertyDescriptors(); 045 } 046 047 public static Class<?> getPropertyType(Class<?> object, String propertyPath) { 048 return new BeanWrapperImpl(object).getPropertyType(propertyPath); 049 } 050 051 public static Class<?> getPropertyType(Object object, String propertyPath) { 052 return wrapObject(object).getPropertyType(propertyPath); 053 } 054 055 @SuppressWarnings("unchecked") 056 public static <T extends Object> T getPropertyValue(Object object, String propertyPath) { 057 T result = null; 058 try { 059 result = (T) wrapObject(object).getPropertyValue(propertyPath); 060 } catch (RuntimeException e) { 061 throw new RuntimeException("Error getting property '" + propertyPath + "' from " + object, e); 062 } 063 return result; 064 } 065 066 public static void initializeProperty(Object object, String propertyPath) { 067 Class<?> propertyType = getPropertyType(object, propertyPath); 068 try { 069 setPropertyValue(object, propertyPath, propertyType.newInstance()); 070 } catch (InstantiationException e) { 071 // just set the value to null 072 setPropertyValue(object, propertyPath, null); 073 } catch (IllegalAccessException e) { 074 throw new RuntimeException("Unable to set new instance for property: " + propertyPath, e); 075 } 076 } 077 078 public static void setPropertyValue(Object object, String propertyPath, Object propertyValue) { 079 wrapObject(object).setPropertyValue(propertyPath, propertyValue); 080 } 081 082 public static void setPropertyValue(Object object, String propertyPath, Object propertyValue, boolean ignoreUnknown) { 083 try { 084 wrapObject(object).setPropertyValue(propertyPath, propertyValue); 085 } 086 catch (BeansException e) { 087 // only throw exception if they have indicated to not ignore unknown 088 if (!ignoreUnknown) { 089 throw new RuntimeException(e); 090 } 091 if (LOG.isTraceEnabled()) { 092 LOG.trace("Ignoring exception thrown during setting of property '" + propertyPath + "': " 093 + e.getLocalizedMessage()); 094 } 095 } 096 } 097 098 public static boolean isReadableProperty(Object object, String propertyPath) { 099 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 }