package managedbean.validators;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import org.primefaces.validate.ClientValidator;

import managedbean.common.ValidationUtils;

@FacesValidator("nifValidator")
public class NifValidator implements Validator<String>, ClientValidator {

	public void validate(FacesContext context, UIComponent comp, String value) throws ValidatorException {
		String strValue = "";
		
		if (value != null)
			strValue = String.valueOf(value);

		if (strValue.equals(""))
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "El NIF no es válido", "El NIF " + value + " no es válido"));

		if (ValidationUtils.isValid(strValue) == false)
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "El NIF no es válido", "El NIF " + value + " no es válido"));
	}

	public Map<String, Object> getMetadata() {
		return null;
	}

	public String getValidatorId() {
		return "nifValidator";
	}
}
