Package br.com.caelum.stella.validation.ie

Source Code of br.com.caelum.stella.validation.ie.IERioGrandeDoSulValidator

package br.com.caelum.stella.validation.ie;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import br.com.caelum.stella.DigitoPara;
import br.com.caelum.stella.MessageProducer;
import br.com.caelum.stella.SimpleMessageProducer;
import br.com.caelum.stella.ValidationMessage;
import br.com.caelum.stella.validation.BaseValidator;
import br.com.caelum.stella.validation.InvalidValue;
import br.com.caelum.stella.validation.Validator;
import br.com.caelum.stella.validation.error.IEError;

public class IERioGrandeDoSulValidator implements Validator<String> {

    private final boolean isFormatted;

    public static final Pattern FORMATED = Pattern.compile("[0-4]\\d{2}\\/\\d{7}");

    public static final Pattern UNFORMATED = Pattern.compile("([0-4]\\d{2})\\d{7}");

    /**
     * Este considera, por padrão, que as cadeias estão formatadas e utiliza um
     * {@linkplain SimpleMessageProducer} para geração de mensagens.
     */
    public IERioGrandeDoSulValidator() {
        this(true);
    }

    /**
     * O validador utiliza um {@linkplain SimpleMessageProducer} para geração de
     * mensagens.
     *
     * @param isFormatted
     *            considerar cadeia formatada quando <code>true</code>
     */
    public IERioGrandeDoSulValidator(boolean isFormatted) {
        this.baseValidator = new BaseValidator();
        this.isFormatted = isFormatted;
    }

    public IERioGrandeDoSulValidator(MessageProducer messageProducer, boolean isFormatted) {
        this.baseValidator = new BaseValidator(messageProducer);
        this.isFormatted = isFormatted;
    }

    private List<InvalidValue> getInvalidValues(String IE) {
        List<InvalidValue> errors = new ArrayList<InvalidValue>();
        errors.clear();
        if (IE != null) {
            String unformatedIE = checkForCorrectFormat(IE, errors);
            if (errors.isEmpty()) {
                if (!hasValidCheckDigits(unformatedIE)) {
                    errors.add(IEError.INVALID_CHECK_DIGITS);
                }
                if (!hasValidMunicipality(unformatedIE)) {
                    errors.add(IEError.INVALID_MUNICIPALITY);
                }
            }
        }
        return errors;
    }

    private boolean hasValidMunicipality(String unformatedIE) {
        String municipality = null;
        Matcher matcher = UNFORMATED.matcher(unformatedIE);
        if (matcher.matches()) {
            municipality = matcher.replaceAll("$1");
        }
        int municipalityInt = Integer.parseInt(municipality);
        boolean result = municipalityInt > 0 && municipalityInt <= 467;
        return result;
    }

    private String checkForCorrectFormat(String ie, List<InvalidValue> errors) {
        String unformatedIE = null;
        if (isFormatted) {
            if (!(FORMATED.matcher(ie).matches())) {
                errors.add(IEError.INVALID_FORMAT);
            }
            unformatedIE = ie.replaceAll("\\D", "");
        } else {
            if (!UNFORMATED.matcher(ie).matches()) {
                errors.add(IEError.INVALID_DIGITS);
            }
            unformatedIE = ie;
        }
        return unformatedIE;
    }

    protected boolean hasValidCheckDigits(String unformattedIE) {
    String iESemDigito = unformattedIE.substring(0, unformattedIE.length() - 1);
    String digito = unformattedIE.substring(unformattedIE.length() - 1);

    String digitoCalculado = calculaDigito(iESemDigito);

    return digito.equals(digitoCalculado);
    }

  private String calculaDigito(String iESemDigito) {
    DigitoPara digitoPara = new DigitoPara(iESemDigito);
    digitoPara.complementarAoModulo().trocandoPorSeEncontrar("0", 10, 11);

    return digitoPara.calcula();
  }

    public boolean isEligible(String value) {
        boolean result;
        if (isFormatted) {
            result = FORMATED.matcher(value).matches();
        } else {
            result = UNFORMATED.matcher(value).matches();
        }
        return result;
    }

    private final BaseValidator baseValidator;

    public void assertValid(String cpf) {
        baseValidator.assertValid(getInvalidValues(cpf));
    }

    public List<ValidationMessage> invalidMessagesFor(String cpf) {
        return baseValidator.generateValidationMessages(getInvalidValues(cpf));
    }
}
TOP

Related Classes of br.com.caelum.stella.validation.ie.IERioGrandeDoSulValidator

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.