View Javadoc
1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.nullify;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.apache.commons.beanutils.PropertyUtils;
23  import org.kuali.common.util.Str;
24  import org.springframework.util.Assert;
25  
26  public class BeanNullifier implements Nullifier {
27  
28  	Object bean;
29  	List<String> properties;
30  	List<String> nullTokens = Arrays.asList(NullUtils.NULL);
31  	boolean caseSensitive = false;
32  
33  	@Override
34  	public void nullify() {
35  		Assert.notNull(bean, "bean cannot be null");
36  		Assert.notNull(properties, "properties cannot be null");
37  		Assert.notNull(nullTokens, "nullTokens cannot be null");
38  
39  		for (String property : properties) {
40  			Object value = getProperty(bean, property);
41  			if (isNullify(value, nullTokens, caseSensitive)) {
42  				setProperty(bean, property, null);
43  			}
44  		}
45  	}
46  
47  	protected boolean isNullify(Object value, List<String> nullTokens, boolean caseSensitive) {
48  		if (value == null) {
49  			return false;
50  		} else {
51  			return Str.contains(nullTokens, value.toString(), caseSensitive);
52  		}
53  	}
54  
55  	protected void setProperty(Object bean, String property, Object value) {
56  		try {
57  			PropertyUtils.setProperty(bean, property, value);
58  		} catch (NoSuchMethodException e) {
59  			throw new IllegalArgumentException(e);
60  		} catch (InvocationTargetException e) {
61  			throw new IllegalArgumentException(e);
62  		} catch (IllegalAccessException e) {
63  			throw new IllegalArgumentException(e);
64  		}
65  	}
66  
67  	protected Object getProperty(Object bean, String property) {
68  		try {
69  			return PropertyUtils.getProperty(bean, property);
70  		} catch (NoSuchMethodException e) {
71  			throw new IllegalArgumentException(e);
72  		} catch (InvocationTargetException e) {
73  			throw new IllegalArgumentException(e);
74  		} catch (IllegalAccessException e) {
75  			throw new IllegalArgumentException(e);
76  		}
77  	}
78  
79  	public Object getBean() {
80  		return bean;
81  	}
82  
83  	public void setBean(Object bean) {
84  		this.bean = bean;
85  	}
86  
87  	public List<String> getProperties() {
88  		return properties;
89  	}
90  
91  	public void setProperties(List<String> properties) {
92  		this.properties = properties;
93  	}
94  
95  	public List<String> getNullTokens() {
96  		return nullTokens;
97  	}
98  
99  	public void setNullTokens(List<String> nullTokens) {
100 		this.nullTokens = nullTokens;
101 	}
102 
103 	public boolean isCaseSensitive() {
104 		return caseSensitive;
105 	}
106 
107 	public void setCaseSensitive(boolean caseSensitive) {
108 		this.caseSensitive = caseSensitive;
109 	}
110 
111 }