001    /*
002     * Copyright 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.kfs.module.cam.util;
017    
018    import java.beans.PropertyDescriptor;
019    
020    import org.apache.commons.beanutils.PropertyUtils;
021    
022    /**
023     * This class is a utility which will do copying of attributes from a original object to destination object. Intention was to
024     * provide a attribute value copying mechanism which is less expensive than ObjectUtils.deepCopy() method
025     */
026    public final class ObjectValueUtils {
027    
028        private ObjectValueUtils() {
029        }
030    
031        /**
032         * This method uses simple getter/setter methods to copy object values from a original object to destination object
033         * 
034         * @param origin original object
035         * @param destination destination object
036         */
037        public static void copySimpleProperties(Object origin, Object destination) {
038            try {
039                Object[] empty = new Object[] {};
040                PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(origin.getClass());
041                for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
042                    if (propertyDescriptor.getReadMethod() != null && propertyDescriptor.getWriteMethod() != null) {
043                        Object value = propertyDescriptor.getReadMethod().invoke(origin, empty);
044                        if (value != null) {
045                            propertyDescriptor.getWriteMethod().invoke(destination, value);
046                        }
047                    }
048                }
049            }
050            catch (Exception e) {
051                throw new RuntimeException("Unexpected error while copying properties.", e);
052    
053            }
054        }
055    
056    
057    }