From f63ba35621167f7006aa422af1ce45dc92a32c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garcia=20Nu=C3=B1ez?= Date: Thu, 12 Dec 2019 00:18:35 +0100 Subject: [PATCH] =?UTF-8?q?A=C3=B1adidos=20custom=20Validators=20de=20JSF?= =?UTF-8?q?=20para=20validar=20NIF=20y=20email.=20Configurada=20validaci?= =?UTF-8?q?=C3=B3n=20en=20cliente=20de=20email.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docroot/profile/RegisterUser.xhtml | 11 +++-- .../docroot/profile/UpdateProfile.xhtml | 13 +++--- .../src/ejb/common/CommonFacadeBean.java | 10 ++--- .../src/ejb/common/CommonFacadeLocal.java | 12 ++++++ .../src/ejb/profile/ProfileFacadeBean.java | 34 ++++----------- .../validators/EmailValidator.java | 43 +++++++++++++++++++ .../managedbean/validators/NifValidator.java | 39 +++++++++++++++++ 7 files changed, 122 insertions(+), 40 deletions(-) create mode 100644 1.sources/MyHealth/src/managedbean/validators/EmailValidator.java create mode 100644 1.sources/MyHealth/src/managedbean/validators/NifValidator.java diff --git a/1.sources/MyHealth/docroot/profile/RegisterUser.xhtml b/1.sources/MyHealth/docroot/profile/RegisterUser.xhtml index 9b60cd1..c46d556 100644 --- a/1.sources/MyHealth/docroot/profile/RegisterUser.xhtml +++ b/1.sources/MyHealth/docroot/profile/RegisterUser.xhtml @@ -82,10 +82,10 @@
- +
- +
@@ -112,10 +112,13 @@
- + + +
- +
diff --git a/1.sources/MyHealth/docroot/profile/UpdateProfile.xhtml b/1.sources/MyHealth/docroot/profile/UpdateProfile.xhtml index 95700b0..fbc1b26 100644 --- a/1.sources/MyHealth/docroot/profile/UpdateProfile.xhtml +++ b/1.sources/MyHealth/docroot/profile/UpdateProfile.xhtml @@ -41,7 +41,7 @@
- +
@@ -71,10 +71,13 @@
- + + +
- +
@@ -153,8 +156,8 @@
- + diff --git a/1.sources/MyHealth/src/ejb/common/CommonFacadeBean.java b/1.sources/MyHealth/src/ejb/common/CommonFacadeBean.java index 4524e03..578e3c3 100644 --- a/1.sources/MyHealth/src/ejb/common/CommonFacadeBean.java +++ b/1.sources/MyHealth/src/ejb/common/CommonFacadeBean.java @@ -159,7 +159,7 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal { return allFDTOs; } - private MedicalSpecialtyTO getPOJOforMedicalSpecialtyJPA(MedicalSpecialtyJPA ms) { + public MedicalSpecialtyTO getPOJOforMedicalSpecialtyJPA(MedicalSpecialtyJPA ms) { MedicalSpecialtyTO msTO = null; if (ms != null) { @@ -169,7 +169,7 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal { return msTO; } - private PrimaryHealthCareCenterTO getPOJOforPrimaryHealthCareCenterJPA(PrimaryHealthCareCenterJPA phc) { + public PrimaryHealthCareCenterTO getPOJOforPrimaryHealthCareCenterJPA(PrimaryHealthCareCenterJPA phc) { PrimaryHealthCareCenterTO phcTO = null; if (phc != null) { @@ -179,7 +179,7 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal { return phcTO; } - private SpecialistDoctorTO getPOJOforSpecialistDoctorJPA(SpecialistDoctorJPA sd, int nestedProps) { + public SpecialistDoctorTO getPOJOforSpecialistDoctorJPA(SpecialistDoctorJPA sd, int nestedProps) { SpecialistDoctorTO sdTO = null; if (sd != null) { @@ -194,7 +194,7 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal { return sdTO; } - private FamilyDoctorTO getPOJOforFamilyDoctorJPA(FamilyDoctorJPA fd, int nestedProps) { + public FamilyDoctorTO getPOJOforFamilyDoctorJPA(FamilyDoctorJPA fd, int nestedProps) { FamilyDoctorTO fdTO = null; if (fd != null) { @@ -209,7 +209,7 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal { return fdTO; } - private PatientTO getPOJOforPatientJPA(PatientJPA pat, int nestedProps) { + public PatientTO getPOJOforPatientJPA(PatientJPA pat, int nestedProps) { PatientTO paTO = null; if (pat != null) { diff --git a/1.sources/MyHealth/src/ejb/common/CommonFacadeLocal.java b/1.sources/MyHealth/src/ejb/common/CommonFacadeLocal.java index 5cc75bd..bd6a081 100644 --- a/1.sources/MyHealth/src/ejb/common/CommonFacadeLocal.java +++ b/1.sources/MyHealth/src/ejb/common/CommonFacadeLocal.java @@ -11,7 +11,9 @@ import TO.PatientTO; import TO.PrimaryHealthCareCenterTO; import TO.SpecialistDoctorTO; import jpa.FamilyDoctorJPA; +import jpa.MedicalSpecialtyJPA; import jpa.PatientJPA; +import jpa.PrimaryHealthCareCenterJPA; import jpa.SpecialistDoctorJPA; /** @@ -52,4 +54,14 @@ public interface CommonFacadeLocal { 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); + } diff --git a/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java b/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java index 80ca931..5260402 100644 --- a/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java +++ b/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java @@ -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)); } - PatientTO paTO = null; pat.setFamilyDoctor(fd); entman.persist(pat); - // CommonFacadeRemote common = new CommonFacadeBean(); - return commonServices.findPatientById(pat.getId()); + return this.commonServices.getPOJOforPatientJPA(pat, 1); } 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); 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) @@ -85,10 +82,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote { SpecialistDoctorJPA sd = new SpecialistDoctorJPA(this.getNextProfessionalNumber(), nif, name, surname, HashUtils.hashMD5(password), email, ms); entman.persist(sd); - SpecialistDoctorTO sdTO = null; - sdTO = new SpecialistDoctorTO(sd.getId(), this.getNextProfessionalNumber(), sd.getNif(), sd.getName(), sd.getSurname(), sd.getPassword(), sd.getEmail(), specialty); - - return sdTO; + return this.commonServices.getPOJOforSpecialistDoctorJPA(sd, 1); } 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); entman.persist(fd); - fdTO = new FamilyDoctorTO(fd.getId(), this.getNextProfessionalNumber(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), cap); - - return fdTO; + return commonServices.getPOJOforFamilyDoctorJPA(fd, 1); } 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); - PatientTO patTO = null; - patTO = new PatientTO(pat.getId(), pat.getPersonalIdentificationCode(), pat.getNif(), pat.getName(), pat.getSurname(), pat.getPassword(), pat.getEmail(), null); - - return patTO; + return this.commonServices.getPOJOforPatientJPA(pat, 1); } 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); - SpecialistDoctorTO sdTO = null; - sdTO = new SpecialistDoctorTO(sd.getId(), sd.getProfessionalNumber(), sd.getNif(), sd.getName(), sd.getSurname(), sd.getPassword(), sd.getEmail(), specialty); - - return sdTO; + return this.commonServices.getPOJOforSpecialistDoctorJPA(sd, 1); } 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); - FamilyDoctorTO fdTO = null; - fdTO = new FamilyDoctorTO(fd.getId(), fd.getProfessionalNumber(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), phcTO); - - return fdTO; + return this.commonServices.getPOJOforFamilyDoctorJPA(fd, 1); } public FamilyDoctorTO changePrimaryHealthCareCenter(int professionalId, PrimaryHealthCareCenterTO newCenter) throws Exception { @@ -202,8 +185,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote { fd.setPrimaryHealthCareCenter(phcC); 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); } } diff --git a/1.sources/MyHealth/src/managedbean/validators/EmailValidator.java b/1.sources/MyHealth/src/managedbean/validators/EmailValidator.java new file mode 100644 index 0000000..f6d2fcc --- /dev/null +++ b/1.sources/MyHealth/src/managedbean/validators/EmailValidator.java @@ -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 getMetadata() { + return null; + } + + public String getValidatorId() { + return "emailValidator"; + } +} diff --git a/1.sources/MyHealth/src/managedbean/validators/NifValidator.java b/1.sources/MyHealth/src/managedbean/validators/NifValidator.java new file mode 100644 index 0000000..c663919 --- /dev/null +++ b/1.sources/MyHealth/src/managedbean/validators/NifValidator.java @@ -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 getMetadata() { + return null; + } + + public String getValidatorId() { + return "nifValidator"; + } +}