From 4c7c978ccdf00dc43b2bfb9638da06a4a343992f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garcia=20Nu=C3=B1ez?= Date: Mon, 9 Dec 2019 23:08:45 +0100 Subject: [PATCH] Nueva funcionalidad (vista, controlador y persistencia) de cambiar medico de familia asignado. Incluidos cambios para utilizar nuvo EJB Common con interfaz local y remota. --- .../docroot/profile/ChangeFamilyDoctor.xhtml | 64 ++++++++-- .../docroot/profile/RegisterUser.xhtml | 2 +- 1.sources/MyHealth/src/TO/FamilyDoctorTO.java | 4 + .../src/ejb/profile/ProfileFacadeBean.java | 56 ++------ .../src/ejb/profile/ProfileFacadeRemote.java | 5 - .../systemAdmin/SystemAdminFacadeBean.java | 56 -------- .../systemAdmin/SystemAdminFacadeRemote.java | 2 - .../profile/ChangeFamilyDoctorMBean.java | 120 ++++++++++++++---- .../ChangePrimaryHealthCareCenterMBean.java | 4 +- .../profile/RegisterUserMBean.java | 4 +- .../profile/UpdateProfileMBean.java | 10 +- 11 files changed, 165 insertions(+), 162 deletions(-) diff --git a/1.sources/MyHealth/docroot/profile/ChangeFamilyDoctor.xhtml b/1.sources/MyHealth/docroot/profile/ChangeFamilyDoctor.xhtml index 8cca1b8..6995afe 100644 --- a/1.sources/MyHealth/docroot/profile/ChangeFamilyDoctor.xhtml +++ b/1.sources/MyHealth/docroot/profile/ChangeFamilyDoctor.xhtml @@ -1,39 +1,75 @@ - - + + +
+
+ +
+
+ Su perfil de usuario no permite que pueda cambiar su Médico de Familia asignado.
Si cree que no debería estar viendo esta página y que se trata de un + error, por favor, contacte con el soporte de la aplicación para obtener asistencia tecnica. +
+
+
+ +
+
+
+
+
+
+ - +
-
- + +
+
- +
-
- +
+ +
+
+
+ + + + + + + +
+
+ +
+
-
+
- +
- +
-
+
+ - + diff --git a/1.sources/MyHealth/docroot/profile/RegisterUser.xhtml b/1.sources/MyHealth/docroot/profile/RegisterUser.xhtml index 0e3dd35..668e6c0 100644 --- a/1.sources/MyHealth/docroot/profile/RegisterUser.xhtml +++ b/1.sources/MyHealth/docroot/profile/RegisterUser.xhtml @@ -175,7 +175,7 @@
- +
diff --git a/1.sources/MyHealth/src/TO/FamilyDoctorTO.java b/1.sources/MyHealth/src/TO/FamilyDoctorTO.java index c37fef8..30e00cf 100644 --- a/1.sources/MyHealth/src/TO/FamilyDoctorTO.java +++ b/1.sources/MyHealth/src/TO/FamilyDoctorTO.java @@ -82,6 +82,10 @@ public class FamilyDoctorTO implements Serializable { public void setId(Integer id) { this.id = id; } + + public String getDisplayDescription() { + return String.format("[%d] %s %s", this.id, this.name, this.surname); + } public PrimaryHealthCareCenterTO getPrimaryHealthCareCenter() { return primaryHealthCareCenter; diff --git a/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java b/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java index 3a70733..e6c34b7 100644 --- a/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java +++ b/1.sources/MyHealth/src/ejb/profile/ProfileFacadeBean.java @@ -1,5 +1,6 @@ package ejb.profile; +import javax.ejb.EJB; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @@ -10,6 +11,9 @@ import TO.PatientTO; import TO.PrimaryHealthCareCenterTO; import TO.SpecialistDoctorTO; import common.HashUtils; +import ejb.common.CommonFacadeBean; +import ejb.common.CommonFacadeLocal; +import ejb.common.CommonFacadeRemote; import jpa.FamilyDoctorJPA; import jpa.MedicalSpecialtyJPA; import jpa.PatientJPA; @@ -27,6 +31,9 @@ public class ProfileFacadeBean implements ProfileFacadeRemote { // Persistence Unit Context @PersistenceContext(unitName = "MyHealth") private EntityManager entman; + + @EJB + CommonFacadeLocal commonServices; public PatientTO changeFamilyDoctor(int patientId, int ProfessionalNumberId) throws Exception { PatientJPA pat = entman.find(PatientJPA.class, patientId); @@ -44,7 +51,8 @@ public class ProfileFacadeBean implements ProfileFacadeRemote { pat.setFamilyDoctor(fd); entman.persist(pat); - return this.retrievePatient(pat.getId()); + //CommonFacadeRemote common = new CommonFacadeBean(); + return commonServices.retrievePatient(pat.getId()); } public PatientTO registerPatient(int id, String nif, String name, String surname, String password, String email) { @@ -188,50 +196,4 @@ public class ProfileFacadeBean implements ProfileFacadeRemote { return fdTO; } - - public PatientTO retrievePatient(int patientId) throws Exception { - PatientJPA pat = entman.find(PatientJPA.class, patientId); - - if (pat == null) { - throw new Exception("No se pueden actualizar los datos del paciente porque no se encuentra en la base de datos ningún registro con id: " + String.valueOf(patientId)); - } - - FamilyDoctorTO fdTO = null; - if (pat.getFamilyDoctor() != null) { - FamilyDoctorJPA fd = pat.getFamilyDoctor(); - fdTO = new FamilyDoctorTO(fd.getId(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), null); - } - - PatientTO paTO = new PatientTO(pat.getId(), pat.getNif(), pat.getName(), pat.getSurname(), pat.getPassword(), pat.getEmail(), fdTO); - return paTO; - } - - public FamilyDoctorTO retrieveFamilyDoctor(int ProfessionalNumberId) throws Exception { - FamilyDoctorJPA fd = entman.find(FamilyDoctorJPA.class, ProfessionalNumberId); - if (fd == null) { - throw new Exception("No se encuentra en la base de datos ningún médico de familia con id: " + String.valueOf(ProfessionalNumberId)); - } - - PrimaryHealthCareCenterTO phc = null; - if (fd.getPrimaryHealthCareCenter() != null) - phc = new PrimaryHealthCareCenterTO(fd.getPrimaryHealthCareCenter().getName(), fd.getPrimaryHealthCareCenter().getLocation()); - - FamilyDoctorTO fdTO = new FamilyDoctorTO(fd.getId(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), phc); - return fdTO; - } - - public SpecialistDoctorTO retrieveSpecialistDoctor(int professionalId) throws Exception { - SpecialistDoctorJPA sd = entman.find(SpecialistDoctorJPA.class, professionalId); - if (sd == null) { - throw new Exception( - "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(professionalId)); - } - - MedicalSpecialtyTO ms = null; - if (sd.getMedicalSpecialty() != null) - ms = new MedicalSpecialtyTO(sd.getMedicalSpecialty().getName(), sd.getMedicalSpecialty().getDescription()); - - SpecialistDoctorTO sdTO = new SpecialistDoctorTO(sd.getId(), sd.getNif(), sd.getName(), sd.getSurname(), sd.getPassword(), sd.getEmail(), ms); - return sdTO; - } } diff --git a/1.sources/MyHealth/src/ejb/profile/ProfileFacadeRemote.java b/1.sources/MyHealth/src/ejb/profile/ProfileFacadeRemote.java index 21919b0..d75674c 100644 --- a/1.sources/MyHealth/src/ejb/profile/ProfileFacadeRemote.java +++ b/1.sources/MyHealth/src/ejb/profile/ProfileFacadeRemote.java @@ -31,10 +31,5 @@ public interface ProfileFacadeRemote { public FamilyDoctorTO updateFamilyDoctorData(int id, String nif, String name, String surname, String password, String email, PrimaryHealthCareCenterTO cap) throws Exception; public FamilyDoctorTO changePrimaryHealthCareCenter(int ProfessionalNumberId, PrimaryHealthCareCenterTO newCenter) throws Exception; - - public PatientTO retrievePatient(int patientId) throws Exception; - public FamilyDoctorTO retrieveFamilyDoctor(int ProfessionalNumberId) throws Exception; - - public SpecialistDoctorTO retrieveSpecialistDoctor(int ProfessionalNumberId) throws Exception; } \ No newline at end of file diff --git a/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeBean.java b/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeBean.java index fd35ab6..ba4469c 100644 --- a/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeBean.java +++ b/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeBean.java @@ -32,62 +32,6 @@ public class SystemAdminFacadeBean implements SystemAdminFacadeRemote { @PersistenceContext(unitName = "MyHealth") private EntityManager entman; - /** - * Metodo que devuelve todas las especialidades medicas - */ - public Collection listAllMedicalSpecialities() { - return this.listPagedMedicalSpecialities(0, 0); - } - - /** - * Metodo que devuelve las especialidades médicas de forma paginada - * - * Acepta como parametro la página (comenzando en 0) y el número de elementos de - * cada página - * - */ - public Collection listPagedMedicalSpecialities(int pageNumber, int pageSize) { - Query query = entman.createQuery("from MedicalSpecialtyJPA order by name"); - - if (pageSize > 0) { - query.setFirstResult(pageNumber * pageSize); - query.setMaxResults(pageSize); - } - - @SuppressWarnings("unchecked") - Collection allJPA = query.getResultList(); - Collection allSpecialities = new ArrayList(); - - for (MedicalSpecialtyJPA ms : allJPA) { - allSpecialities.add(new MedicalSpecialtyTO(ms.getName(), ms.getDescription())); - } - - return allSpecialities; - } - - public Collection listAllCAPs() { - return this.listPagedAllCAPs(0, 0); - } - - public Collection listPagedAllCAPs(int pageNumber, int pageSize) { - Query query = entman.createQuery("from PrimaryHealthCareCenterJPA order by name"); - - if (pageSize > 0) { - query.setFirstResult(pageNumber * pageSize); - query.setMaxResults(pageSize); - } - - @SuppressWarnings("unchecked") - Collection allJPA = query.getResultList(); - Collection allCAPs = new ArrayList(); - - for (PrimaryHealthCareCenterJPA cap : allJPA) { - allCAPs.add(new PrimaryHealthCareCenterTO(cap.getName(), cap.getLocation())); - } - - return allCAPs; - } - public LoggedUserTO login(String id, String pwd) { LoggedUserTO usr = null; diff --git a/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeRemote.java b/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeRemote.java index 98bbf18..7c275b5 100644 --- a/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeRemote.java +++ b/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeRemote.java @@ -18,9 +18,7 @@ public interface SystemAdminFacadeRemote { /** * Definimos la interfaz remota */ - public Collection listAllMedicalSpecialities(); - public Collection listAllCAPs(); public LoggedUserTO login(String id, String pwd); } \ No newline at end of file diff --git a/1.sources/MyHealth/src/managedbean/profile/ChangeFamilyDoctorMBean.java b/1.sources/MyHealth/src/managedbean/profile/ChangeFamilyDoctorMBean.java index 960a88c..77996c8 100644 --- a/1.sources/MyHealth/src/managedbean/profile/ChangeFamilyDoctorMBean.java +++ b/1.sources/MyHealth/src/managedbean/profile/ChangeFamilyDoctorMBean.java @@ -1,50 +1,114 @@ package managedbean.profile; import java.io.Serializable; -import java.util.Properties; +import java.util.Collection; -import javax.ejb.EJB; -import javax.enterprise.context.RequestScoped; +import javax.annotation.PostConstruct; +import javax.faces.application.FacesMessage; +import javax.faces.view.ViewScoped; import javax.inject.Named; -import javax.naming.Context; -import javax.naming.InitialContext; -import ejb.profile.ProfileFacadeRemote; +import TO.FamilyDoctorTO; +import TO.LoggedUserTO; +import TO.PatientTO; +import common.UserType; import managedbean.common.ManagedBeanBase; +import managedbean.common.SessionUtils; /*** * * @author Marcos García Núñez (mgarcianun@uoc.edu) * */ -@Named( "ChangeFamilyDoctorMBean") -@RequestScoped +@Named("ChangeFD") +@ViewScoped public class ChangeFamilyDoctorMBean extends ManagedBeanBase implements Serializable { - private static final long serialVersionUID = 1L; - @EJB - private ProfileFacadeRemote remoteManager; + private int id; + private FamilyDoctorTO currentFamilyDoctor; + private FamilyDoctorTO newFamilyDoctor; + private Collection familyDoctorList; + + public ChangeFamilyDoctorMBean() { - /** - * Constructor. Inicializa la conexión con el EJB Remoto - * - * @throws Exception - */ - public ChangeFamilyDoctorMBean() throws Exception { - initializeAdminFacadeRemote(); } - /** - * Inicializa la conexión con el EJB Remoto - * - * @throws Exception - */ - private void initializeAdminFacadeRemote() throws Exception { - Properties props = System.getProperties(); - Context ctx = new InitialContext(props); - remoteManager = (ProfileFacadeRemote) ctx - .lookup("java:app/myHealth.jar/ProfileFacadeBean!ejb.component.ProfileFacadeRemote"); + @PostConstruct + public void init() { + // Recuperamos el usuario logeado actual + LoggedUserTO usr = null; + try { + usr = SessionUtils.getloggedOnUser(); + + if (usr == null) + this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Sesión no válida", + "Su sesión actual no es válida, por favor cierre su sesión y vuelva a logearse en el sistema."); + else { + this.id = Integer.valueOf(usr.getId()); + + if (usr.getUserType() == UserType.PATIENT) { + this.familyDoctorList = this.getRemoteManagerCommon().listAllFamilyDoctors(); + + PatientTO pat = this.getRemoteManagerCommon().retrievePatient(this.id); + this.setCurrentFamilyDoctor(pat.getFamilyDoctor()); + } + } + } catch (Exception e) { + this.manageException(e); + } + + } + + public Collection getFamilyDoctorList() { + return familyDoctorList; + } + + public int getId() { + return id; + } + + public void saveData() { + int error = 0; + + if (this.getNewFamilyDoctor() == null) { + this.addFacesMessage(FacesMessage.SEVERITY_WARN, "Nuevo médico de familia no seleccionado", "Por favor, especifique un nuevvo médico de familia."); + error++; + } + + if (this.getCurrentFamilyDoctor() != null && this.getNewFamilyDoctor().getId() == this.getCurrentFamilyDoctor().getId()) { + this.addFacesMessage(FacesMessage.SEVERITY_WARN, "El médico de familia debe ser diferente", + "Por favor, seleccione un médico de familia diferente al que tiene actualmente asignado."); + error++; + } + + if (error == 0) { + try { + PatientTO paTO = this.getRemoteManagerProfile().changeFamilyDoctor(this.getId(), this.newFamilyDoctor.getId()); + this.currentFamilyDoctor = paTO.getFamilyDoctor(); + this.newFamilyDoctor = null; + + this.addFacesMessage(FacesMessage.SEVERITY_INFO, "Los datos se han guardado", "Su médico de familia asignado se ha cambiado correctamente."); + } catch (Exception e) { + this.manageException(e); + } + } + } + + public FamilyDoctorTO getCurrentFamilyDoctor() { + return currentFamilyDoctor; + } + + public void setCurrentFamilyDoctor(FamilyDoctorTO familyDoctor) { + this.currentFamilyDoctor = familyDoctor; + } + + public FamilyDoctorTO getNewFamilyDoctor() { + return newFamilyDoctor; + } + + public void setNewFamilyDoctor(FamilyDoctorTO familyDoctor) { + this.newFamilyDoctor = familyDoctor; } } diff --git a/1.sources/MyHealth/src/managedbean/profile/ChangePrimaryHealthCareCenterMBean.java b/1.sources/MyHealth/src/managedbean/profile/ChangePrimaryHealthCareCenterMBean.java index d45b95a..7d894a0 100644 --- a/1.sources/MyHealth/src/managedbean/profile/ChangePrimaryHealthCareCenterMBean.java +++ b/1.sources/MyHealth/src/managedbean/profile/ChangePrimaryHealthCareCenterMBean.java @@ -48,9 +48,9 @@ public class ChangePrimaryHealthCareCenterMBean extends ManagedBeanBase implemen this.id = Integer.valueOf(usr.getId()); if (usr.getUserType() == UserType.FAMILY_DOCTOR) { - this.primaryHealthCareCentersList = this.getRemoteManagerSystemAdmin().listAllCAPs(); + this.primaryHealthCareCentersList = this.getRemoteManagerCommon().listAllCAPs(); - FamilyDoctorTO fd = this.getRemoteManagerProfile().retrieveFamilyDoctor(this.id); + FamilyDoctorTO fd = this.getRemoteManagerCommon().retrieveFamilyDoctor(this.id); this.setCurrentCenter(fd.getPrimaryHealthCareCenter()); } } diff --git a/1.sources/MyHealth/src/managedbean/profile/RegisterUserMBean.java b/1.sources/MyHealth/src/managedbean/profile/RegisterUserMBean.java index 844a4ed..331f6eb 100644 --- a/1.sources/MyHealth/src/managedbean/profile/RegisterUserMBean.java +++ b/1.sources/MyHealth/src/managedbean/profile/RegisterUserMBean.java @@ -67,8 +67,8 @@ public class RegisterUserMBean extends ManagedBeanBase implements Serializable { this.userType = UserType.PATIENT.name(); - this.medicalSpecialitiesList = this.getRemoteManagerSystemAdmin().listAllMedicalSpecialities(); - this.primaryHealthCareCentersList = this.getRemoteManagerSystemAdmin().listAllCAPs(); + this.medicalSpecialitiesList = this.getRemoteManagerCommon().listAllMedicalSpecialities(); + this.primaryHealthCareCentersList = this.getRemoteManagerCommon().listAllCAPs(); } public List getUserTypes() { diff --git a/1.sources/MyHealth/src/managedbean/profile/UpdateProfileMBean.java b/1.sources/MyHealth/src/managedbean/profile/UpdateProfileMBean.java index 08daeb5..6bd252f 100644 --- a/1.sources/MyHealth/src/managedbean/profile/UpdateProfileMBean.java +++ b/1.sources/MyHealth/src/managedbean/profile/UpdateProfileMBean.java @@ -87,7 +87,7 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable switch (usr.getUserType()) { case PATIENT: - PatientTO pat = this.getRemoteManagerProfile().retrievePatient(this.id); + PatientTO pat = this.getRemoteManagerCommon().retrievePatient(this.id); this.name = pat.getName(); this.surname = pat.getName(); @@ -96,8 +96,8 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable this.currentPassword = pat.getPassword(); break; case SPECIALIST_DOCTOR: - this.medicalSpecialitiesList = this.getRemoteManagerSystemAdmin().listAllMedicalSpecialities(); - SpecialistDoctorTO sd = this.getRemoteManagerProfile().retrieveSpecialistDoctor(this.id); + this.medicalSpecialitiesList = this.getRemoteManagerCommon().listAllMedicalSpecialities(); + SpecialistDoctorTO sd = this.getRemoteManagerCommon().retrieveSpecialistDoctor(this.id); this.name = sd.getName(); this.surname = sd.getName(); @@ -107,8 +107,8 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable this.medicalSpecialty = sd.getMedicalSpecialty(); break; case FAMILY_DOCTOR: - this.primaryHealthCareCentersList = this.getRemoteManagerSystemAdmin().listAllCAPs(); - FamilyDoctorTO fd = this.getRemoteManagerProfile().retrieveFamilyDoctor(this.id); + this.primaryHealthCareCentersList = this.getRemoteManagerCommon().listAllCAPs(); + FamilyDoctorTO fd = this.getRemoteManagerCommon().retrieveFamilyDoctor(this.id); this.name = fd.getName(); this.surname = fd.getName();