Añadidos custom Validators de JSF para validar NIF y email.

Configurada validación en cliente de email.
This commit is contained in:
Marcos Garcia Nuñez
2019-12-12 00:18:35 +01:00
parent 28b1192036
commit f63ba35621
7 changed files with 122 additions and 40 deletions

View File

@@ -82,10 +82,10 @@
<p:outputLabel value="NIF:" for="nif" /> <p:outputLabel value="NIF:" for="nif" />
</div> </div>
<div class="ui-g-4 ui-md-4"> <div class="ui-g-4 ui-md-4">
<p:inputText id="nif" value="#{RegisterUser.nif}" required="true" requiredMessage="Por favor, indque su NIF" /> <p:inputText id="nif" value="#{RegisterUser.nif}" validator="nifValidator" required="true" requiredMessage="Por favor, indque su NIF" />
</div> </div>
<div class="ui-g-6 ui-md-6"> <div class="ui-g-6 ui-md-6">
<p:message for="nif" display="text" /> <p:message for="nif" display="text" showDetail="true" showSummary="true" />
</div> </div>
<div class="ui-g-2 ui-md-2"> <div class="ui-g-2 ui-md-2">
@@ -112,10 +112,13 @@
<p:outputLabel value="Correo electrónico:" for="email" /> <p:outputLabel value="Correo electrónico:" for="email" />
</div> </div>
<div class="ui-g-4 ui-md-4"> <div class="ui-g-4 ui-md-4">
<p:inputText id="email" value="#{RegisterUser.email}" required="true" requiredMessage="Por favor, especifique su correo electrónico" /> <p:inputText id="email" value="#{RegisterUser.email}" required="true" validator="emailValidator" validatorMessage="La dirección #{RegisterUser.email} no es válida"
requiredMessage="Por favor, especifique su correo electrónico">
<f:validateRegex pattern="[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
</p:inputText>
</div> </div>
<div class="ui-g-6 ui-md-6"> <div class="ui-g-6 ui-md-6">
<p:message for="email" /> <p:message for="email" display="text" showDetail="true" showSummary="true" />
</div> </div>
<div class="ui-g-2 ui-md-2"> <div class="ui-g-2 ui-md-2">
<p:outputLabel value="Contraseña" for="password" /> <p:outputLabel value="Contraseña" for="password" />

View File

@@ -41,7 +41,7 @@
<p:outputLabel value="NIF:" for="nif" /> <p:outputLabel value="NIF:" for="nif" />
</div> </div>
<div class="ui-g-4 ui-md-4"> <div class="ui-g-4 ui-md-4">
<p:inputText id="nif" value="#{UpdateProfile.nif}" required="true" requiredMessage="Por favor, indque su NIF" /> <p:inputText id="nif" value="#{UpdateProfile.nif}" validator="nifValidator" required="true" requiredMessage="Por favor, indque su NIF" />
</div> </div>
<div class="ui-g-6 ui-md-6"> <div class="ui-g-6 ui-md-6">
<p:message for="nif" display="text" /> <p:message for="nif" display="text" />
@@ -71,10 +71,13 @@
<p:outputLabel value="Correo electrónico:" for="email" /> <p:outputLabel value="Correo electrónico:" for="email" />
</div> </div>
<div class="ui-g-4 ui-md-4"> <div class="ui-g-4 ui-md-4">
<p:inputText id="email" value="#{UpdateProfile.email}" required="true" requiredMessage="Por favor, especifique su correo electrónico" /> <p:inputText id="email" value="#{UpdateProfile.email}" required="true" validator="emailValidator" validatorMessage="La dirección #{RegisterUser.email} no es válida"
requiredMessage="Por favor, especifique su correo electrónico">
<f:validateRegex pattern="[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
</p:inputText>
</div> </div>
<div class="ui-g-6 ui-md-6"> <div class="ui-g-6 ui-md-6">
<p:message for="email" /> <p:message for="email" display="text" showDetail="true" showSummary="true" />
</div> </div>
<div class="ui-g-2 ui-md-2"> <div class="ui-g-2 ui-md-2">
<p:outputLabel value="Contraseña anterior" for="oldpassword" /> <p:outputLabel value="Contraseña anterior" for="oldpassword" />
@@ -153,8 +156,8 @@
<p:outputLabel value="Médico de familia:" for="FamilyDoc" /> <p:outputLabel value="Médico de familia:" for="FamilyDoc" />
</div> </div>
<div class="ui-g-4 ui-md-4"> <div class="ui-g-4 ui-md-4">
<p:autoComplete id="FamilyDoc" disabled="true" dropdown="true" required="true" value="#{UpdateProfile.familyDoctor}" completeMethod="#{UpdateProfile.completeFamilyDoctor}" var="fd" <p:autoComplete id="FamilyDoc" disabled="true" dropdown="true" required="true" value="#{UpdateProfile.familyDoctor}" completeMethod="#{UpdateProfile.completeFamilyDoctor}"
itemLabel="#{fd.displayName}" itemValue="#{fd}" forceSelection="true" requiredMessage="Por favor, selecciona un médico de familia"> var="fd" itemLabel="#{fd.displayName}" itemValue="#{fd}" forceSelection="true" requiredMessage="Por favor, selecciona un médico de familia">
<o:converter converterId="omnifaces.ListConverter" list="#{UpdateProfile.familyDoctorList}" /> <o:converter converterId="omnifaces.ListConverter" list="#{UpdateProfile.familyDoctorList}" />
<p:column headerText="Num. Prof."> <p:column headerText="Num. Prof.">
<h:outputText value="#{fd.id}" /> <h:outputText value="#{fd.id}" />

View File

@@ -159,7 +159,7 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal {
return allFDTOs; return allFDTOs;
} }
private MedicalSpecialtyTO getPOJOforMedicalSpecialtyJPA(MedicalSpecialtyJPA ms) { public MedicalSpecialtyTO getPOJOforMedicalSpecialtyJPA(MedicalSpecialtyJPA ms) {
MedicalSpecialtyTO msTO = null; MedicalSpecialtyTO msTO = null;
if (ms != null) { if (ms != null) {
@@ -169,7 +169,7 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal {
return msTO; return msTO;
} }
private PrimaryHealthCareCenterTO getPOJOforPrimaryHealthCareCenterJPA(PrimaryHealthCareCenterJPA phc) { public PrimaryHealthCareCenterTO getPOJOforPrimaryHealthCareCenterJPA(PrimaryHealthCareCenterJPA phc) {
PrimaryHealthCareCenterTO phcTO = null; PrimaryHealthCareCenterTO phcTO = null;
if (phc != null) { if (phc != null) {
@@ -179,7 +179,7 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal {
return phcTO; return phcTO;
} }
private SpecialistDoctorTO getPOJOforSpecialistDoctorJPA(SpecialistDoctorJPA sd, int nestedProps) { public SpecialistDoctorTO getPOJOforSpecialistDoctorJPA(SpecialistDoctorJPA sd, int nestedProps) {
SpecialistDoctorTO sdTO = null; SpecialistDoctorTO sdTO = null;
if (sd != null) { if (sd != null) {
@@ -194,7 +194,7 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal {
return sdTO; return sdTO;
} }
private FamilyDoctorTO getPOJOforFamilyDoctorJPA(FamilyDoctorJPA fd, int nestedProps) { public FamilyDoctorTO getPOJOforFamilyDoctorJPA(FamilyDoctorJPA fd, int nestedProps) {
FamilyDoctorTO fdTO = null; FamilyDoctorTO fdTO = null;
if (fd != null) { if (fd != null) {
@@ -209,7 +209,7 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal {
return fdTO; return fdTO;
} }
private PatientTO getPOJOforPatientJPA(PatientJPA pat, int nestedProps) { public PatientTO getPOJOforPatientJPA(PatientJPA pat, int nestedProps) {
PatientTO paTO = null; PatientTO paTO = null;
if (pat != null) { if (pat != null) {

View File

@@ -11,7 +11,9 @@ import TO.PatientTO;
import TO.PrimaryHealthCareCenterTO; import TO.PrimaryHealthCareCenterTO;
import TO.SpecialistDoctorTO; import TO.SpecialistDoctorTO;
import jpa.FamilyDoctorJPA; import jpa.FamilyDoctorJPA;
import jpa.MedicalSpecialtyJPA;
import jpa.PatientJPA; import jpa.PatientJPA;
import jpa.PrimaryHealthCareCenterJPA;
import jpa.SpecialistDoctorJPA; import jpa.SpecialistDoctorJPA;
/** /**
@@ -52,4 +54,14 @@ public interface CommonFacadeLocal {
public SpecialistDoctorJPA findSpecialistDoctorByCode(String code); public SpecialistDoctorJPA findSpecialistDoctorByCode(String code);
public MedicalSpecialtyTO getPOJOforMedicalSpecialtyJPA(MedicalSpecialtyJPA ms);
public SpecialistDoctorTO getPOJOforSpecialistDoctorJPA(SpecialistDoctorJPA sd, int nestedProps);
public PrimaryHealthCareCenterTO getPOJOforPrimaryHealthCareCenterJPA(PrimaryHealthCareCenterJPA phc);
public FamilyDoctorTO getPOJOforFamilyDoctorJPA(FamilyDoctorJPA fd, int nestedProps);
public PatientTO getPOJOforPatientJPA(PatientJPA pat, int nestedProps);
} }

View File

@@ -57,12 +57,10 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
"No se pueden actualizar los datos del médico de familia porque no se encuentra en la base de datos ningún registro con id: " + String.valueOf(newDoctor)); "No se pueden actualizar los datos del médico de familia porque no se encuentra en la base de datos ningún registro con id: " + String.valueOf(newDoctor));
} }
PatientTO paTO = null;
pat.setFamilyDoctor(fd); pat.setFamilyDoctor(fd);
entman.persist(pat); entman.persist(pat);
// CommonFacadeRemote common = new CommonFacadeBean(); return this.commonServices.getPOJOforPatientJPA(pat, 1);
return commonServices.findPatientById(pat.getId());
} }
public PatientTO registerPatient(int id, String nif, String name, String surname, String password, String email) { public PatientTO registerPatient(int id, String nif, String name, String surname, String password, String email) {
@@ -70,9 +68,8 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
PatientJPA pat = new PatientJPA(this.getNextPersonalIdentificationCode(), nif, name, surname, HashUtils.hashMD5(password), email, null); PatientJPA pat = new PatientJPA(this.getNextPersonalIdentificationCode(), nif, name, surname, HashUtils.hashMD5(password), email, null);
entman.persist(pat); entman.persist(pat);
paTO = new PatientTO(pat.getId(), this.getNextPersonalIdentificationCode(), pat.getNif(), pat.getName(), pat.getSurname(), pat.getPassword(), pat.getEmail(), null);
return paTO; return this.commonServices.getPOJOforPatientJPA(pat, 1);
} }
public SpecialistDoctorTO registerSpecialistDoctor(int id, String nif, String name, String surname, String password, String email, MedicalSpecialtyTO specialty) public SpecialistDoctorTO registerSpecialistDoctor(int id, String nif, String name, String surname, String password, String email, MedicalSpecialtyTO specialty)
@@ -85,10 +82,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
SpecialistDoctorJPA sd = new SpecialistDoctorJPA(this.getNextProfessionalNumber(), nif, name, surname, HashUtils.hashMD5(password), email, ms); SpecialistDoctorJPA sd = new SpecialistDoctorJPA(this.getNextProfessionalNumber(), nif, name, surname, HashUtils.hashMD5(password), email, ms);
entman.persist(sd); entman.persist(sd);
SpecialistDoctorTO sdTO = null; return this.commonServices.getPOJOforSpecialistDoctorJPA(sd, 1);
sdTO = new SpecialistDoctorTO(sd.getId(), this.getNextProfessionalNumber(), sd.getNif(), sd.getName(), sd.getSurname(), sd.getPassword(), sd.getEmail(), specialty);
return sdTO;
} }
public FamilyDoctorTO registerFamilyDoctor(int id, String nif, String name, String surname, String password, String email, PrimaryHealthCareCenterTO cap) throws Exception { public FamilyDoctorTO registerFamilyDoctor(int id, String nif, String name, String surname, String password, String email, PrimaryHealthCareCenterTO cap) throws Exception {
@@ -102,9 +96,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
FamilyDoctorJPA fd = new FamilyDoctorJPA(this.getNextProfessionalNumber(), nif, name, surname, HashUtils.hashMD5(password), email, phcC); FamilyDoctorJPA fd = new FamilyDoctorJPA(this.getNextProfessionalNumber(), nif, name, surname, HashUtils.hashMD5(password), email, phcC);
entman.persist(fd); entman.persist(fd);
fdTO = new FamilyDoctorTO(fd.getId(), this.getNextProfessionalNumber(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), cap); return commonServices.getPOJOforFamilyDoctorJPA(fd, 1);
return fdTO;
} }
public PatientTO updatePatientData(int id, String nif, String name, String surname, String password, String email) throws Exception { public PatientTO updatePatientData(int id, String nif, String name, String surname, String password, String email) throws Exception {
@@ -124,10 +116,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
entman.persist(pat); entman.persist(pat);
PatientTO patTO = null; return this.commonServices.getPOJOforPatientJPA(pat, 1);
patTO = new PatientTO(pat.getId(), pat.getPersonalIdentificationCode(), pat.getNif(), pat.getName(), pat.getSurname(), pat.getPassword(), pat.getEmail(), null);
return patTO;
} }
public SpecialistDoctorTO updateSpecialistDoctorData(int id, String nif, String name, String surname, String password, String email, MedicalSpecialtyTO specialty) public SpecialistDoctorTO updateSpecialistDoctorData(int id, String nif, String name, String surname, String password, String email, MedicalSpecialtyTO specialty)
@@ -153,10 +142,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
entman.persist(sd); entman.persist(sd);
SpecialistDoctorTO sdTO = null; return this.commonServices.getPOJOforSpecialistDoctorJPA(sd, 1);
sdTO = new SpecialistDoctorTO(sd.getId(), sd.getProfessionalNumber(), sd.getNif(), sd.getName(), sd.getSurname(), sd.getPassword(), sd.getEmail(), specialty);
return sdTO;
} }
public FamilyDoctorTO updateFamilyDoctorData(int id, String nif, String name, String surname, String password, String email, PrimaryHealthCareCenterTO phcTO) throws Exception { public FamilyDoctorTO updateFamilyDoctorData(int id, String nif, String name, String surname, String password, String email, PrimaryHealthCareCenterTO phcTO) throws Exception {
@@ -181,10 +167,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
entman.persist(fd); entman.persist(fd);
FamilyDoctorTO fdTO = null; return this.commonServices.getPOJOforFamilyDoctorJPA(fd, 1);
fdTO = new FamilyDoctorTO(fd.getId(), fd.getProfessionalNumber(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), phcTO);
return fdTO;
} }
public FamilyDoctorTO changePrimaryHealthCareCenter(int professionalId, PrimaryHealthCareCenterTO newCenter) throws Exception { public FamilyDoctorTO changePrimaryHealthCareCenter(int professionalId, PrimaryHealthCareCenterTO newCenter) throws Exception {
@@ -202,8 +185,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
fd.setPrimaryHealthCareCenter(phcC); fd.setPrimaryHealthCareCenter(phcC);
entman.persist(fd); entman.persist(fd);
fdTO = new FamilyDoctorTO(fd.getId(), fd.getProfessionalNumber(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), newCenter);
return fdTO; return this.commonServices.getPOJOforFamilyDoctorJPA(fd, 1);
} }
} }

View File

@@ -0,0 +1,43 @@
package managedbean.validators;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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;
@FacesValidator("emailValidator")
public class EmailValidator implements Validator, ClientValidator {
private final static String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
private final static Pattern EMAIL_COMPILED_PATTERN = Pattern.compile(EMAIL_PATTERN);
public void validate(FacesContext context, UIComponent comp, Object value) throws ValidatorException {
String strValue = "";
if (value != null)
strValue = String.valueOf(value);
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"));
Matcher matcher = EMAIL_COMPILED_PATTERN.matcher((String) value);
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"));
}
public Map<String, Object> getMetadata() {
return null;
}
public String getValidatorId() {
return "emailValidator";
}
}

View File

@@ -0,0 +1,39 @@
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, ClientValidator {
public void validate(FacesContext context, UIComponent comp, Object 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";
}
}