001    /**
002     * Copyright 2010-2013 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.nullify;
017    
018    import java.lang.reflect.InvocationTargetException;
019    import java.util.Arrays;
020    import java.util.List;
021    
022    import org.apache.commons.beanutils.PropertyUtils;
023    import org.kuali.common.util.Str;
024    import org.kuali.common.util.property.Constants;
025    import org.springframework.util.Assert;
026    
027    public class DefaultBeanNullifier implements Nullify {
028    
029            Object bean;
030            List<String> properties;
031            List<String> nullTokens = Arrays.asList(Constants.NULL);
032            boolean caseSensitive = false;
033    
034            @Override
035            public void nullify() {
036                    Assert.notNull(bean, "bean cannot be null");
037                    Assert.notNull(properties, "properties cannot be null");
038                    Assert.notNull(nullTokens, "nullTokens cannot be null");
039    
040                    for (String property : properties) {
041                            Object value = getProperty(bean, property);
042                            if (isNullify(value, nullTokens, caseSensitive)) {
043                                    setProperty(bean, property, null);
044                            }
045                    }
046            }
047    
048            protected boolean isNullify(Object value, List<String> nullTokens, boolean caseSensitive) {
049                    if (value == null) {
050                            return false;
051                    } else {
052                            return Str.contains(nullTokens, value.toString(), caseSensitive);
053                    }
054            }
055    
056            protected void setProperty(Object bean, String property, Object value) {
057                    try {
058                            PropertyUtils.setProperty(bean, property, value);
059                    } catch (NoSuchMethodException e) {
060                            throw new IllegalArgumentException(e);
061                    } catch (InvocationTargetException e) {
062                            throw new IllegalArgumentException(e);
063                    } catch (IllegalAccessException e) {
064                            throw new IllegalArgumentException(e);
065                    }
066            }
067    
068            protected Object getProperty(Object bean, String property) {
069                    try {
070                            return PropertyUtils.getProperty(bean, property);
071                    } catch (NoSuchMethodException e) {
072                            throw new IllegalArgumentException(e);
073                    } catch (InvocationTargetException e) {
074                            throw new IllegalArgumentException(e);
075                    } catch (IllegalAccessException e) {
076                            throw new IllegalArgumentException(e);
077                    }
078            }
079    
080            public Object getBean() {
081                    return bean;
082            }
083    
084            public void setBean(Object bean) {
085                    this.bean = bean;
086            }
087    
088            public List<String> getProperties() {
089                    return properties;
090            }
091    
092            public void setProperties(List<String> properties) {
093                    this.properties = properties;
094            }
095    
096            public List<String> getNullTokens() {
097                    return nullTokens;
098            }
099    
100            public void setNullTokens(List<String> nullTokens) {
101                    this.nullTokens = nullTokens;
102            }
103    
104            public boolean isCaseSensitive() {
105                    return caseSensitive;
106            }
107    
108            public void setCaseSensitive(boolean caseSensitive) {
109                    this.caseSensitive = caseSensitive;
110            }
111    
112    }