001: /*
002: * $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/main/java/nl/knowlogy/validation/validators/MaxSizeValidator.java,v 1.8 2007/10/31 16:34:58 roberthofstra Exp $
003: * $Revision: 1.8 $
004: * $Date: 2007/10/31 16:34:58 $
005: *
006: *
007: * Created on Oct 6, 2004
008: *
009: * All right reserved(c) 2004, Knowlogy
010: *
011: * Copyright 2004 - 2005 Knowlogy, the Netherlands. All rights reserved.
012: * All Knowlogy brand and product names are trademarks or registered trademarks
013: * of Knowlogy in the Netherlands and other countries.
014: *
015: * No part of this publication may be reproduced, transmitted, stored in a retrieval system,
016: * or translated into any human or computer language, in any form, or by any means, electronic,
017: * mechanical, magnetic, optical, chemical, manual, or otherwise,
018: * without the prior written permission of the copyright owner, Knowlogy.
019: *
020: */
021: package nl.knowlogy.validation.validators;
022:
023: import nl.knowlogy.validation.ErrorCodes;
024: import nl.knowlogy.validation.MessageType;
025: import nl.knowlogy.validation.Messages;
026: import nl.knowlogy.validation.PropertyMetadataSuplier;
027: import nl.knowlogy.validation.metadata.PropertyMetadata;
028:
029: /**
030: *
031: * @author Robert
032: */
033: public class MaxSizeValidator extends NumberValidator implements
034: PropertyMetadataSuplier {
035:
036: private Long maxSize;
037:
038: /**
039: *
040: */
041: public MaxSizeValidator(String propertyName, Long maxSize) {
042: super (propertyName);
043: this .maxSize = maxSize;
044: }
045:
046: public MaxSizeValidator(String propertyName, Long maxSize,
047: String errorCode) {
048: this (propertyName, maxSize);
049: setErrorCode(errorCode);
050: }
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see nl.knowlogy.validation.validators.BasePropertyValidator#getDefaultErrorCode()
056: */
057: public String getDefaultErrorCode() {
058: return ErrorCodes.INVALID_MAX_SIZE;
059: }
060:
061: /*
062: * (non-Javadoc)
063: *
064: * @see nl.knowlogy.validation.metadata.PropertyValidation#doValidatePropertyValue(java.lang.Object,
065: * nl.knowlogy.validation.errors.Errors)
066: */
067: // public void doValidatePropertyValue(Object toValidate, Object propertyValue, Messages errors) {
068: //
069: // if (propertyValue instanceof Integer) {
070: // Integer value = (Integer) propertyValue;
071: //
072: // if ((value != null) && (value.intValue() > maxSize.longValue())) {
073: // Object[] errorArg = new Object[2];
074: // errorArg[0] = new Long(maxSize.longValue());
075: // errorArg[1] = new Integer(value.intValue());
076: // errors.addPropertyMessage(MessageType.ERROR, toValidate, getPropertyName(), getErrorCode(),errorArg, null);
077: // }
078: // } else if (propertyValue instanceof Long) {
079: // Long value = (Long) propertyValue;
080: // Object[] errorArg = new Object[2];
081: // errorArg[0] = new Long(maxSize.longValue());
082: // errorArg[1] = new Long(value.longValue());
083: // if ((value != null) && (value.longValue() > maxSize.longValue())) {
084: // errors.addPropertyMessage(MessageType.ERROR, toValidate, getPropertyName(), getErrorCode(), errorArg, null);
085: //
086: // }
087: // }
088: //
089: // }
090: //
091: protected void doValidateNumber(Object toValidate, Number value,
092: Messages messages) {
093: if ((value.longValue() > maxSize.longValue())) {
094: Object[] errorArg = new Object[2];
095: errorArg[0] = new Long(maxSize.longValue());
096: errorArg[1] = new Long(value.longValue());
097: messages.addPropertyMessage(MessageType.ERROR, toValidate,
098: getPropertyName(), getErrorCode(), errorArg, null);
099: }
100: }
101:
102: public void supplyMetaData(PropertyMetadata propertyMetadata) {
103: propertyMetadata.setMaxSize(this .maxSize);
104: }
105:
106: public String toString() {
107: return "MaxSizeValidator, maxSize=[" + maxSize + "]"
108: + super.toString();
109: }
110:
111: }
|