Multiples cambios para soportar la utilización de códigos de usuario

únicos para (Pacientes, medicos de familia, medicos especialista y
administradores).
Nuevos métodos en EJB común para consultar Entidades por Id y por código
(Para el login).
This commit is contained in:
Marcos Garcia Nuñez
2019-12-11 21:47:44 +01:00
parent acdc195828
commit 28b1192036
19 changed files with 366 additions and 164 deletions

View File

@@ -14,6 +14,7 @@ public class FamilyDoctorTO implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String professionalNumber;
private String nif;
private String name;
private String surname;
@@ -25,8 +26,9 @@ public class FamilyDoctorTO implements Serializable {
super();
}
public FamilyDoctorTO(Integer id, String nif, String name, String surname, String password, String email, PrimaryHealthCareCenterTO phc) {
public FamilyDoctorTO(Integer id, String pic, String nif, String name, String surname, String password, String email, PrimaryHealthCareCenterTO phc) {
this.id = id;
this.professionalNumber = pic;
this.nif = nif;
this.name = name;
this.surname = surname;
@@ -95,4 +97,11 @@ public class FamilyDoctorTO implements Serializable {
this.primaryHealthCareCenter = primaryHealthCareCenter;
}
public String getProfessionalNumber() {
return professionalNumber;
}
public void setProfessionalNumber(String professionalNumber) {
this.professionalNumber = professionalNumber;
}
}

View File

@@ -16,6 +16,7 @@ public class PatientTO implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String personalIdentificationCode;
private String nif;
private String name;
private String surname;
@@ -30,8 +31,9 @@ public class PatientTO implements Serializable {
super();
}
public PatientTO(Integer id, String nif, String name, String surname, String password, String email, FamilyDoctorTO familyDoc) {
public PatientTO(Integer id, String pic, String nif, String name, String surname, String password, String email, FamilyDoctorTO familyDoc) {
this.id = id;
this.personalIdentificationCode = pic;
this.nif = nif;
this.name = name;
this.surname = surname;
@@ -96,5 +98,11 @@ public class PatientTO implements Serializable {
this.familyDoctor = familyDoctor;
}
public String getPersonalIdentificationCode() {
return personalIdentificationCode;
}
public void setPersonalIdentificationCode(String personalIdentificationCode) {
this.personalIdentificationCode = personalIdentificationCode;
}
}

View File

@@ -14,6 +14,7 @@ public class SpecialistDoctorTO implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String professionalNumber;
private String nif;
private String name;
private String surname;
@@ -25,8 +26,9 @@ public class SpecialistDoctorTO implements Serializable {
super();
}
public SpecialistDoctorTO(Integer id, String nif, String name, String surname, String password, String email, MedicalSpecialtyTO medicalSpec) {
public SpecialistDoctorTO(Integer id, String pic, String nif, String name, String surname, String password, String email, MedicalSpecialtyTO medicalSpec) {
this.id = id;
this.professionalNumber = pic;
this.nif = nif;
this.name = name;
this.surname = surname;
@@ -91,4 +93,11 @@ public class SpecialistDoctorTO implements Serializable {
this.medicalSpecialty = medicalSpecialty;
}
public String getProfessionalNumber() {
return professionalNumber;
}
public void setProfessionalNumber(String professionalNumber) {
this.professionalNumber = professionalNumber;
}
}

View File

@@ -2,4 +2,6 @@ package common;
public class Constants {
public static final int MAX_ITEMS_AUTOCOMPLETE_SEARCH = 200;
public static final String PROFESSIONAL_NUMBER_PREFIX= "PRO#";
public static final String PERSONAL_IDENTIFICATION_CODE_PREFIX= "PAT#";
}

View File

@@ -1,7 +1,6 @@
package ejb.common;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
@@ -10,6 +9,8 @@ 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;
@@ -71,8 +72,8 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal {
List<MedicalSpecialtyJPA> allJPA = query.getResultList();
List<MedicalSpecialtyTO> allSpecialities = new ArrayList<MedicalSpecialtyTO>();
for (MedicalSpecialtyJPA cap : allJPA) {
allSpecialities.add(new MedicalSpecialtyTO(cap.getId(), cap.getName(), cap.getDescription()));
for (MedicalSpecialtyJPA item : allJPA) {
allSpecialities.add(this.getPOJOforMedicalSpecialtyJPA(item));
}
return allSpecialities;
@@ -111,8 +112,8 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal {
List<PrimaryHealthCareCenterJPA> allJPA = query.getResultList();
List<PrimaryHealthCareCenterTO> allCAPs = new ArrayList<PrimaryHealthCareCenterTO>();
for (PrimaryHealthCareCenterJPA cap : allJPA) {
allCAPs.add(new PrimaryHealthCareCenterTO(cap.getId(), cap.getName(), cap.getLocation()));
for (PrimaryHealthCareCenterJPA item : allJPA) {
allCAPs.add(this.getPOJOforPrimaryHealthCareCenterJPA(item));
}
return allCAPs;
@@ -148,60 +149,127 @@ public class CommonFacadeBean implements CommonFacadeRemote, CommonFacadeLocal {
query.setMaxResults(pageSize);
}
@SuppressWarnings("unchecked")
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;
}
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));
private 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));
private 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().getId(), 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));
private 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().getId(), 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;
}
private 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;
}
private 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

@@ -10,6 +10,9 @@ import TO.MedicalSpecialtyTO;
import TO.PatientTO;
import TO.PrimaryHealthCareCenterTO;
import TO.SpecialistDoctorTO;
import jpa.FamilyDoctorJPA;
import jpa.PatientJPA;
import jpa.SpecialistDoctorJPA;
/**
*
@@ -37,9 +40,16 @@ public interface CommonFacadeLocal {
public List<FamilyDoctorTO> listFamilyDoctorsFiltered(String searchTerm, int pageNumber, int pageSize);
public PatientTO retrievePatient(int patientId) throws Exception;
public PatientTO findPatientById(int patientId);
public FamilyDoctorTO retrieveFamilyDoctor(int ProfessionalNumberId) throws Exception;
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

@@ -10,6 +10,9 @@ import TO.MedicalSpecialtyTO;
import TO.PatientTO;
import TO.PrimaryHealthCareCenterTO;
import TO.SpecialistDoctorTO;
import jpa.FamilyDoctorJPA;
import jpa.PatientJPA;
import jpa.SpecialistDoctorJPA;
/**
*
@@ -37,9 +40,16 @@ public interface CommonFacadeRemote {
public List<FamilyDoctorTO> listFamilyDoctorsFiltered(String searchTerm, int pageNumber, int pageSize);
public PatientTO retrievePatient(int patientId) throws Exception;
public PatientTO findPatientById(int patientId);
public FamilyDoctorTO retrieveFamilyDoctor(int ProfessionalNumberId) throws Exception;
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,36 +31,46 @@ 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());
// CommonFacadeRemote common = new CommonFacadeBean();
return commonServices.findPatientById(pat.getId());
}
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);
paTO = new PatientTO(pat.getId(), this.getNextPersonalIdentificationCode(), pat.getNif(), pat.getName(), pat.getSurname(), pat.getPassword(), pat.getEmail(), null);
return paTO;
}
@@ -72,11 +82,11 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
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);
sdTO = new SpecialistDoctorTO(sd.getId(), this.getNextProfessionalNumber(), sd.getNif(), sd.getName(), sd.getSurname(), sd.getPassword(), sd.getEmail(), specialty);
return sdTO;
}
@@ -89,10 +99,10 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
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);
fdTO = new FamilyDoctorTO(fd.getId(), this.getNextProfessionalNumber(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), cap);
return fdTO;
}
@@ -115,7 +125,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);
patTO = new PatientTO(pat.getId(), pat.getPersonalIdentificationCode(), pat.getNif(), pat.getName(), pat.getSurname(), pat.getPassword(), pat.getEmail(), null);
return patTO;
}
@@ -144,7 +154,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);
sdTO = new SpecialistDoctorTO(sd.getId(), sd.getProfessionalNumber(), sd.getNif(), sd.getName(), sd.getSurname(), sd.getPassword(), sd.getEmail(), specialty);
return sdTO;
}
@@ -172,7 +182,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);
fdTO = new FamilyDoctorTO(fd.getId(), fd.getProfessionalNumber(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), phcTO);
return fdTO;
}
@@ -192,7 +202,7 @@ public class ProfileFacadeBean implements ProfileFacadeRemote {
fd.setPrimaryHealthCareCenter(phcC);
entman.persist(fd);
fdTO = new FamilyDoctorTO(fd.getId(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), newCenter);
fdTO = new FamilyDoctorTO(fd.getId(), fd.getProfessionalNumber(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail(), newCenter);
return fdTO;
}

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;
}
}

View File

@@ -30,6 +30,8 @@ public class FamilyDoctorJPA implements Serializable {
@Column(updatable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false, unique = true)
private String professionalNumber;
private String nif;
private String name;
private String surname;
@@ -49,7 +51,8 @@ public class FamilyDoctorJPA implements Serializable {
super();
}
public FamilyDoctorJPA(String nif, String name, String surname, String password, String email, PrimaryHealthCareCenterJPA phc) {
public FamilyDoctorJPA(String pin, String nif, String name, String surname, String password, String email, PrimaryHealthCareCenterJPA phc) {
this.setProfessionalNumber(pin);
this.nif = nif;
this.name = name;
this.surname = surname;
@@ -124,4 +127,12 @@ public class FamilyDoctorJPA implements Serializable {
public void setPrimaryHealthCareCenter(PrimaryHealthCareCenterJPA center) {
this.primaryHealthCareCenter = center;
}
public String getProfessionalNumber() {
return professionalNumber;
}
public void setProfessionalNumber(String professionalNumber) {
this.professionalNumber = professionalNumber;
}
}

View File

@@ -26,6 +26,8 @@ public class PatientJPA implements Serializable {
@Column(updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false, unique = true)
private String personalIdentificationCode;
private String nif;
private String name;
private String surname;
@@ -42,7 +44,8 @@ public class PatientJPA implements Serializable {
super();
}
public PatientJPA(String nif, String name, String surname, String password, String email, FamilyDoctorJPA familyDoc) {
public PatientJPA(String pic, String nif, String name, String surname, String password, String email, FamilyDoctorJPA familyDoc) {
this.personalIdentificationCode = pic;
this.nif = nif;
this.name = name;
this.surname = surname;
@@ -106,4 +109,12 @@ public class PatientJPA implements Serializable {
public void setFamilyDoctor(FamilyDoctorJPA familyDoc) {
this.familyDoctor = familyDoc;
}
public String getPersonalIdentificationCode() {
return personalIdentificationCode;
}
public void setPersonalIdentificationCode(String personalIdentificationCode) {
this.personalIdentificationCode = personalIdentificationCode;
}
}

View File

@@ -26,6 +26,8 @@ public class SpecialistDoctorJPA implements Serializable {
@Column(updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false, unique = true)
private String professionalNumber;
private String nif;
private String name;
private String surname;
@@ -42,7 +44,8 @@ public class SpecialistDoctorJPA implements Serializable {
super();
}
public SpecialistDoctorJPA(String nif, String name, String surname, String password, String email, MedicalSpecialtyJPA ms) {
public SpecialistDoctorJPA(String pic, String nif, String name, String surname, String password, String email, MedicalSpecialtyJPA ms) {
this.setProfessionalNumber(pic);
this.nif = nif;
this.name = name;
this.surname = surname;
@@ -106,4 +109,12 @@ public class SpecialistDoctorJPA implements Serializable {
public void setMedicalSpecialty(MedicalSpecialtyJPA specialty) {
this.medicalSpecialty = specialty;
}
public String getProfessionalNumber() {
return professionalNumber;
}
public void setProfessionalNumber(String professionalNumber) {
this.professionalNumber = professionalNumber;
}
}

View File

@@ -53,7 +53,7 @@ public class ChangeFamilyDoctorMBean extends ManagedBeanBase implements Serializ
if (usr.getUserType() == UserType.PATIENT) {
this.familyDoctorList = this.getRemoteManagerCommon().listFamilyDoctorsPaged(0, Constants.MAX_ITEMS_AUTOCOMPLETE_SEARCH);
PatientTO pat = this.getRemoteManagerCommon().retrievePatient(this.id);
PatientTO pat = this.getRemoteManagerCommon().findPatientById(this.id);
this.setCurrentFamilyDoctor(pat.getFamilyDoctor());
}
}

View File

@@ -55,7 +55,7 @@ public class ChangePrimaryHealthCareCenterMBean extends ManagedBeanBase implemen
if (usr.getUserType() == UserType.FAMILY_DOCTOR) {
this.primaryHealthCareCentersList = this.getRemoteManagerCommon().listCAPsPaged(0, Constants.MAX_ITEMS_AUTOCOMPLETE_SEARCH);
FamilyDoctorTO fd = this.getRemoteManagerCommon().retrieveFamilyDoctor(this.id);
FamilyDoctorTO fd = this.getRemoteManagerCommon().findFamilyDoctorById(this.id);
this.setCurrentCenter(fd.getPrimaryHealthCareCenter());
}
}

View File

@@ -36,8 +36,8 @@ import managedbean.common.ValidationUtils;
public class RegisterUserMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String cipCode;
private String nif;
private String name;
private String surname;
@@ -185,14 +185,6 @@ public class RegisterUserMBean extends ManagedBeanBase implements Serializable {
this.nif = nif;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void addNewUser() {
int error = 0;
@@ -214,17 +206,17 @@ public class RegisterUserMBean extends ManagedBeanBase implements Serializable {
switch (UserType.valueOf(this.userType)) {
case PATIENT:
PatientTO pat = this.getRemoteManagerProfile().registerPatient(id, nif, name, surname, password, email);
this.id = pat.getId();
this.cipCode = pat.getPersonalIdentificationCode();
break;
case FAMILY_DOCTOR:
FamilyDoctorTO fd = this.getRemoteManagerProfile().registerFamilyDoctor(id, nif, name, surname, password, email, this.primaryHealthCareCenter);
this.id = fd.getId();
this.cipCode = fd.getProfessionalNumber();
break;
case SPECIALIST_DOCTOR:
SpecialistDoctorTO sd = this.getRemoteManagerProfile().registerSpecialistDoctor(id, nif, name, surname, password, email, this.medicalSpecialty);
this.id = sd.getId();
this.cipCode = sd.getProfessionalNumber();
break;
case ADMINISTRATOR:
@@ -268,4 +260,12 @@ public class RegisterUserMBean extends ManagedBeanBase implements Serializable {
return registered;
}
public String getCipCode() {
return cipCode;
}
public void setCipCode(String cipCode) {
this.cipCode = cipCode;
}
}

View File

@@ -40,6 +40,7 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable
private static final long serialVersionUID = 1L;
private int id;
private String cipCode;
private String nif;
private String name;
private String surname;
@@ -88,35 +89,15 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable
switch (usr.getUserType()) {
case PATIENT:
this.familyDoctorList = this.getRemoteManagerCommon().listFamilyDoctorsPaged(0, Constants.MAX_ITEMS_AUTOCOMPLETE_SEARCH);
PatientTO pat = this.getRemoteManagerCommon().retrievePatient(this.id);
this.name = pat.getName();
this.surname = pat.getName();
this.nif = pat.getNif();
this.email = pat.getEmail();
this.currentPassword = pat.getPassword();
this.setPatientData(this.getRemoteManagerCommon().findPatientById(this.id));
break;
case SPECIALIST_DOCTOR:
this.medicalSpecialitiesList = this.getRemoteManagerCommon().listMedicalSpecialitiesPaged(0, Constants.MAX_ITEMS_AUTOCOMPLETE_SEARCH);
SpecialistDoctorTO sd = this.getRemoteManagerCommon().retrieveSpecialistDoctor(this.id);
this.name = sd.getName();
this.surname = sd.getName();
this.nif = sd.getNif();
this.email = sd.getEmail();
this.currentPassword = sd.getPassword();
this.medicalSpecialty = sd.getMedicalSpecialty();
this.setSpecialistDoctorData(this.getRemoteManagerCommon().findSpecialistDoctorById(this.id));
break;
case FAMILY_DOCTOR:
this.primaryHealthCareCentersList = this.getRemoteManagerCommon().listCAPsPaged(0, Constants.MAX_ITEMS_AUTOCOMPLETE_SEARCH);
FamilyDoctorTO fd = this.getRemoteManagerCommon().retrieveFamilyDoctor(this.id);
this.name = fd.getName();
this.surname = fd.getName();
this.nif = fd.getNif();
this.email = fd.getEmail();
this.currentPassword = fd.getPassword();
this.primaryHealthCareCenter = fd.getPrimaryHealthCareCenter();
this.setFamilyDoctorData(this.getRemoteManagerCommon().findFamilyDoctorById(this.id));
break;
case ADMINISTRATOR:
// TODO: Recuperar usuario administrador para editar su perfil ¿?
@@ -127,7 +108,39 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable
} catch (Exception e) {
this.manageException(e);
}
}
private void setPatientData(PatientTO pat) {
this.id = pat.getId();
this.cipCode = pat.getPersonalIdentificationCode();
this.name = pat.getName();
this.surname = pat.getName();
this.nif = pat.getNif();
this.email = pat.getEmail();
this.currentPassword = pat.getPassword();
this.familyDoctor = pat.getFamilyDoctor();
}
private void setFamilyDoctorData(FamilyDoctorTO fd) {
this.id = fd.getId();
this.cipCode = fd.getProfessionalNumber();
this.name = fd.getName();
this.surname = fd.getName();
this.nif = fd.getNif();
this.email = fd.getEmail();
this.currentPassword = fd.getPassword();
this.primaryHealthCareCenter = fd.getPrimaryHealthCareCenter();
}
private void setSpecialistDoctorData(SpecialistDoctorTO sd) {
this.id = sd.getId();
this.cipCode = sd.getProfessionalNumber();
this.name = sd.getName();
this.surname = sd.getName();
this.nif = sd.getNif();
this.email = sd.getEmail();
this.currentPassword = sd.getPassword();
this.medicalSpecialty = sd.getMedicalSpecialty();
}
public List<UserType> getUserTypes() {
@@ -290,17 +303,17 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable
switch (UserType.valueOf(this.userType)) {
case PATIENT:
PatientTO pat = this.getRemoteManagerProfile().updatePatientData(id, nif, name, surname, password, email);
this.id = pat.getId();
this.setPatientData(pat);
break;
case FAMILY_DOCTOR:
FamilyDoctorTO fd = this.getRemoteManagerProfile().updateFamilyDoctorData(id, nif, name, surname, password, email, this.primaryHealthCareCenter);
this.id = fd.getId();
this.setFamilyDoctorData(fd);
break;
case SPECIALIST_DOCTOR:
SpecialistDoctorTO sd = this.getRemoteManagerProfile().updateSpecialistDoctorData(id, nif, name, surname, password, email, this.medicalSpecialty);
this.id = sd.getId();
this.setSpecialistDoctorData(sd);
break;
case ADMINISTRATOR:
@@ -360,4 +373,8 @@ public class UpdateProfileMBean extends ManagedBeanBase implements Serializable
this.familyDoctor = familyDoctor;
}
public String getCipCode() {
return cipCode;
}
}