001    /**
002     * Copyright 2010-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.common.util.property.processor;
017    
018    import java.lang.reflect.InvocationTargetException;
019    import java.util.Properties;
020    
021    import org.apache.commons.beanutils.BeanUtils;
022    
023    /**
024     * Copy a String property value into a bean
025     */
026    public class CopyStringProcessor implements PropertyProcessor {
027    
028            Object bean;
029            String beanProperty;
030            String propertyKey;
031    
032            public CopyStringProcessor() {
033                    this(null, null, null);
034            }
035    
036            public CopyStringProcessor(Object bean, String beanProperty, String propertyKey) {
037                    super();
038                    this.bean = bean;
039                    this.beanProperty = beanProperty;
040                    this.propertyKey = propertyKey;
041            }
042    
043            @Override
044            public void process(Properties properties) {
045                    copyProperty(bean, beanProperty, properties, propertyKey);
046            }
047    
048            protected void copyProperty(Object bean, String beanProperty, Properties properties, String propertyKey) {
049                    try {
050                            String value = properties.getProperty(propertyKey);
051                            BeanUtils.copyProperty(bean, beanProperty, value);
052                    } catch (InvocationTargetException e) {
053                            throw new IllegalArgumentException(e);
054                    } catch (IllegalAccessException e) {
055                            throw new IllegalArgumentException(e);
056                    }
057            }
058    
059            public Object getBean() {
060                    return bean;
061            }
062    
063            public void setBean(Object bean) {
064                    this.bean = bean;
065            }
066    
067            public String getBeanProperty() {
068                    return beanProperty;
069            }
070    
071            public void setBeanProperty(String beanProperty) {
072                    this.beanProperty = beanProperty;
073            }
074    
075            public String getPropertyKey() {
076                    return propertyKey;
077            }
078    
079            public void setPropertyKey(String propertyKey) {
080                    this.propertyKey = propertyKey;
081            }
082    
083    }