001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.datadictionary.impl;
017
018import java.lang.reflect.InvocationTargetException;
019import java.util.List;
020
021import org.apache.commons.beanutils.BeanUtils;
022import org.apache.commons.beanutils.PropertyUtils;
023import org.apache.log4j.Logger;
024import org.kuali.rice.krad.datadictionary.BeanOverride;
025import org.kuali.rice.krad.datadictionary.FieldOverride;
026
027/**
028 * The base implementation of the BeanOverride interface. 
029 * 
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 *
032 */
033public class BeanOverrideImpl implements BeanOverride {
034        private static final Logger LOG = Logger.getLogger(BeanOverrideImpl.class);
035        private String beanName;
036    private List<FieldOverride> fieldOverrides;
037
038    /**
039     * 
040     * @see org.kuali.rice.krad.datadictionary.BeanOverride#getFieldOverrides()
041     */
042    public List<FieldOverride> getFieldOverrides() {
043        return this.fieldOverrides;
044    }
045
046    public void setFieldOverrides(List<FieldOverride> fieldOverirdes) {
047        this.fieldOverrides = fieldOverirdes;
048    }
049
050    /**
051     * 
052     * @see org.kuali.rice.krad.datadictionary.BeanOverride#getBeanName()
053     */
054    public String getBeanName() {
055        return this.beanName;
056    }
057
058    public void setBeanName(String beanName) {
059        this.beanName = beanName;
060    }
061
062    /**
063     * 
064     * @see org.kuali.rice.krad.datadictionary.BeanOverride#performOverride(java.lang.Object)
065     */
066    public void performOverride(Object bean) 
067    {
068        try 
069        {
070            for (FieldOverride fieldOverride: fieldOverrides)
071            {
072                Object property = PropertyUtils.getProperty(bean, fieldOverride.getPropertyName());                
073                Object newProperty = fieldOverride.performFieldOverride(bean, property);                
074                BeanUtils.setProperty(bean, fieldOverride.getPropertyName(), newProperty);
075            }
076        }
077        catch(IllegalAccessException e )
078        {
079            throw new RuntimeException(e);
080        }
081        catch(InvocationTargetException e)
082        {
083            throw new RuntimeException(e);
084            
085        }
086        catch (NoSuchMethodException e)
087        {
088            throw new RuntimeException(e);
089        }
090    }
091}