1 23 24 package org.infoglue.cms.util.validators; 25 26 import java.util.regex.Pattern ; 27 28 import org.infoglue.cms.controllers.kernel.impl.simple.ValidationController; 29 import org.infoglue.cms.exception.ConstraintException; 30 import org.infoglue.cms.exception.SystemException; 31 import org.infoglue.cms.util.ConstraintExceptionBuffer; 32 33 34 47 48 public abstract class AbstractValidator 49 { 50 51 private static final String REQUIRED_FIELD_ERROR_CODE = "300"; 52 private static final String NOTUNIQUE_FIELD_ERROR_CODE = "302"; 53 54 55 private ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 57 private String fieldName; 59 private boolean isRequired; 61 62 private boolean mustBeUnique; 63 64 private Class objectClass = null; 65 66 private Integer excludeId = null; 67 private Object excludeObject = null; 68 69 protected AbstractValidator(String fieldName) 70 { 71 this(fieldName, true); 72 } 73 74 75 78 protected AbstractValidator(String fieldName, boolean isRequired) 79 { 80 this.fieldName = fieldName; 81 this.isRequired = isRequired; 82 } 83 84 protected AbstractValidator(String fieldName, boolean isRequired, boolean mustBeUnique, Class objectClass, Integer excludeId, Object excludeObject) 85 { 86 this.fieldName = fieldName; 87 this.isRequired = isRequired; 88 this.mustBeUnique = mustBeUnique; 89 this.objectClass = objectClass; 90 this.excludeId = excludeId; 91 this.excludeObject = excludeObject; 92 } 93 94 95 98 private final ConstraintException createConstraintException(String errorCode) 99 { 100 return new ConstraintException(this.fieldName, errorCode); 101 } 102 103 104 107 protected void validateIsRequired(Object value) throws ConstraintException 108 { 109 if(this.isRequired && value == null) 110 { 111 throw createConstraintException(REQUIRED_FIELD_ERROR_CODE); 112 } 113 } 114 115 protected void validateUniqueness(String value) throws ConstraintException, SystemException 116 { 117 if (this.mustBeUnique) 118 { 119 Pattern p = Pattern.compile("[.\\s]+"); 120 String [] arrString = p.split(fieldName); 121 String cleanField = arrString[arrString.length-1]; 122 123 if(ValidationController.fieldValueExists(objectClass, cleanField, value, excludeId, excludeObject)) 124 { 125 throw createConstraintException(NOTUNIQUE_FIELD_ERROR_CODE); 126 } 127 } 128 } 129 130 133 protected final void addConstraintException(String errorCode) 134 { 135 this.ceb.add(createConstraintException(errorCode)); 136 } 137 138 141 protected final void failIfAnyExceptionsFound() throws ConstraintException 142 { 143 this.ceb.throwIfNotEmpty(); 144 } 145 146 150 public String getFieldName() 151 { 152 return fieldName; 153 } 154 155 159 public void setFieldName(String fieldName) 160 { 161 this.fieldName = fieldName; 162 } 163 164 168 public boolean isRequired() 169 { 170 return isRequired; 171 } 172 173 177 public void setIsRequired(boolean isRequired) 178 { 179 this.isRequired = isRequired; 180 } 181 182 186 public boolean isMustBeUnique() 187 { 188 return mustBeUnique; 189 } 190 191 195 public void setMustBeUnique(boolean mustBeUnique) 196 { 197 this.mustBeUnique = mustBeUnique; 198 } 199 200 204 public Class getObjectClass() 205 { 206 return objectClass; 207 } 208 209 213 public void setObjectClass(Class objectClass) 214 { 215 this.objectClass = objectClass; 216 } 217 218 222 public Integer getExcludeId() 223 { 224 return excludeId; 225 } 226 227 231 public void setExcludeId(Integer excludeId) 232 { 233 this.excludeId = excludeId; 234 } 235 236 240 public Object getExcludeObject() 241 { 242 return excludeObject; 243 } 244 245 249 public void setExcludeObject(Object excludeObject) 250 { 251 this.excludeObject = excludeObject; 252 } 253 254 } 255 | Popular Tags |