001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.item.validator;
018
019import java.util.Collection;
020
021import org.springframework.beans.factory.InitializingBean;
022import org.springframework.util.Assert;
023import org.springframework.validation.BeanPropertyBindingResult;
024import org.springframework.validation.BindException;
025import org.springframework.validation.Errors;
026
027/**
028 * Adapts the {@link org.springframework.validation.Validator} interface to
029 * {@link org.springframework.batch.item.validator.Validator}.
030 * 
031 * @author Tomas Slanina
032 * @author Robert Kasanicky
033 */
034public class SpringValidator<T> implements Validator<T>, InitializingBean {
035
036        private org.springframework.validation.Validator validator;
037
038        /**
039         * @see Validator#validate(Object)
040         */
041    @Override
042        public void validate(T item) throws ValidationException {
043
044                if (!validator.supports(item.getClass())) {
045                        throw new ValidationException("Validation failed for " + item + ": " + item.getClass().getName()
046                                        + " class is not supported by validator.");
047                }
048
049                BeanPropertyBindingResult errors = new BeanPropertyBindingResult(item, "item");
050
051                validator.validate(item, errors);
052
053                if (errors.hasErrors()) {
054                        throw new ValidationException("Validation failed for " + item + ": " + errorsToString(errors), new BindException(errors));
055                }
056        }
057
058        /**
059         * @return string of field errors followed by global errors.
060         */
061        private String errorsToString(Errors errors) {
062                StringBuilder builder = new StringBuilder();
063
064                appendCollection(errors.getFieldErrors(), builder);
065                appendCollection(errors.getGlobalErrors(), builder);
066
067                return builder.toString();
068        }
069
070        /**
071         * Append the string representation of elements of the collection (separated
072         * by new lines) to the given StringBuilder.
073         */
074        private void appendCollection(Collection<?> collection, StringBuilder builder) {
075                for (Object value : collection) {
076                        builder.append("\n");
077                        builder.append(value.toString());
078                }
079        }
080
081        public void setValidator(org.springframework.validation.Validator validator) {
082                this.validator = validator;
083        }
084
085    @Override
086        public void afterPropertiesSet() throws Exception {
087                Assert.notNull(validator, "validator must be set");
088
089        }
090}