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