001    /**
002     * Copyright 2005-2011 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.springframework.beans.BeanWrapper;
019    import org.springframework.beans.BeanWrapperImpl;
020    import org.springframework.beans.BeansException;
021    
022    import java.beans.PropertyDescriptor;
023    import java.util.Map;
024    
025    /**
026     * Utility methods to get/set property values and working with objects
027     * 
028     * @author Kuali Rice Team (rice.collab@kuali.org)
029     * @see org.springframework.beans.BeanWrapper
030     */
031    public class ObjectPropertyUtils {
032            private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ObjectPropertyUtils.class);
033    
034            public static void copyPropertiesToObject(Map<String, String> properties, Object object) {
035                    for (Map.Entry<String, String> property : properties.entrySet()) {
036                            setPropertyValue(object, property.getKey(), property.getValue());
037                    }
038            }
039    
040            public static PropertyDescriptor[] getPropertyDescriptors(Object object) {
041                    return wrapObject(object).getPropertyDescriptors();
042            }
043    
044            public static Class<?> getPropertyType(Class<?> object, String propertyPath) {
045                    return new BeanWrapperImpl(object).getPropertyType(propertyPath);
046            }
047    
048            public static Class<?> getPropertyType(Object object, String propertyPath) {
049                    return wrapObject(object).getPropertyType(propertyPath);
050            }
051    
052            @SuppressWarnings("unchecked")
053            public static <T extends Object> T getPropertyValue(Object object, String propertyPath) {
054                T result = null;
055                try {
056                    result = (T) wrapObject(object).getPropertyValue(propertyPath);
057                } catch (RuntimeException e) {
058                    throw new RuntimeException("Error getting property '" + propertyPath + "' from " + object, e);
059                }
060                return result;
061            }
062    
063            public static void initializeProperty(Object object, String propertyPath) {
064                    Class<?> propertyType = getPropertyType(object, propertyPath);
065            try {
066                setPropertyValue(object, propertyPath, propertyType.newInstance());
067            } catch (InstantiationException e) {
068                // just set the value to null
069                setPropertyValue(object, propertyPath, null);
070            } catch (IllegalAccessException e) {
071                throw new RuntimeException("Unable to set new instance for property: " + propertyPath, e);
072            }
073        }
074    
075            public static void setPropertyValue(Object object, String propertyPath, Object propertyValue) {
076                    wrapObject(object).setPropertyValue(propertyPath, propertyValue);
077            }
078    
079            public static void setPropertyValue(Object object, String propertyPath, Object propertyValue, boolean ignoreUnknown) {
080                    try {
081                            wrapObject(object).setPropertyValue(propertyPath, propertyValue);
082                    }
083                    catch (BeansException e) {
084                            // only throw exception if they have indicated to not ignore unknown
085                            if (!ignoreUnknown) {
086                                    throw new RuntimeException(e);
087                            }
088                            if (LOG.isTraceEnabled()) {
089                                    LOG.trace("Ignoring exception thrown during setting of property '" + propertyPath + "': "
090                                                    + e.getLocalizedMessage());
091                            }
092                    }
093            }
094            
095        public static boolean isReadableProperty(Object object, String propertyPath) {
096            return wrapObject(object).isReadableProperty(propertyPath);
097        }
098    
099        public static boolean isWritableProperty(Object object, String propertyPath) {
100            return wrapObject(object).isWritableProperty(propertyPath);
101        }
102    
103            public static BeanWrapper wrapObject(Object object) {
104                    BeanWrapper beanWrapper = new BeanWrapperImpl(object);
105                    beanWrapper.setAutoGrowNestedPaths(true);
106                    
107                    return beanWrapper;
108            }
109    
110    }