Comprobación de duplicados en el validador de email para
administradores.
This commit is contained in:
@@ -4,6 +4,7 @@ import java.util.Map;
|
|||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import javax.el.ValueExpression;
|
||||||
import javax.faces.application.FacesMessage;
|
import javax.faces.application.FacesMessage;
|
||||||
import javax.faces.component.UIComponent;
|
import javax.faces.component.UIComponent;
|
||||||
import javax.faces.context.FacesContext;
|
import javax.faces.context.FacesContext;
|
||||||
@@ -13,6 +14,8 @@ import javax.faces.validator.ValidatorException;
|
|||||||
|
|
||||||
import org.primefaces.validate.ClientValidator;
|
import org.primefaces.validate.ClientValidator;
|
||||||
|
|
||||||
|
import managedbean.systemAdmin.AddAdminMBean;
|
||||||
|
|
||||||
@FacesValidator("emailValidator")
|
@FacesValidator("emailValidator")
|
||||||
public class EmailValidator implements Validator<String>, ClientValidator {
|
public class EmailValidator implements Validator<String>, ClientValidator {
|
||||||
|
|
||||||
@@ -23,7 +26,7 @@ public class EmailValidator implements Validator<String>, ClientValidator {
|
|||||||
String strValue = "";
|
String strValue = "";
|
||||||
|
|
||||||
if (value != null)
|
if (value != null)
|
||||||
strValue = String.valueOf(value);
|
strValue = String.valueOf(value).toLowerCase();
|
||||||
|
|
||||||
if (strValue.equals(""))
|
if (strValue.equals(""))
|
||||||
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Correo electrónico no válido", "La dirección " + value + " no es una dirección válida"));
|
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Correo electrónico no válido", "La dirección " + value + " no es una dirección válida"));
|
||||||
@@ -31,6 +34,22 @@ public class EmailValidator implements Validator<String>, ClientValidator {
|
|||||||
Matcher matcher = EMAIL_COMPILED_PATTERN.matcher((String) value);
|
Matcher matcher = EMAIL_COMPILED_PATTERN.matcher((String) value);
|
||||||
if (!matcher.matches())
|
if (!matcher.matches())
|
||||||
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Correo electrónico no válido", "La dirección " + value + " no es una dirección válida"));
|
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Correo electrónico no válido", "La dirección " + value + " no es una dirección válida"));
|
||||||
|
|
||||||
|
boolean emailIsDupe = false;
|
||||||
|
ValueExpression valueExpr = comp.getValueExpression("managedBean");
|
||||||
|
|
||||||
|
if (valueExpr != null) {
|
||||||
|
Object managedBean = comp.getValueExpression("managedBean").getValue(context.getELContext());
|
||||||
|
|
||||||
|
if (managedBean instanceof AddAdminMBean) {
|
||||||
|
AddAdminMBean admBean = AddAdminMBean.class.cast(managedBean);
|
||||||
|
emailIsDupe = admBean.emailAlreadyRegistered(strValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (emailIsDupe == true)
|
||||||
|
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, "Correo electrónico ya registrado",
|
||||||
|
"La dirección de correo electrónico " + strValue + " ya ha sido registrada en el sistema"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> getMetadata() {
|
public Map<String, Object> getMetadata() {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package managedbean.validators;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.el.ValueExpression;
|
||||||
import javax.faces.application.FacesMessage;
|
import javax.faces.application.FacesMessage;
|
||||||
import javax.faces.component.UIComponent;
|
import javax.faces.component.UIComponent;
|
||||||
import javax.faces.context.FacesContext;
|
import javax.faces.context.FacesContext;
|
||||||
@@ -33,15 +34,19 @@ public class NifValidator implements Validator<String>, ClientValidator {
|
|||||||
if (ValidationUtils.isValid(strValue) == false)
|
if (ValidationUtils.isValid(strValue) == false)
|
||||||
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "El NIF no es válido", "El NIF " + strValue + " no es válido"));
|
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "El NIF no es válido", "El NIF " + strValue + " no es válido"));
|
||||||
|
|
||||||
Object managedBean = comp.getValueExpression("managedBean").getValue(context.getELContext());
|
|
||||||
boolean nifIsDupe = false;
|
boolean nifIsDupe = false;
|
||||||
|
ValueExpression valueExpr = comp.getValueExpression("managedBean");
|
||||||
|
|
||||||
if (managedBean instanceof RegisterUserMBean) {
|
if (valueExpr != null) {
|
||||||
RegisterUserMBean ruBean = RegisterUserMBean.class.cast(managedBean);
|
Object managedBean = valueExpr.getValue(context.getELContext());
|
||||||
nifIsDupe = ruBean.checkNIFDuplicated(strValue, ruBean.getUserType());
|
|
||||||
} else if (managedBean instanceof UpdateProfileMBean) {
|
if (managedBean instanceof RegisterUserMBean) {
|
||||||
UpdateProfileMBean upBean = UpdateProfileMBean.class.cast(managedBean);
|
RegisterUserMBean ruBean = RegisterUserMBean.class.cast(managedBean);
|
||||||
nifIsDupe = upBean.checkNIFDuplicated(strValue, upBean.getUserType(), upBean.getId());
|
nifIsDupe = ruBean.checkNIFDuplicated(strValue, ruBean.getUserType());
|
||||||
|
} else if (managedBean instanceof UpdateProfileMBean) {
|
||||||
|
UpdateProfileMBean upBean = UpdateProfileMBean.class.cast(managedBean);
|
||||||
|
nifIsDupe = upBean.checkNIFDuplicated(strValue, upBean.getUserType(), upBean.getId());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PrimeFaces.current().ajax().addCallbackParam("formattedNIF", strValue);
|
PrimeFaces.current().ajax().addCallbackParam("formattedNIF", strValue);
|
||||||
|
|||||||
Reference in New Issue
Block a user