/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.component.bean.validator;

import java.util.HashSet;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.ValidatorFactory;

import org.apache.camel.Exchange;
import org.apache.camel.support.DefaultProducer;

/**
 * Bean validator that uses the JSR 303 reference implementation (Hibernate Validator). Throws
 * {@link BeanValidationException} if constrain violations are detected.
 */
public class BeanValidatorProducer extends DefaultProducer {

    private ValidatorFactory validatorFactory;
    private Class<?> group;

    public BeanValidatorProducer(BeanValidatorEndpoint endpoint) {
        super(endpoint);
    }

    @Override
    public void process(Exchange exchange) throws Exception {
        Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>();

        if (exchange.getIn().getBody() instanceof Iterable) {
            Iterable<?> body = exchange.getIn().getBody(Iterable.class);
            body.forEach(b -> constraintViolations.addAll(getConstraintViolationsForSingleBean(b, this.group)));
        } else {
            constraintViolations.addAll(getConstraintViolationsForSingleBean(exchange.getIn().getBody(), this.group));
        }

        if (!constraintViolations.isEmpty()) {
            throw new BeanValidationException(exchange, constraintViolations, exchange.getIn().getBody());
        }
    }

    public ValidatorFactory getValidatorFactory() {
        return validatorFactory;
    }

    public void setValidatorFactory(ValidatorFactory validatorFactory) {
        this.validatorFactory = validatorFactory;
    }

    public Class<?> getGroup() {
        return group;
    }

    public void setGroup(Class<?> group) {
        this.group = group;
    }

    private Set<ConstraintViolation<Object>> getConstraintViolationsForSingleBean(Object bean, Class<?> group) {
        Set<ConstraintViolation<Object>> constraintViolations;

        if (this.group != null) {
            constraintViolations = validatorFactory.getValidator().validate(bean, group);
        } else {
            constraintViolations = validatorFactory.getValidator().validate(bean);
        }

        return constraintViolations;
    }

}
