Files
myhealth/1.sources/MyHealth/src/ejb/systemAdmin/SystemAdminFacadeBean.java
2019-12-09 21:18:53 +01:00

141 lines
4.2 KiB
Java

package ejb.systemAdmin;
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.LoggedUserTO;
import TO.MedicalSpecialtyTO;
import TO.PrimaryHealthCareCenterTO;
import common.HashUtils;
import common.UserType;
import jpa.AdministratorJPA;
import jpa.FamilyDoctorJPA;
import jpa.MedicalSpecialtyJPA;
import jpa.PatientJPA;
import jpa.PrimaryHealthCareCenterJPA;
import jpa.SpecialistDoctorJPA;
/**
*
* @author Marcos García Núñez (mgarcianun@uoc.edu)
*
*/
@Stateless
public class SystemAdminFacadeBean implements SystemAdminFacadeRemote {
// Persistence Unit Context
@PersistenceContext(unitName = "MyHealth")
private EntityManager entman;
/**
* Metodo que devuelve todas las especialidades medicas
*/
public Collection<MedicalSpecialtyTO> 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<MedicalSpecialtyTO> 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<MedicalSpecialtyJPA> allJPA = query.getResultList();
Collection<MedicalSpecialtyTO> allSpecialities = new ArrayList<MedicalSpecialtyTO>();
for (MedicalSpecialtyJPA ms : allJPA) {
allSpecialities.add(new MedicalSpecialtyTO(ms.getName(), ms.getDescription()));
}
return allSpecialities;
}
public Collection<PrimaryHealthCareCenterTO> listAllCAPs() {
return this.listPagedAllCAPs(0, 0);
}
public Collection<PrimaryHealthCareCenterTO> 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<PrimaryHealthCareCenterJPA> allJPA = query.getResultList();
Collection<PrimaryHealthCareCenterTO> allCAPs = new ArrayList<PrimaryHealthCareCenterTO>();
for (PrimaryHealthCareCenterJPA cap : allJPA) {
allCAPs.add(new PrimaryHealthCareCenterTO(cap.getName(), cap.getLocation()));
}
return allCAPs;
}
public LoggedUserTO login(String id, 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 (usr != null) {
// Comprobamos el password
if (usr.getPassword().equals(HashUtils.hashMD5(pwd)) == false) {
// Bad Password, devolvemos null!
usr = null;
}
}
return usr;
}
}