Package org.internna.iwebmvc.core.validation.annotation

Examples of org.internna.iwebmvc.core.validation.annotation.CollectionConstraint


    }

    @SuppressWarnings("unchecked")
    protected Set<ValidationError> validateCollectionConstraint(final Field field, final Object collection) {
        Set<ValidationError> errors = new HashSet<ValidationError>();
        CollectionConstraint collectionConstraint = field.getAnnotation(CollectionConstraint.class);
        if (collectionConstraint != null) {
            if (collectionConstraint.required() & (collection == null)) errors.add(new ValidationError(field.getName(), errorCodes[0], collectionConstraint, collection));
            if (collection != null) {
                if (Collection.class.isInstance(collection)) {
                    Collection<?> col = (Collection<?>) collection;
                    if (collectionConstraint.traverse()) {
                        Collection<ValidationError> deepErrors = new HashSet<ValidationError>();
                        int i = 0;
                        for (Object element : col) {
                            Collection<ValidationError> inner = this.visit(element);
                            for (ValidationError error : inner) error.addPath(field.getName() + "[" + i + "]");
                            deepErrors.addAll(inner);
                            i++;
                        }
                        errors.addAll(deepErrors);
                    }
                    if (col.size() < collectionConstraint.minLength()) errors.add(new ValidationError(field.getName(), errorCodes[2], collectionConstraint, collection));
                    if (col.size() > collectionConstraint.maxLength()) errors.add(new ValidationError(field.getName(), errorCodes[3], collectionConstraint, collection));
                } else {
                    throw new IllegalArgumentException("The annotated element is not a collection [" + collection + "]");
                }
            }
        }
View Full Code Here

TOP

Related Classes of org.internna.iwebmvc.core.validation.annotation.CollectionConstraint

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.