com.titilink.camel.rest.util.ValidationUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.titilink.camel.rest.util.ValidationUtil.java

Source

/**
 * Copyright 2005-2015 titilink
 *
 * The contents of this file are subject to the terms of one of the following
 * open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
 * 1.0 (the "Licenses"). You can select the license that you prefer but you may
 * not use this file except in compliance with one of these Licenses.
 *
 * You can obtain a copy of the Apache 2.0 license at
 * http://www.opensource.org/licenses/apache-2.0
 *
 * You can obtain a copy of the LGPL 3.0 license at
 * http://www.opensource.org/licenses/lgpl-3.0
 *
 * You can obtain a copy of the LGPL 2.1 license at
 * http://www.opensource.org/licenses/lgpl-2.1
 *
 * You can obtain a copy of the CDDL 1.0 license at
 * http://www.opensource.org/licenses/cddl1
 *
 * You can obtain a copy of the EPL 1.0 license at
 * http://www.opensource.org/licenses/eclipse-1.0
 *
 * See the Licenses for the specific language governing permissions and
 * limitations under the Licenses.
 *
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly at
 * https://github.com/titilink/titilink-framework
 *
 * titilink is a registered trademark of titilink.inc
 */
package com.titilink.camel.rest.util;

import com.titilink.camel.rest.common.RootResponse;
import org.apache.commons.lang.StringUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.HashSet;
import java.util.Set;

/**
 * 
 * <p>
 * @author by kam
 * @date 2015/05/01
 * @since v1.0.0
 */
public final class ValidationUtil {

    /**
     *  message?
     */
    public final static String MESSAGE_TEMPLATE_SEPARATOR = "##";

    private static <T> Set<ConstraintViolation<T>> validateBean(T instance, Class<?>... groups) {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<T>> result = validator.validate(instance, groups);
        return result;
    }

    /**
     * ??JSR-303bean
     *
     * @param instance 
     * @return
     */
    public static <T> Set<String> validate(T instance, Class<?>... groups) {
        Set<ConstraintViolation<T>> set = validateBean(instance, groups);

        Set<String> errorCodeSet = new HashSet<String>(set.size());
        for (ConstraintViolation<T> c : set) {
            errorCodeSet.add(c.getMessage());
        }

        return errorCodeSet;
    }

    /**
     * ??JSR-303bean
     *
     * @param instance 
     * @return
     */
    public static <T> Set<RootResponse> validateExtend(T instance, Class<?>... groups) {
        Set<ConstraintViolation<T>> set = validateBean(instance, groups);

        if (set.size() == 0) {
            return null;
        }

        Set<RootResponse> errorBoxes = new HashSet<>(set.size());
        for (ConstraintViolation<T> c : set) {
            String message = c.getMessage();
            if (StringUtils.isNotEmpty(message)) {
                String[] item = message.split(MESSAGE_TEMPLATE_SEPARATOR);
                errorBoxes.add(new RootResponse(item[0], item[1]));
            }
        }
        return errorBoxes;
    }

    /**
     * 
     */
    private ValidationUtil() {
        // nothing
    }

}