Package br.com.caelum.stella.hibernate.validator

Source Code of br.com.caelum.stella.hibernate.validator.StellaCPFValidator

package br.com.caelum.stella.hibernate.validator;

import org.hibernate.validator.Validator;

import br.com.caelum.stella.validation.CPFValidator;

/**
* Valida a cadeia gerada através do método {@linkplain #toString()} para
* verificar se ela está de acordo com o padrĂ£o de CPF.
*
* @author Fabio Kung
* @author Leonardo Bessa
*/
public class StellaCPFValidator implements Validator<CPF> {
    private CPFValidator stellaValidator;

    /**
     * @see org.hibernate.validator.Validator#initialize(java.lang.annotation.Annotation)
     */
    public void initialize(CPF cpf) {
        AnnotationMessageProducer messageProducer = new AnnotationMessageProducer(cpf);
        stellaValidator = new CPFValidator(messageProducer, cpf.formatted());
    }

    /**
     * @see org.hibernate.validator.Validator#isValid(java.lang.Object)
     */
    public boolean isValid(Object o) {
        if (o != null) {
            String cpf = o.toString();
            if (cpf.trim().length() == 0) {
                return true;
            } else {
                return stellaValidator.invalidMessagesFor(cpf).isEmpty();
            }
        } else {
            return true;
        }
    }
}
TOP

Related Classes of br.com.caelum.stella.hibernate.validator.StellaCPFValidator

TOP
Copyright © 2018 www.massapi.com. 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.