1 package org.apache.fulcrum.intake.validator; 2 3 56 57 import java.math.BigDecimal ; 58 import java.util.Map ; 59 60 import org.apache.fulcrum.ServiceException; 61 62 79 public class NumberValidator 80 extends DefaultValidator 81 { 82 private static String INVALID_NUMBER = "Entry was not a valid number"; 83 84 private BigDecimal minValue; 85 protected String minValueMessage; 86 private BigDecimal maxValue; 87 protected String maxValueMessage; 88 protected String invalidNumberMessage; 89 90 public NumberValidator(Map paramMap) 91 throws ServiceException 92 { 93 this(); 94 init(paramMap); 95 } 96 97 public NumberValidator() 98 { 99 invalidNumberMessage = getDefaultInvalidNumberMessage(); 100 } 101 102 110 public void init(Map paramMap) 111 throws ServiceException 112 { 113 super.init(paramMap); 114 115 minValueMessage = null; 116 maxValueMessage = null; 117 118 doInit(paramMap); 119 120 Constraint constraint = (Constraint)paramMap.get("notANumberMessage"); 121 if ( constraint != null ) 122 { 123 String param = constraint.getValue(); 124 if ( param != null && param.length() != 0 ) 125 { 126 invalidNumberMessage = param; 127 } 128 else if ( constraint.getMessage().length() != 0 ) 129 { 130 invalidNumberMessage = constraint.getMessage(); 131 } 132 } 133 } 134 135 protected void doInit(Map paramMap) 136 { 137 minValue = null; 138 maxValue = null; 139 140 Constraint constraint = (Constraint)paramMap.get("minValue"); 141 if ( constraint != null ) 142 { 143 String param = constraint.getValue(); 144 minValue = new BigDecimal (param); 145 minValueMessage = constraint.getMessage(); 146 } 147 148 constraint = (Constraint)paramMap.get("maxValue"); 149 if ( constraint != null ) 150 { 151 String param = constraint.getValue(); 152 maxValue = new BigDecimal (param); 153 maxValueMessage = constraint.getMessage(); 154 } 155 } 156 157 protected String getDefaultInvalidNumberMessage() 158 { 159 return INVALID_NUMBER; 160 } 161 162 170 protected void doAssertValidity(String testValue) 171 throws ValidationException 172 { 173 BigDecimal bd = null; 174 try 175 { 176 bd = new BigDecimal (testValue); 177 } 178 catch (RuntimeException e) 179 { 180 message = invalidNumberMessage; 181 throw new ValidationException(invalidNumberMessage); 182 } 183 184 if ( minValue != null && bd.compareTo(minValue) < 0 ) 185 { 186 message = minValueMessage; 187 throw new ValidationException(minValueMessage); 188 } 189 if ( maxValue != null && bd.compareTo(maxValue) > 0 ) 190 { 191 message = maxValueMessage; 192 throw new ValidationException(maxValueMessage); 193 } 194 } 195 196 200 204 public BigDecimal getMinValueAsBigDecimal() 205 { 206 return minValue; 207 } 208 209 213 public void setMinValue(BigDecimal v) 214 { 215 this.minValue = v; 216 } 217 218 222 public String getMinValueMessage() 223 { 224 return minValueMessage; 225 } 226 227 231 public void setMinValueMessage(String v) 232 { 233 this.minValueMessage = v; 234 } 235 236 240 public BigDecimal getMaxValueAsBigDecimal() 241 { 242 return maxValue; 243 } 244 245 249 public void setMaxValue(BigDecimal v) 250 { 251 this.maxValue = v; 252 } 253 254 258 public String getMaxValueMessage() 259 { 260 return maxValueMessage; 261 } 262 263 267 public void setMaxValueMessage(String v) 268 { 269 this.maxValueMessage = v; 270 } 271 272 276 public String getInvalidNumberMessage() 277 { 278 return invalidNumberMessage; 279 } 280 281 285 public void setInvalidNumberMessage(String v) 286 { 287 this.invalidNumberMessage = v; 288 } 289 290 } 291 | Popular Tags |