diff --git a/1.sources/MyHealth/src/ejb/common/CommonFacadeBean.java b/1.sources/MyHealth/src/ejb/common/CommonFacadeBean.java new file mode 100644 index 0000000..44fdb1e --- /dev/null +++ b/1.sources/MyHealth/src/ejb/common/CommonFacadeBean.java @@ -0,0 +1,152 @@ +package ejb.common; + +import java.util.ArrayList; +import java.util.Collection; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import TO.FamilyDoctorTO; +import TO.MedicalSpecialtyTO; +import TO.PatientTO; +import TO.PrimaryHealthCareCenterTO; +import TO.SpecialistDoctorTO; +import jpa.FamilyDoctorJPA; +import jpa.MedicalSpecialtyJPA; +import jpa.PatientJPA; +import jpa.PrimaryHealthCareCenterJPA; +import jpa.SpecialistDoctorJPA; + +@Stateless +public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal { + // Persistence Unit Context + @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 Collection listAllFamilyDoctors() { + return this.listAllFamilyDoctors(0, 0); + } + + public Collection listAllFamilyDoctors(int pageNumber, int pageSize) { + Query query = entman.createQuery("from FamilyDoctorJPA order by name, surname"); + + if (pageSize > 0) { + query.setFirstResult(pageNumber * pageSize); + query.setMaxResults(pageSize); + } + + @SuppressWarnings("unchecked") + Collection allFDsJPA = query.getResultList(); + Collection allFDTOs = new ArrayList(); + + for (FamilyDoctorJPA item : allFDsJPA) { + allFDTOs.add(new FamilyDoctorTO(item.getId(), item.getNif(), item.getName(), item.getSurname(), item.getPassword(), item.getEmail(), null)); + } + + return allFDTOs; + } + + 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/common/CommonFacadeLocal.java b/1.sources/MyHealth/src/ejb/common/CommonFacadeLocal.java new file mode 100644 index 0000000..3ec40c9 --- /dev/null +++ b/1.sources/MyHealth/src/ejb/common/CommonFacadeLocal.java @@ -0,0 +1,38 @@ +package ejb.common; + +import java.util.Collection; + +import javax.ejb.Local; + +import TO.FamilyDoctorTO; +import TO.MedicalSpecialtyTO; +import TO.PatientTO; +import TO.PrimaryHealthCareCenterTO; +import TO.SpecialistDoctorTO; + +/** + * + * @author Marcos García Núñez (mgarcianun@uoc.edu) + * + */ +@Local +public interface CommonFacadeLocal { + + public Collection listAllMedicalSpecialities(); + + public Collection listPagedMedicalSpecialities(int pageNumber, int pageSize); + + public Collection listAllCAPs(); + + public Collection listPagedAllCAPs(int pageNumber, int pageSize); + + public Collection listAllFamilyDoctors(); + + public Collection listAllFamilyDoctors(int pageNumber, int pageSize); + + public PatientTO retrievePatient(int patientId) throws Exception; + + public FamilyDoctorTO retrieveFamilyDoctor(int ProfessionalNumberId) throws Exception; + + public SpecialistDoctorTO retrieveSpecialistDoctor(int ProfessionalNumberId) throws Exception; +} diff --git a/1.sources/MyHealth/src/ejb/common/CommonFacadeRemote.java b/1.sources/MyHealth/src/ejb/common/CommonFacadeRemote.java new file mode 100644 index 0000000..6a9732b --- /dev/null +++ b/1.sources/MyHealth/src/ejb/common/CommonFacadeRemote.java @@ -0,0 +1,38 @@ +package ejb.common; + +import java.util.Collection; + +import javax.ejb.Remote; + +import TO.FamilyDoctorTO; +import TO.MedicalSpecialtyTO; +import TO.PatientTO; +import TO.PrimaryHealthCareCenterTO; +import TO.SpecialistDoctorTO; + +/** + * + * @author Marcos García Núñez (mgarcianun@uoc.edu) + * + */ +@Remote +public interface CommonFacadeRemote { + + public Collection listAllMedicalSpecialities(); + + public Collection listPagedMedicalSpecialities(int pageNumber, int pageSize); + + public Collection listAllCAPs(); + + public Collection listPagedAllCAPs(int pageNumber, int pageSize); + + public Collection listAllFamilyDoctors(); + + public Collection listAllFamilyDoctors(int pageNumber, int pageSize); + + public PatientTO retrievePatient(int patientId) throws Exception; + + public FamilyDoctorTO retrieveFamilyDoctor(int ProfessionalNumberId) throws Exception; + + public SpecialistDoctorTO retrieveSpecialistDoctor(int ProfessionalNumberId) throws Exception; +}