Merge branch 'master' into rorden

This commit is contained in:
Roberto Orden Erena
2019-12-14 11:23:42 +01:00
39 changed files with 1348 additions and 644 deletions

View File

@@ -1,18 +1,22 @@
package ejb.common;
import java.util.ArrayList;
import java.util.Collection;
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;
@@ -28,8 +32,8 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal {
/**
* Metodo que devuelve todas las especialidades medicas
*/
public Collection<MedicalSpecialtyTO> listAllMedicalSpecialities() {
return this.listPagedMedicalSpecialities(0, 0);
public List<MedicalSpecialtyTO> listAllMedicalSpecialities() {
return this.listMedicalSpecialitiesPaged(0, 0);
}
/**
@@ -39,114 +43,233 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal {
* cada página
*
*/
public Collection<MedicalSpecialtyTO> listPagedMedicalSpecialities(int pageNumber, int pageSize) {
Query query = entman.createQuery("from MedicalSpecialtyJPA order by name");
public List<MedicalSpecialtyTO> listMedicalSpecialitiesPaged(int pageNumber, int pageSize) {
return listMedicalSpecialitiesFiltered(null, pageNumber, pageSize);
}
public List<MedicalSpecialtyTO> 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.normalizeTerm(searchTerm);
if (searchTerm.length() > 0) {
strFilter = "WHERE lower(ms.name) LIKE :searchTerm OR lower(ms.description) LIKE :searchTerm";
}
TypedQuery<MedicalSpecialtyJPA> 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);
}
@SuppressWarnings("unchecked")
Collection<MedicalSpecialtyJPA> allJPA = query.getResultList();
Collection<MedicalSpecialtyTO> allSpecialities = new ArrayList<MedicalSpecialtyTO>();
List<MedicalSpecialtyJPA> allJPA = query.getResultList();
List<MedicalSpecialtyTO> allSpecialities = new ArrayList<MedicalSpecialtyTO>();
for (MedicalSpecialtyJPA ms : allJPA) {
allSpecialities.add(new MedicalSpecialtyTO(ms.getName(), ms.getDescription()));
for (MedicalSpecialtyJPA item : allJPA) {
allSpecialities.add(this.getPOJOforMedicalSpecialtyJPA(item));
}
return allSpecialities;
}
public Collection<PrimaryHealthCareCenterTO> listAllCAPs() {
return this.listPagedAllCAPs(0, 0);
public List<PrimaryHealthCareCenterTO> listAllCAPs() {
return this.listCAPsPaged(0, 0);
}
public Collection<PrimaryHealthCareCenterTO> listPagedAllCAPs(int pageNumber, int pageSize) {
Query query = entman.createQuery("from PrimaryHealthCareCenterJPA order by name");
public List<PrimaryHealthCareCenterTO> listCAPsPaged(int pageNumber, int pageSize) {
return this.listCAPsFiltered(null, pageNumber, pageSize);
}
public List<PrimaryHealthCareCenterTO> 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.normalizeTerm(searchTerm);
if (searchTerm.length() > 0) {
strFilter = "WHERE lower(phc.name) LIKE :searchTerm OR lower(phc.location) LIKE :searchTerm";
}
TypedQuery<PrimaryHealthCareCenterJPA> 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);
}
@SuppressWarnings("unchecked")
Collection<PrimaryHealthCareCenterJPA> allJPA = query.getResultList();
Collection<PrimaryHealthCareCenterTO> allCAPs = new ArrayList<PrimaryHealthCareCenterTO>();
List<PrimaryHealthCareCenterJPA> allJPA = query.getResultList();
List<PrimaryHealthCareCenterTO> allCAPs = new ArrayList<PrimaryHealthCareCenterTO>();
for (PrimaryHealthCareCenterJPA cap : allJPA) {
allCAPs.add(new PrimaryHealthCareCenterTO(cap.getName(), cap.getLocation()));
for (PrimaryHealthCareCenterJPA item : allJPA) {
allCAPs.add(this.getPOJOforPrimaryHealthCareCenterJPA(item));
}
return allCAPs;
}
public Collection<FamilyDoctorTO> listAllFamilyDoctors() {
return this.listAllFamilyDoctors(0, 0);
public List<FamilyDoctorTO> listAllFamilyDoctors() {
return this.listFamilyDoctorsPaged(0, 0);
}
public Collection<FamilyDoctorTO> listAllFamilyDoctors(int pageNumber, int pageSize) {
Query query = entman.createQuery("from FamilyDoctorJPA order by name, surname");
public List<FamilyDoctorTO> listFamilyDoctorsPaged(int pageNumber, int pageSize) {
return this.listFamilyDoctorsFiltered(null, pageNumber, pageSize);
}
public List<FamilyDoctorTO> 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.normalizeTerm(searchTerm);
if (searchTerm.length() > 0) {
strFilter = "WHERE lower(fd.name) LIKE :searchTerm OR lower(fd.surname) LIKE :searchTerm";
}
TypedQuery<FamilyDoctorJPA> 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);
}
@SuppressWarnings("unchecked")
Collection<FamilyDoctorJPA> allFDsJPA = query.getResultList();
Collection<FamilyDoctorTO> allFDTOs = new ArrayList<FamilyDoctorTO>();
List<FamilyDoctorJPA> allFDsJPA = query.getResultList();
List<FamilyDoctorTO> allFDTOs = new ArrayList<FamilyDoctorTO>();
for (FamilyDoctorJPA item : allFDsJPA) {
allFDTOs.add(new FamilyDoctorTO(item.getId(), item.getNif(), item.getName(), item.getSurname(), item.getPassword(), item.getEmail(), null));
allFDTOs.add(this.getPOJOforFamilyDoctorJPA(item, 0));
}
return allFDTOs;
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));
public MedicalSpecialtyTO getPOJOforMedicalSpecialtyJPA(MedicalSpecialtyJPA ms) {
MedicalSpecialtyTO msTO = null;
if (ms != null) {
msTO = new MedicalSpecialtyTO(ms.getId(), ms.getName(), ms.getDescription());
}
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;
return msTO;
}
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));
public PrimaryHealthCareCenterTO getPOJOforPrimaryHealthCareCenterJPA(PrimaryHealthCareCenterJPA phc) {
PrimaryHealthCareCenterTO phcTO = null;
if (phc != null) {
phcTO = new PrimaryHealthCareCenterTO(phc.getId(), phc.getName(), phc.getLocation());
}
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;
return phcTO;
}
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));
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));
}
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;
}
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<PatientJPA> query = entman.createQuery("from PatientJPA pat where pat.personalIdentificationCode=:code", PatientJPA.class);
query.setParameter("code", code);
List<PatientJPA> 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<FamilyDoctorJPA> query = entman.createQuery("from FamilyDoctorJPA d where d.professionalNumber=:code", FamilyDoctorJPA.class);
query.setParameter("code", code);
List<FamilyDoctorJPA> 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<SpecialistDoctorJPA> query = entman.createQuery("from SpecialistDoctorJPA d where d.professionalNumber=:code", SpecialistDoctorJPA.class);
query.setParameter("code", code);
List<SpecialistDoctorJPA> results = query.getResultList();
if (results.size() > 0)
return results.get(0);
else
return null;
}
}

View File

@@ -1,6 +1,7 @@
package ejb.common;
import java.util.Collection;
import java.util.List;
import javax.ejb.Local;
@@ -9,6 +10,11 @@ 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;
/**
*
@@ -18,21 +24,44 @@ import TO.SpecialistDoctorTO;
@Local
public interface CommonFacadeLocal {
public Collection<MedicalSpecialtyTO> listAllMedicalSpecialities();
public List<MedicalSpecialtyTO> listAllMedicalSpecialities();
public Collection<MedicalSpecialtyTO> listPagedMedicalSpecialities(int pageNumber, int pageSize);
public List<MedicalSpecialtyTO> listMedicalSpecialitiesPaged(int pageNumber, int pageSize);
public Collection<PrimaryHealthCareCenterTO> listAllCAPs();
public Collection<PrimaryHealthCareCenterTO> listPagedAllCAPs(int pageNumber, int pageSize);
public List<MedicalSpecialtyTO> listMedicalSpecialitiesFiltered(String searchTerm, int pageNumber, int pageSize);
public Collection<FamilyDoctorTO> listAllFamilyDoctors();
public Collection<FamilyDoctorTO> listAllFamilyDoctors(int pageNumber, int pageSize);
public PatientTO retrievePatient(int patientId) throws Exception;
public List<PrimaryHealthCareCenterTO> listAllCAPs();
public FamilyDoctorTO retrieveFamilyDoctor(int ProfessionalNumberId) throws Exception;
public List<PrimaryHealthCareCenterTO> listCAPsPaged(int pageNumber, int pageSize);
public List<PrimaryHealthCareCenterTO> listCAPsFiltered(String searchTerm, int pageNumber, int pageSize);
public List<FamilyDoctorTO> listAllFamilyDoctors();
public List<FamilyDoctorTO> listFamilyDoctorsPaged(int pageNumber, int pageSize);
public List<FamilyDoctorTO> listFamilyDoctorsFiltered(String searchTerm, int pageNumber, int pageSize);
public PatientTO findPatientById(int patientId);
public PatientJPA findPatientByCode(String code);
public FamilyDoctorTO findFamilyDoctorById(int ProfessionalNumberId);
public FamilyDoctorJPA findFamilyDoctorByCode(String code);
public SpecialistDoctorTO findSpecialistDoctorById(int ProfessionalNumberId);
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);
public SpecialistDoctorTO retrieveSpecialistDoctor(int ProfessionalNumberId) throws Exception;
}

View File

@@ -1,6 +1,7 @@
package ejb.common;
import java.util.Collection;
import java.util.List;
import javax.ejb.Remote;
@@ -9,6 +10,9 @@ import TO.MedicalSpecialtyTO;
import TO.PatientTO;
import TO.PrimaryHealthCareCenterTO;
import TO.SpecialistDoctorTO;
import jpa.FamilyDoctorJPA;
import jpa.PatientJPA;
import jpa.SpecialistDoctorJPA;
/**
*
@@ -18,21 +22,34 @@ import TO.SpecialistDoctorTO;
@Remote
public interface CommonFacadeRemote {
public Collection<MedicalSpecialtyTO> listAllMedicalSpecialities();
public List<MedicalSpecialtyTO> listAllMedicalSpecialities();
public Collection<MedicalSpecialtyTO> listPagedMedicalSpecialities(int pageNumber, int pageSize);
public List<MedicalSpecialtyTO> listMedicalSpecialitiesPaged(int pageNumber, int pageSize);
public Collection<PrimaryHealthCareCenterTO> listAllCAPs();
public Collection<PrimaryHealthCareCenterTO> listPagedAllCAPs(int pageNumber, int pageSize);
public List<MedicalSpecialtyTO> listMedicalSpecialitiesFiltered(String searchTerm, int pageNumber, int pageSize);
public Collection<FamilyDoctorTO> listAllFamilyDoctors();
public Collection<FamilyDoctorTO> listAllFamilyDoctors(int pageNumber, int pageSize);
public PatientTO retrievePatient(int patientId) throws Exception;
public List<PrimaryHealthCareCenterTO> listAllCAPs();
public FamilyDoctorTO retrieveFamilyDoctor(int ProfessionalNumberId) throws Exception;
public List<PrimaryHealthCareCenterTO> listCAPsPaged(int pageNumber, int pageSize);
public List<PrimaryHealthCareCenterTO> listCAPsFiltered(String searchTerm, int pageNumber, int pageSize);
public List<FamilyDoctorTO> listAllFamilyDoctors();
public List<FamilyDoctorTO> listFamilyDoctorsPaged(int pageNumber, int pageSize);
public List<FamilyDoctorTO> listFamilyDoctorsFiltered(String searchTerm, int pageNumber, int pageSize);
public PatientTO findPatientById(int patientId);
public PatientJPA findPatientByCode(String code);
public FamilyDoctorTO findFamilyDoctorById(int ProfessionalNumberId);
public FamilyDoctorJPA findFamilyDoctorByCode(String code);
public SpecialistDoctorTO findSpecialistDoctorById(int ProfessionalNumberId);
public SpecialistDoctorJPA findSpecialistDoctorByCode(String code);
public SpecialistDoctorTO retrieveSpecialistDoctor(int ProfessionalNumberId) throws Exception;
}

View File

@@ -4,16 +4,16 @@ import javax.ejb.EJB;
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 common.Constants;
import common.HashUtils;
import ejb.common.CommonFacadeBean;
import ejb.common.CommonFacadeLocal;
import ejb.common.CommonFacadeRemote;
import jpa.FamilyDoctorJPA;
import jpa.MedicalSpecialtyJPA;
import jpa.PatientJPA;
@@ -31,70 +31,72 @@ 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 {
@EJB
CommonFacadeLocal commonServices;
private String getNextProfessionalNumber() {
Query q = entman.createNativeQuery("select nextval('myhealth.profesionalnumber')");
return Constants.PROFESSIONAL_NUMBER_PREFIX.concat(String.valueOf(q.getSingleResult()));
}
private String getNextPersonalIdentificationCode() {
Query q = entman.createNativeQuery("select nextval('myhealth.profesionalnumber')");
return Constants.PERSONAL_IDENTIFICATION_CODE_PREFIX.concat(String.valueOf(q.getSingleResult()));
}
public PatientTO changeFamilyDoctor(int patientId, int newDoctor) 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));
}
FamilyDoctorJPA fd = entman.find(FamilyDoctorJPA.class, ProfessionalNumberId);
FamilyDoctorJPA fd = entman.find(FamilyDoctorJPA.class, newDoctor);
if (fd == 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(ProfessionalNumberId));
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(newDoctor));
}
PatientTO paTO = null;
pat.setFamilyDoctor(fd);
entman.persist(pat);
//CommonFacadeRemote common = new CommonFacadeBean();
return commonServices.retrievePatient(pat.getId());
return this.commonServices.getPOJOforPatientJPA(pat, 1);
}
public PatientTO registerPatient(int id, String nif, String name, String surname, String password, String email) {
PatientTO paTO = null;
PatientJPA pat = new PatientJPA(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);
paTO = new PatientTO(pat.getId(), 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)
throws Exception {
MedicalSpecialtyJPA ms = entman.find(MedicalSpecialtyJPA.class, specialty.getName());
MedicalSpecialtyJPA ms = entman.find(MedicalSpecialtyJPA.class, specialty.getId());
if (ms == null) {
throw new Exception("No se encuentra la especialidad médica con identificador: " + specialty.getName());
}
SpecialistDoctorJPA sd = new SpecialistDoctorJPA(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);
SpecialistDoctorTO sdTO = null;
sdTO = new SpecialistDoctorTO(sd.getId(), 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 {
FamilyDoctorTO fdTO = null;
PrimaryHealthCareCenterJPA phcC = entman.find(PrimaryHealthCareCenterJPA.class, cap.getName());
PrimaryHealthCareCenterJPA phcC = entman.find(PrimaryHealthCareCenterJPA.class, cap.getId());
if (phcC == null) {
throw new Exception("No se encuentra el centro de atención primaria con identificador: " + cap.getName());
}
FamilyDoctorJPA fd = new FamilyDoctorJPA(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);
fdTO = new FamilyDoctorTO(fd.getId(), 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 {
@@ -114,10 +116,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
entman.persist(pat);
PatientTO patTO = null;
patTO = new PatientTO(pat.getId(), 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)
@@ -127,7 +126,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
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(id));
}
MedicalSpecialtyJPA ms = entman.find(MedicalSpecialtyJPA.class, specialty.getName());
MedicalSpecialtyJPA ms = entman.find(MedicalSpecialtyJPA.class, specialty.getId());
if (ms == null) {
throw new Exception("No se encuentra la especialidad médica con identificador: " + specialty.getName());
}
@@ -143,10 +142,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
entman.persist(sd);
SpecialistDoctorTO sdTO = null;
sdTO = new SpecialistDoctorTO(sd.getId(), 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 {
@@ -155,7 +151,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
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(id));
}
PrimaryHealthCareCenterJPA phcC = entman.find(PrimaryHealthCareCenterJPA.class, phcTO.getName());
PrimaryHealthCareCenterJPA phcC = entman.find(PrimaryHealthCareCenterJPA.class, phcTO.getId());
if (phcC == null) {
throw new Exception("No se encuentra el centro de atención primaria con identificador: " + phcTO.getName());
}
@@ -171,10 +167,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
entman.persist(fd);
FamilyDoctorTO fdTO = null;
fdTO = new FamilyDoctorTO(fd.getId(), 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 {
@@ -185,15 +178,14 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
throw new Exception("No se encuentra en la base de datos ningún médico de familia con id: " + String.valueOf(professionalId));
}
PrimaryHealthCareCenterJPA phcC = entman.find(PrimaryHealthCareCenterJPA.class, newCenter.getName());
PrimaryHealthCareCenterJPA phcC = entman.find(PrimaryHealthCareCenterJPA.class, newCenter.getId());
if (phcC == null) {
throw new Exception("No se encuentra el centro de atención primaria con identificador: " + newCenter.getName());
}
fd.setPrimaryHealthCareCenter(phcC);
entman.persist(fd);
fdTO = new FamilyDoctorTO(fd.getId(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), newCenter);
return fdTO;
return this.commonServices.getPOJOforFamilyDoctorJPA(fd, 1);
}
}

View File

@@ -16,7 +16,7 @@ import TO.SpecialistDoctorTO;
@Remote
public interface ProfileFacadeRemote {
public PatientTO changeFamilyDoctor(int id, int ProfessionalNumberId) throws Exception;
public PatientTO changeFamilyDoctor(int id, int newDoctor) throws Exception;
public PatientTO registerPatient(int id, String nif, String name, String surname, String password, String email);

View File

@@ -1,23 +1,18 @@
package ejb.systemAdmin;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import TO.LoggedUserTO;
import TO.MedicalSpecialtyTO;
import TO.PrimaryHealthCareCenterTO;
import common.Constants;
import common.HashUtils;
import common.UserType;
import ejb.common.CommonFacadeLocal;
import jpa.AdministratorJPA;
import jpa.FamilyDoctorJPA;
import jpa.MedicalSpecialtyJPA;
import jpa.PatientJPA;
import jpa.PrimaryHealthCareCenterJPA;
import jpa.SpecialistDoctorJPA;
/**
@@ -32,49 +27,80 @@ public class SystemAdminFacadeBean implements SystemAdminFacadeRemote {
@PersistenceContext(unitName = "MyHealth")
private EntityManager entman;
public LoggedUserTO login(String id, String pwd) {
@EJB
CommonFacadeLocal commonServices;
/**
* Si la autenticación no es correcgta devuelve null, sino devuelve un POJO con
* datos del usuario logeado.
*
* La autenticación se realiza en 2 pasos:
*
* Paso 1. Se intenta localizar un registro de usuario, por orden:
*
* a. Primero Paciente, si el identificador comienza por los caracteres
* correctos.
*
* b.Después médico de familia, si el identificador es de profesional
*
* c. Si no lo localizamos buscamos el identificador en la tabla de médicos
* especialistas (el identificador es de profesional)
*
* d. Si no hemos localizado aún al usuario, lo buscamos en la tabla de
* administradores, aún cuando el identificador comience por cualquier carácter
* (podría ser una dirección de email que comienza por caracteres del
* identificaodr de paciente o profesional)
*
* Paso 2. Si hemos localizado un registro de usuario, verificamos si el
* password recibido coincide con el de la base de datos, en tal caso la
* autenticación se compelta y es correcta.
*/
public LoggedUserTO login(String userCode, String pwd) {
LoggedUserTO usr = null;
// First try to login as Admin
AdministratorJPA adm = entman.find(AdministratorJPA.class, id);
if (adm != null) {
usr = new LoggedUserTO(adm.getEmail(), adm.getEmail(), adm.getPassword(), UserType.ADMINISTRATOR);
} else {
try {
int iId = Integer.parseInt(id);
// Try to login Patient, FamilyDoctor or SpecialistDoctor
// TODO: Si Patient, FamilyDoctor o Specialist Doctor tienen el mismo id, solo
// el paciente se puede logear. ¿Deberíamos cambiar esto permitiendo seleccionar
// el perfil a logearse?
PatientJPA pat = entman.find(PatientJPA.class, iId);
if (pat != null) {
usr = new LoggedUserTO(String.valueOf(pat.getId()), pat.getName(), pat.getPassword(), UserType.PATIENT);
} else {
FamilyDoctorJPA fdoc = entman.find(FamilyDoctorJPA.class, iId);
if (fdoc != null) {
usr = new LoggedUserTO(String.valueOf(fdoc.getId()), fdoc.getName(), fdoc.getPassword(), UserType.FAMILY_DOCTOR);
} else {
SpecialistDoctorJPA sdoc = entman.find(SpecialistDoctorJPA.class, iId);
if (sdoc != null) {
usr = new LoggedUserTO(String.valueOf(sdoc.getId()), sdoc.getName(), sdoc.getPassword(), UserType.SPECIALIST_DOCTOR);
}
}
}
} catch (NumberFormatException nf) {
// id is not an intenger, so, login fails
usr = null;
if (userCode.startsWith(Constants.PERSONAL_IDENTIFICATION_CODE_PREFIX)) {
// Si el identificador de usuario es de tipo paciente, intentamos realizar el
// login.
PatientJPA pat = this.commonServices.findPatientByCode(userCode);
if (pat != null) {
usr = new LoggedUserTO(String.valueOf(pat.getId()), pat.getName(), pat.getPassword(), UserType.PATIENT);
}
} else if (userCode.startsWith(Constants.PROFESSIONAL_NUMBER_PREFIX)) {
// Si el identificador de usuario es de tipo profesional, intentamos realizar el
// login, primero como médico de familia, después como especialista
FamilyDoctorJPA fd = this.commonServices.findFamilyDoctorByCode(userCode);
if (fd != null) {
usr = new LoggedUserTO(String.valueOf(fd.getId()), fd.getName(), fd.getPassword(), UserType.FAMILY_DOCTOR);
} else {
// No era un código de médico de familia, intenamos logearlo como especialista
SpecialistDoctorJPA sd = this.commonServices.findSpecialistDoctorByCode(userCode);
if (sd != null) {
usr = new LoggedUserTO(String.valueOf(sd.getId()), sd.getName(), sd.getPassword(), UserType.SPECIALIST_DOCTOR);
}
}
}
// Si todavía no hemos conseguido autenticar al usuario, podría tratarse de una
// dirección de email de un administrador que empiece por
// PERSONAL_IDENTIFICATION_CODE_PREFIX o por PROFESSIONAL_NUMBER_PREFIX, por lo
// cual intentamos login contra la tabla de administradores
if (usr == null) {
// Intentamos recuperar un registro de administrador
AdministratorJPA adm = entman.find(AdministratorJPA.class, userCode);
if (adm != null) {
usr = new LoggedUserTO(adm.getEmail(), adm.getEmail(), adm.getPassword(), UserType.ADMINISTRATOR);
}
}
// Si el objeto usr, es que hemos localizado un registro de usuario, verificamos
// la contraseña.
if (usr != null) {
// Comprobamos el password
if (usr.getPassword().equals(HashUtils.hashMD5(pwd)) == false) {
// Bad Password, devolvemos null!
// Bad Password, devolvemos null! La autenticación falla.
usr = null;
}
}