package utils;

import beans.ClienteFacadeLocal;
import entities.Cliente;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;


@ManagedBean
@RequestScoped
public class UniqueRutValidator implements Validator {
  @EJB
  private ClienteFacadeLocal clienteFacade;

  @Override
  public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException {
    Cliente cliente = clienteFacade.findByRut(o.toString());
    
    if (cliente != null) {
      FacesMessage message = new FacesMessage("Rut: Ya existe un usuario con ese rut");
      message.setSeverity(FacesMessage.SEVERITY_ERROR);
      throw new ValidatorException(message);
    }
  }
}
