1 56 package org.objectstyle.cayenne.validation; 57 58 import java.util.Collection ; 59 60 import org.apache.commons.beanutils.PropertyUtils; 61 import org.objectstyle.cayenne.CayenneRuntimeException; 62 63 71 public class BeanValidationFailure extends SimpleValidationFailure { 72 73 protected String property; 74 75 private static String validationMessage(String attribute, String message) { 76 StringBuffer buffer = new StringBuffer (message.length() + attribute.length() + 5); 77 buffer.append('\"').append(attribute).append("\" ").append(message); 78 return buffer.toString(); 79 } 80 81 85 public static ValidationFailure validateNotEmpty( 86 Object bean, 87 String attribute, 88 Collection value) { 89 90 if (value == null) { 91 return new BeanValidationFailure( 92 bean, 93 attribute, 94 validationMessage(attribute, " is required.")); 95 } 96 97 if (value.isEmpty()) { 98 return new BeanValidationFailure( 99 bean, 100 attribute, 101 validationMessage(attribute, " can not be empty.")); 102 } 103 104 return null; 105 } 106 107 public static ValidationFailure validateMandatory( 108 Object bean, 109 String attribute, 110 Object value) { 111 112 if (value instanceof String ) { 113 return validateNotEmpty(bean, attribute, (String ) value); 114 } 115 if (value instanceof Collection ) { 116 return validateNotEmpty(bean, attribute, (Collection ) value); 117 } 118 return validateNotNull(bean, attribute, value); 119 } 120 121 public static ValidationFailure validateMandatory(Object bean, String attribute) { 122 if (bean == null) { 123 throw new NullPointerException ("Null bean."); 124 } 125 126 try { 127 Object result = PropertyUtils.getProperty(bean, attribute); 128 return validateMandatory(bean, attribute, result); 129 } 130 catch (Exception ex) { 131 throw new CayenneRuntimeException( 132 "Error validationg bean property: " 133 + bean.getClass().getName() 134 + "." 135 + attribute, 136 ex); 137 } 138 } 139 140 public static ValidationFailure validateNotNull( 141 Object bean, 142 String attribute, 143 Object value) { 144 145 if (value == null) { 146 return new BeanValidationFailure( 147 bean, 148 attribute, 149 validationMessage(attribute, " is required.")); 150 } 151 152 return null; 153 } 154 155 public static ValidationFailure validateNotEmpty( 156 Object bean, 157 String attribute, 158 String value) { 159 if (value == null || value.length() == 0) { 160 return new BeanValidationFailure( 161 bean, 162 attribute, 163 validationMessage(attribute, " is a required field.")); 164 } 165 return null; 166 } 167 168 171 public BeanValidationFailure(Object source, String property, Object error) { 172 super(source, error); 173 174 if (source == null && property != null) { 175 throw new IllegalArgumentException ("ValidationFailure cannot have 'property' when 'source' is null."); 176 } 177 178 this.property = property; 179 } 180 181 184 public String getProperty() { 185 return property; 186 } 187 188 191 public String toString() { 192 StringBuffer buffer = new StringBuffer (); 193 194 buffer.append("Validation failure for "); 195 Object source = getSource(); 196 197 if (source == null) { 198 buffer.append("[General]"); 199 } 200 else { 201 String property = getProperty(); 202 buffer.append(source.getClass().getName()).append('.').append( 203 (property == null ? "[General]" : property)); 204 } 205 buffer.append(": "); 206 buffer.append(getDescription()); 207 return buffer.toString(); 208 } 209 } 210 | Popular Tags |