Mejora en validador de NIF, ahora comprueba duplicados sin antes de
guardar el formulario.
This commit is contained in:
@@ -67,9 +67,12 @@
|
||||
<div class="ui-g-4 ui-md-4">
|
||||
<div class="ui-inputgroup">
|
||||
<p:inputText id="nif" widgetVar="nif" value="#{RegisterUser.nif}" validator="nifValidator" maxlength="20" required="true" requiredMessage="Por favor, indque su NIF">
|
||||
<p:ajax event="blur" update="nifmsg" listener="#{RegisterUser.handleNIFValueChange}" oncomplete="handleNIFResponse(xhr, status, args)" />
|
||||
<f:validator validatorId="nifValidator" for="nif">
|
||||
<f:attribute name="managedBean" value="#{RegisterUser}" />
|
||||
</f:validator>
|
||||
<p:ajax event="change" update="nifmsg" oncomplete="handleNIFResponse(xhr, status, args)" />
|
||||
</p:inputText>
|
||||
<p:commandButton widgetVar="nifButton" icon="pi pi-times" styleClass="red-button" />
|
||||
<p:commandButton id="nifButton" widgetVar="nifButton" onclick="nifCheckClick();" icon="pi pi-check" styleClass="green-button" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="ui-g-6 ui-md-6">
|
||||
|
||||
@@ -42,13 +42,16 @@
|
||||
<div class="ui-g-4 ui-md-4">
|
||||
<div class="ui-inputgroup">
|
||||
<p:inputText id="nif" widgetVar="nif" value="#{UpdateProfile.nif}" validator="nifValidator" maxlength="20" required="true" requiredMessage="Por favor, indque su NIF">
|
||||
<p:ajax event="blur" update="nifmsg" listener="#{UpdateProfile.handleNIFValueChange}" oncomplete="handleNIFResponse(xhr, status, args)" />
|
||||
<f:validator validatorId="nifValidator" for="nif">
|
||||
<f:attribute name="managedBean" value="#{UpdateProfile}" />
|
||||
</f:validator>
|
||||
<p:ajax event="change" update="nifmsg" oncomplete="handleNIFResponse(xhr, status, args)" />
|
||||
</p:inputText>
|
||||
<p:commandButton widgetVar="nifButton" icon="pi pi-check" styleClass="green-button" />
|
||||
<p:commandButton id="nifButton" widgetVar="nifButton" onclick="nifCheckClick();" icon="pi pi-check" styleClass="green-button" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="ui-g-6 ui-md-6">
|
||||
<p:message id="nifmsg" for="nif" display="text" />
|
||||
<p:message id="nifmsg" for="nif" />
|
||||
</div>
|
||||
|
||||
<div class="ui-g-2 ui-md-2">
|
||||
|
||||
@@ -22,15 +22,27 @@ function startLogin() {
|
||||
PF('btnLogin').disable();
|
||||
}
|
||||
|
||||
function nifCheckClick() {
|
||||
var nif = PF('nif');
|
||||
nif.jq.change();
|
||||
}
|
||||
|
||||
// Actualiza la interfaz tras validar si un nif está duplicado.
|
||||
function handleNIFResponse(xhr, status, args) {
|
||||
var isValid = false;
|
||||
|
||||
if (typeof args.validationFailed != "undefined")
|
||||
isValid = !args.validationFailed;
|
||||
else if (typeof args.NIFisValid != "undefined")
|
||||
isValid = args.NIFisValid;
|
||||
|
||||
if (args.formattedNIF) {
|
||||
var nif = PF('nif');
|
||||
nif.jq.val(args.formattedNIF);
|
||||
}
|
||||
|
||||
var nifButton = PF('nifButton');
|
||||
if (args.NIFisDupe == false) {
|
||||
if (isValid == true) {
|
||||
nifButton.jq.children(".ui-icon").removeClass("pi pi-times");
|
||||
nifButton.jq.removeClass('red-button');
|
||||
|
||||
|
||||
@@ -16,8 +16,12 @@ public class ValidationUtils {
|
||||
static final String NIE_LETTERS = "XYZ";
|
||||
|
||||
public static String normalizeNIF(String nif) {
|
||||
if (nif == null)
|
||||
return nif;
|
||||
else
|
||||
return nif.toUpperCase().replace("-", "").replace(".", "");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param nif NIF a validar
|
||||
@@ -57,17 +61,13 @@ public class ValidationUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Comprueba si un NIF existe para un tipo de usuario. * No se permite que el
|
||||
* mismo NIF esté registrado como médico de familia y como especialista. * Se
|
||||
* permite que el mismo NIF esté registrado como paciente y como médico (de
|
||||
* familia o especialista)
|
||||
* Comprueba si un NIF existe para un tipo de usuario. * No se permite que el mismo NIF esté registrado como médico de familia y como especialista. * Se permite que el mismo
|
||||
* NIF esté registrado como paciente y como médico (de familia o especialista)
|
||||
*
|
||||
* @param remoteSvc Servicio Remoto para recuperar datos.
|
||||
* @param userType Tipo de usuario a comprobar
|
||||
* @param nif NIF a comprobar.
|
||||
* @param id Parámetro opcional, id del usuario actual (si encuentra el
|
||||
* mismo nif y el usuario coincide no se devuelve la
|
||||
* coincidencia)
|
||||
* @param id Parámetro opcional, id del usuario actual (si encuentra el mismo nif y el usuario coincide no se devuelve la coincidencia)
|
||||
* @return true si el NIF ya estaba registrado para un usuario diferente.
|
||||
*/
|
||||
public static boolean checkIfNifAlreadyRegistered(CommonFacadeRemote remoteSvc, UserType userType, String nif, Integer id) {
|
||||
@@ -90,7 +90,7 @@ public class ValidationUtils {
|
||||
|
||||
if (sd != null && (id == null || sd.getId().equals(id) == false))
|
||||
nifExists = true;
|
||||
} else if (id == null || fd.getId().equals(id) == false )
|
||||
} else if (id == null || fd.getId().equals(id) == false)
|
||||
// Si se trata de un usuario diferente, entonces está repetido
|
||||
nifExists = true;
|
||||
|
||||
|
||||
@@ -34,7 +34,6 @@ import managedbean.common.ValidationUtils;
|
||||
public class RegisterUserMBean extends ManagedBeanBase implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private int id;
|
||||
private String cipCode;
|
||||
private String nif;
|
||||
private String name;
|
||||
@@ -156,22 +155,12 @@ public class RegisterUserMBean extends ManagedBeanBase implements Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gestióna el evento de modficación del NIF del usuario actual. Búsca si el NIF ya está en uso para otro usuario. Solo se permite que el mismo NIF se refistro como paciente y
|
||||
* como médico (especialista o de familia, pero no como ambos).
|
||||
* Búsca si el NIF ya está en uso para otro usuario. Solo se permite que el mismo NIF se refistro como paciente y como médico (especialista o de familia, pero no como ambos).
|
||||
*
|
||||
* Un Médico puede estar registrado como paciente con el mismo NIF, pero como médico de familia y especialista al mismo tiempo.
|
||||
*/
|
||||
public void handleNIFValueChange() {
|
||||
boolean isDupe = false;
|
||||
this.nif = ValidationUtils.normalizeNIF(this.nif);
|
||||
|
||||
if (ValidationUtils.checkIfNifAlreadyRegistered(this.getRemoteManagerCommon(), this.userType, this.nif, null) == true) {
|
||||
isDupe = true;
|
||||
this.addFacesMessage("frmRegisterUser:nif", FacesMessage.SEVERITY_WARN, "NIF duplicado", "El nif indicado pertenece a otro usuario previamente registrado");
|
||||
}
|
||||
|
||||
PrimeFaces.current().ajax().addCallbackParam("NIFisDupe", isDupe);
|
||||
PrimeFaces.current().ajax().addCallbackParam("formattedNIF", this.nif);
|
||||
public boolean checkNIFDuplicated(String nifValue, UserType userType) {
|
||||
return ValidationUtils.checkIfNifAlreadyRegistered(this.getRemoteManagerCommon(), userType, nifValue, null);
|
||||
}
|
||||
|
||||
public boolean isPatient() {
|
||||
@@ -263,7 +252,7 @@ public class RegisterUserMBean extends ManagedBeanBase implements Serializable {
|
||||
this.addFacesMessage(FacesMessage.SEVERITY_WARN, "El NIF indicado no es válido", "Por favor, especifique un NIF válido.");
|
||||
error++;
|
||||
}
|
||||
if (ValidationUtils.checkIfNifAlreadyRegistered(this.getRemoteManagerCommon(), this.userType, this.nif, null) == true) {
|
||||
if (this.checkNIFDuplicated(this.nif, this.userType) == true) {
|
||||
this.addFacesMessage("frmRegisterUser:nif", FacesMessage.SEVERITY_WARN, "NIF duplicado", "El nif indicado pertenece a otro usuario previamente registrado");
|
||||
error++;
|
||||
}
|
||||
@@ -272,17 +261,17 @@ public class RegisterUserMBean extends ManagedBeanBase implements Serializable {
|
||||
try {
|
||||
switch (this.userType) {
|
||||
case PATIENT:
|
||||
PatientTO pat = this.getRemoteManagerProfile().registerPatient(id, nif, name, surname, password, email);
|
||||
PatientTO pat = this.getRemoteManagerProfile().registerPatient(nif, name, surname, password, email);
|
||||
this.cipCode = pat.getPersonalIdentificationCode();
|
||||
|
||||
break;
|
||||
case FAMILY_DOCTOR:
|
||||
FamilyDoctorTO fd = this.getRemoteManagerProfile().registerFamilyDoctor(id, nif, name, surname, password, email, this.primaryHealthCareCenter);
|
||||
FamilyDoctorTO fd = this.getRemoteManagerProfile().registerFamilyDoctor(nif, name, surname, password, email, this.primaryHealthCareCenter);
|
||||
this.cipCode = fd.getProfessionalNumber();
|
||||
|
||||
break;
|
||||
case SPECIALIST_DOCTOR:
|
||||
SpecialistDoctorTO sd = this.getRemoteManagerProfile().registerSpecialistDoctor(id, nif, name, surname, password, email, this.medicalSpecialty);
|
||||
SpecialistDoctorTO sd = this.getRemoteManagerProfile().registerSpecialistDoctor(nif, name, surname, password, email, this.medicalSpecialty);
|
||||
this.cipCode = sd.getProfessionalNumber();
|
||||
|
||||
break;
|
||||
|
||||
@@ -10,8 +10,6 @@ import javax.faces.view.ViewScoped;
|
||||
import javax.inject.Named;
|
||||
import javax.resource.NotSupportedException;
|
||||
|
||||
import org.primefaces.PrimeFaces;
|
||||
|
||||
import TO.FamilyDoctorTO;
|
||||
import TO.LoggedUserTO;
|
||||
import TO.MedicalSpecialtyTO;
|
||||
@@ -206,22 +204,12 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable
|
||||
}
|
||||
|
||||
/**
|
||||
* Gestióna el evento de modficación del NIF del usuario actual. Búsca si el NIF ya está en uso para otro usuario. Solo se permite que el mismo NIF se refistro como paciente y
|
||||
* como médico (especialista o de familia, pero no como ambos).
|
||||
* Búsca si el NIF ya está en uso para otro usuario. Solo se permite que el mismo NIF se refistro como paciente y como médico (especialista o de familia, pero no como ambos).
|
||||
*
|
||||
* Un Médico puede estar registrado como paciente con el mismo NIF, pero como médico de familia y especialista al mismo tiempo.
|
||||
*/
|
||||
public void handleNIFValueChange() {
|
||||
boolean isDupe = false;
|
||||
this.nif = ValidationUtils.normalizeNIF(this.nif);
|
||||
|
||||
if (ValidationUtils.checkIfNifAlreadyRegistered(this.getRemoteManagerCommon(), this.userType, this.nif, this.id) == true) {
|
||||
isDupe = true;
|
||||
this.addFacesMessage("frmUpdateProfile:nif", FacesMessage.SEVERITY_WARN, "NIF duplicado", "El nif indicado pertenece a otro usuario previamente registrado");
|
||||
}
|
||||
|
||||
PrimeFaces.current().ajax().addCallbackParam("NIFisDupe", isDupe);
|
||||
PrimeFaces.current().ajax().addCallbackParam("formattedNIF", this.nif);
|
||||
public boolean checkNIFDuplicated(String nifValue, UserType userType, Integer userId) {
|
||||
return ValidationUtils.checkIfNifAlreadyRegistered(this.getRemoteManagerCommon(), userType, nifValue, userId);
|
||||
}
|
||||
|
||||
public List<FamilyDoctorTO> getFamilyDoctorList() {
|
||||
@@ -304,12 +292,11 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable
|
||||
}
|
||||
|
||||
/**
|
||||
* Método que guarda los datos realizados en la modificación del perfil.
|
||||
* Tiene en cuenta el tipo de usuario que está editando su perfil.
|
||||
* Método que guarda los datos realizados en la modificación del perfil. Tiene en cuenta el tipo de usuario que está editando su perfil.
|
||||
*
|
||||
* Si el usuario es un médico de familia requiere que se seleccione un CAP para el usuario.
|
||||
* Si el usuario es un médico especialista requiere que se seleccione una especialidad médica para el usuario.
|
||||
* Se comprueba que el NIF especificado sea válido y no pertenezca a otro usuario registrado (ver relgas de NIF permitidos para usuarios en el método handleNIFValueChange)
|
||||
* Si el usuario es un médico de familia requiere que se seleccione un CAP para el usuario. Si el usuario es un médico especialista requiere que se seleccione una especialidad
|
||||
* médica para el usuario. Se comprueba que el NIF especificado sea válido y no pertenezca a otro usuario registrado (ver relgas de NIF permitidos para usuarios en el método
|
||||
* handleNIFValueChange)
|
||||
*
|
||||
* Si se especifica una nueva contraseña, entonces se realiz un cambio de contraseña, y se requiere además: Que la nueva contraseña sea diferente a la anterior.
|
||||
*
|
||||
|
||||
@@ -9,9 +9,12 @@ import javax.faces.validator.FacesValidator;
|
||||
import javax.faces.validator.Validator;
|
||||
import javax.faces.validator.ValidatorException;
|
||||
|
||||
import org.primefaces.PrimeFaces;
|
||||
import org.primefaces.validate.ClientValidator;
|
||||
|
||||
import managedbean.common.ValidationUtils;
|
||||
import managedbean.profile.RegisterUserMBean;
|
||||
import managedbean.profile.UpdateProfileMBean;
|
||||
|
||||
@FacesValidator("nifValidator")
|
||||
public class NifValidator implements Validator<String>, ClientValidator {
|
||||
@@ -26,8 +29,28 @@ public class NifValidator implements Validator<String>, ClientValidator {
|
||||
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "El NIF no es válido", "El NIF " + strValue + " no es válido"));
|
||||
|
||||
strValue = ValidationUtils.normalizeNIF(strValue);
|
||||
|
||||
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"));
|
||||
|
||||
Object managedBean = comp.getValueExpression("managedBean").getValue(context.getELContext());
|
||||
boolean nifIsDupe = false;
|
||||
|
||||
if (managedBean instanceof RegisterUserMBean) {
|
||||
RegisterUserMBean ruBean = RegisterUserMBean.class.cast(managedBean);
|
||||
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);
|
||||
|
||||
if (nifIsDupe == true)
|
||||
throw new ValidatorException(
|
||||
new FacesMessage(FacesMessage.SEVERITY_ERROR, "El NIF está duplicado", "El NIF " + strValue + " pertenece a otro usuario previamente registrado"));
|
||||
|
||||
PrimeFaces.current().ajax().addCallbackParam("NIFisValid", true);
|
||||
}
|
||||
|
||||
public Map<String, Object> getMetadata() {
|
||||
|
||||
Reference in New Issue
Block a user