View Javadoc
1   package org.kuali.common.util.validate;
2   
3   import java.lang.reflect.Field;
4   
5   import org.apache.commons.lang3.StringUtils;
6   import org.kuali.common.util.ReflectionUtils;
7   
8   import com.google.common.base.Optional;
9   
10  public class NoBlankStringsValidator extends AbstractFieldsValidator<NoBlankStrings, Object> {
11  
12  	@Override
13  	protected Optional<String> validate(Field field, Object instance) {
14  
15  		// This field may not be a String
16  		if (!ReflectionUtils.isString(field)) {
17  			return Optional.absent();
18  		}
19  
20  		// Extract the value of the field into an optional
21  		Optional<?> fieldValue = ReflectionUtils.get(field, instance);
22  
23  		// We know the field contains a string at this point
24  		String string = (String) fieldValue.orNull();
25  
26  		// Null is ok
27  		if (string == null) {
28  			return Optional.absent();
29  		}
30  
31  		// Non-null value cannot be blank
32  		if (StringUtils.isBlank(string)) {
33  			return Validation.errorMessage(field, "blank strings not allowed");
34  		} else {
35  			return Optional.absent();
36  		}
37  	}
38  
39  }