01: package nl.knowlogy.validation.validators;
02:
03: import nl.knowlogy.validation.ErrorCodes;
04: import nl.knowlogy.validation.MessageType;
05: import nl.knowlogy.validation.Messages;
06: import nl.knowlogy.validation.PropertyMetadataSuplier;
07: import nl.knowlogy.validation.metadata.PropertyMetadata;
08:
09: public class RangeValidator extends NumberValidator implements
10: PropertyMetadataSuplier {
11:
12: private Long minSize;
13: private Long maxSize;
14:
15: /**
16: *
17: */
18: public RangeValidator(String propertyName, Long minSize,
19: Long maxSize) {
20: super (propertyName);
21: this .minSize = minSize;
22: this .maxSize = maxSize;
23: }
24:
25: public RangeValidator(String propertyName, Long minSize,
26: Long maxSize, String errorCode) {
27: this (propertyName, minSize, maxSize);
28: setErrorCode(errorCode);
29: }
30:
31: /*
32: * (non-Javadoc)
33: *
34: * @see nl.knowlogy.validation.validators.BasePropertyValidator#getDefaultErrorCode()
35: */
36: public String getDefaultErrorCode() {
37: return ErrorCodes.INVALID_RANGE;
38: }
39:
40: private Object[] createMessageArg(long actualValue) {
41: Object[] messageArg = new Object[3];
42: messageArg[0] = new Long(minSize.longValue());
43: messageArg[1] = new Long(maxSize.longValue());
44: messageArg[2] = new Long(actualValue);
45: return messageArg;
46: }
47:
48: /*
49: * (non-Javadoc)
50: *
51: * @see nl.knowlogy.validation.metadata.PropertyValidation#doValidatePropertyValue(java.lang.Object,
52: * nl.knowlogy.validation.errors.Errors)
53: */
54: // public void doValidatePropertyValue(Object toValidate, Object propertyValue, Messages errors) {
55: //
56: // if (propertyValue instanceof Integer) {
57: // Integer value = (Integer) propertyValue;
58: //
59: // if ( value != null && (value.intValue() < minSize.longValue() || value.intValue() > maxSize.longValue())) {
60: // Object[] errorArg = createMessageArg(value.intValue());
61: // errors.addPropertyMessage(MessageType.ERROR, toValidate, getPropertyName(), getErrorCode(),errorArg, null);
62: // }
63: // } else if (propertyValue instanceof Long) {
64: // Long value = (Long) propertyValue;
65: // if (value != null && ( value.longValue() < minSize.longValue() || value.longValue() > maxSize.longValue())) {
66: //
67: // Object[] errorArg = createMessageArg(value.longValue());
68: // errors.addPropertyMessage(MessageType.ERROR, toValidate, getPropertyName(), getErrorCode(), errorArg, null);
69: //
70: // }
71: // }
72: // }
73: protected void doValidateNumber(Object toValidate, Number value,
74: Messages messages) {
75: if ((value.longValue() < minSize.longValue() || value
76: .longValue() > maxSize.longValue())) {
77:
78: Object[] errorArg = createMessageArg(value.longValue());
79: messages.addPropertyMessage(MessageType.ERROR, toValidate,
80: getPropertyName(), getErrorCode(), errorArg, null);
81:
82: }
83: }
84:
85: public void supplyMetaData(PropertyMetadata propertyMetadata) {
86: propertyMetadata.setMaxSize(this .maxSize);
87: propertyMetadata.setMinSize(this .minSize);
88: }
89:
90: public String toString() {
91: return "MaxSizeValidator, maxSize=[" + maxSize + "]"
92: + super.toString();
93: }
94: }
|