package ejb.common; import java.util.ArrayList; import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import javax.persistence.TypedQuery; import org.jboss.security.auth.spi.Users.User; import TO.FamilyDoctorTO; import TO.MedicalSpecialtyTO; import TO.PatientTO; import TO.PrimaryHealthCareCenterTO; import TO.SpecialistDoctorTO; import common.Utils; 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 List listAllMedicalSpecialities() { return this.listMedicalSpecialitiesPaged(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 List listMedicalSpecialitiesPaged(int pageNumber, int pageSize) { return listMedicalSpecialitiesFiltered(null, pageNumber, pageSize); } public List listMedicalSpecialitiesFiltered(String searchTerm, int pageNumber, int pageSize) { String strQuery = "SELECT ms from MedicalSpecialtyJPA ms %s order by ms.name, ms.description"; String strFilter = ""; if (searchTerm == null) searchTerm = ""; else searchTerm = Utils.stripAccents(searchTerm).toLowerCase(); if (searchTerm.length() > 0) { strFilter = "WHERE lower(ms.name) LIKE :searchTerm OR lower(ms.description) LIKE :searchTerm"; } TypedQuery query = entman.createQuery(String.format(strQuery, strFilter), MedicalSpecialtyJPA.class); if (searchTerm.length() > 0) query.setParameter("searchTerm", "%" + searchTerm + "%"); if (pageSize > 0) { query.setFirstResult(pageNumber * pageSize); query.setMaxResults(pageSize); } List allJPA = query.getResultList(); List allSpecialities = new ArrayList(); for (MedicalSpecialtyJPA item : allJPA) { allSpecialities.add(this.getPOJOforMedicalSpecialtyJPA(item)); } return allSpecialities; } public List listAllCAPs() { return this.listCAPsPaged(0, 0); } public List listCAPsPaged(int pageNumber, int pageSize) { return this.listCAPsFiltered(null, pageNumber, pageSize); } public List listCAPsFiltered(String searchTerm, int pageNumber, int pageSize) { String strQuery = "SELECT phc from PrimaryHealthCareCenterJPA phc %s order by phc.name, phc.location"; String strFilter = ""; if (searchTerm == null) searchTerm = ""; else searchTerm = Utils.stripAccents(searchTerm).toLowerCase(); if (searchTerm.length() > 0) { strFilter = "WHERE lower(phc.name) LIKE :searchTerm OR lower(phc.location) LIKE :searchTerm"; } TypedQuery query = entman.createQuery(String.format(strQuery, strFilter), PrimaryHealthCareCenterJPA.class); if (searchTerm.length() > 0) query.setParameter("searchTerm", "%" + searchTerm + "%"); if (pageSize > 0) { query.setFirstResult(pageNumber * pageSize); query.setMaxResults(pageSize); } List allJPA = query.getResultList(); List allCAPs = new ArrayList(); for (PrimaryHealthCareCenterJPA item : allJPA) { allCAPs.add(this.getPOJOforPrimaryHealthCareCenterJPA(item)); } return allCAPs; } public List listAllFamilyDoctors() { return this.listFamilyDoctorsPaged(0, 0); } public List listFamilyDoctorsPaged(int pageNumber, int pageSize) { return this.listFamilyDoctorsFiltered(null, pageNumber, pageSize); } public List listFamilyDoctorsFiltered(String searchTerm, int pageNumber, int pageSize) { String strQuery = "SELECT fd FROM FamilyDoctorJPA fd %s order by fd.name, fd.surname"; String strFilter = ""; if (searchTerm == null) searchTerm = ""; else searchTerm = Utils.stripAccents(searchTerm).toLowerCase(); if (searchTerm.length() > 0) { strFilter = "WHERE lower(fd.name) LIKE :searchTerm OR lower(fd.surname) LIKE :searchTerm"; } TypedQuery query = entman.createQuery(String.format(strQuery, strFilter), FamilyDoctorJPA.class); if (searchTerm.length() > 0) query.setParameter("searchTerm", "%" + searchTerm + "%"); if (pageSize > 0) { query.setFirstResult(pageNumber * pageSize); query.setMaxResults(pageSize); } List allFDsJPA = query.getResultList(); List allFDTOs = new ArrayList(); for (FamilyDoctorJPA item : allFDsJPA) { allFDTOs.add(this.getPOJOforFamilyDoctorJPA(item, 0)); } return allFDTOs; } public MedicalSpecialtyTO getPOJOforMedicalSpecialtyJPA(MedicalSpecialtyJPA ms) { MedicalSpecialtyTO msTO = null; if (ms != null) { msTO = new MedicalSpecialtyTO(ms.getId(), ms.getName(), ms.getDescription()); } return msTO; } public PrimaryHealthCareCenterTO getPOJOforPrimaryHealthCareCenterJPA(PrimaryHealthCareCenterJPA phc) { PrimaryHealthCareCenterTO phcTO = null; if (phc != null) { phcTO = new PrimaryHealthCareCenterTO(phc.getId(), phc.getName(), phc.getLocation()); } return phcTO; } public SpecialistDoctorTO getPOJOforSpecialistDoctorJPA(SpecialistDoctorJPA sd, int nestedProps) { SpecialistDoctorTO sdTO = null; if (sd != null) { MedicalSpecialtyJPA ms = null; if (nestedProps > 0) ms = sd.getMedicalSpecialty(); nestedProps--; sdTO = new SpecialistDoctorTO(sd.getId(), sd.getProfessionalNumber(), sd.getNif(), sd.getName(), sd.getSurname(), sd.getPassword(), sd.getEmail(), this.getPOJOforMedicalSpecialtyJPA(ms)); } return sdTO; } public FamilyDoctorTO getPOJOforFamilyDoctorJPA(FamilyDoctorJPA fd, int nestedProps) { FamilyDoctorTO fdTO = null; if (fd != null) { PrimaryHealthCareCenterJPA phc = null; if (nestedProps > 0) phc = fd.getPrimaryHealthCareCenter(); nestedProps--; fdTO = new FamilyDoctorTO(fd.getId(), fd.getProfessionalNumber(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), this.getPOJOforPrimaryHealthCareCenterJPA(phc)); } return fdTO; } public PatientTO getPOJOforPatientJPA(PatientJPA pat, int nestedProps) { PatientTO paTO = null; if (pat != null) { FamilyDoctorJPA fd = null; if (nestedProps > 0) fd = pat.getFamilyDoctor(); nestedProps--; paTO = new PatientTO(pat.getId(), pat.getPersonalIdentificationCode(), pat.getNif(), pat.getName(), pat.getSurname(), pat.getPassword(), pat.getEmail(), this.getPOJOforFamilyDoctorJPA(fd, nestedProps)); } return paTO; } public PatientTO findPatientById(int patientId) { // Recuperamos propiedades anidadas 1 nivel! return this.getPOJOforPatientJPA(entman.find(PatientJPA.class, patientId), 1); } public PatientJPA findPatientByCode(String code) { TypedQuery query = entman.createQuery("from PatientJPA pat where pat.personalIdentificationCode=:code", PatientJPA.class); query.setParameter("code", code); List results = query.getResultList(); if (results.size() > 0) return results.get(0); else return null; } public FamilyDoctorTO findFamilyDoctorById(int ProfessionalNumberId) { return this.getPOJOforFamilyDoctorJPA(entman.find(FamilyDoctorJPA.class, ProfessionalNumberId), 1); } public FamilyDoctorJPA findFamilyDoctorByCode(String code) { TypedQuery query = entman.createQuery("from FamilyDoctorJPA d where d.professionalNumber=:code", FamilyDoctorJPA.class); query.setParameter("code", code); List results = query.getResultList(); if (results.size() > 0) return results.get(0); else return null; } public SpecialistDoctorTO findSpecialistDoctorById(int ProfessionalNumberId) { return this.getPOJOforSpecialistDoctorJPA(entman.find(SpecialistDoctorJPA.class, ProfessionalNumberId), 1); } public SpecialistDoctorJPA findSpecialistDoctorByCode(String code) { TypedQuery query = entman.createQuery("from SpecialistDoctorJPA d where d.professionalNumber=:code", SpecialistDoctorJPA.class); query.setParameter("code", code); List results = query.getResultList(); if (results.size() > 0) return results.get(0); else return null; } }