Avances en la pantalla de registro de usuarios nuevos (pacientes).

* Actualización de script para crear tablas (Todo a minusculas, no case
sensitive)
* Script de datos de pruebas de especialides médicas.
* Nuevo bean para registro de usuarios (Quidato de filtro de seguridad
de login, acceso sin login).
* Actualización de entidades JPA con campos Identity.
* Enumerado para gestionar tipos de usuarios (Paciente, Medico Familia,
Especialista y Administrador)
* Clase común para realizar validaciones (función para validar nif).
This commit is contained in:
mgarcianun
2019-11-27 00:11:21 +01:00
parent 3686ead2ba
commit 9859f83326
26 changed files with 788 additions and 202 deletions

View File

@@ -35,7 +35,7 @@ public class AuthorizationFilter implements Filter {
HttpSession ses = reqt.getSession(false);
String reqURI = reqt.getRequestURI();
if (reqURI.indexOf("/login.xhtml") >= 0 || (ses != null && ses.getAttribute("username") != null)
if (reqURI.indexOf("/login.xhtml") >= 0 || reqURI.indexOf("/RegisterUser.xhtml") >= 0 || (ses != null && ses.getAttribute("username") != null)
|| reqURI.indexOf("/public/") >= 0 || reqURI.indexOf("/home.xhtml") >= 0 || reqURI.contains("javax.faces.resource"))
chain.doFilter(request, response);
else

View File

@@ -0,0 +1,18 @@
package managedbean.common;
public enum UserType {
PATIENT("Paciente"),
FAMILY_DOCTOR("Médico de familia"),
SPECIALIST_DOCTOR("Médico especialista"),
ADMINISTRADOR("Administrador");
private String userTypename;
private UserType (String userTypename){
this.userTypename = userTypename;
}
public String getUserTypename() {
return userTypename;
}
}

View File

@@ -0,0 +1,50 @@
package managedbean.common;
/***
*
* @author Marcos García Núñez (mgarcianun@uoc.edu)
*
*/
public class ValidationUtils {
static final String NIF_LETTERS = "TRWAGMYFPDXBNJZSQVHLCKE";
static final String NIE_LETTERS = "XYZ";
/**
*
* @param nif NIF a validar
* @return true si es válido, false en otro caso
*/
public static boolean isValid(String nif) {
if (nif == null)
return false;
nif = nif.toUpperCase().replace("-", "").replace(".", "");
if (nif.length() < 2 || nif.length() > 9)
return false;
if (nif.startsWith("X")) {
nif = nif.replace('X', '0');
} else if (nif.startsWith("Y")) {
nif = nif.replace('Y', '1');
} else if (nif.startsWith("Z")) {
nif = nif.replace('Z', '2');
}
char letra = nif.charAt(nif.length() - 1);
if (NIF_LETTERS.indexOf(letra) < 0)
return false;
String numbers = nif.substring(0, nif.length() - 1);
try {
if (NIF_LETTERS.charAt(Integer.parseInt(numbers) % 23) == letra)
return true;
} catch (Exception ex) {
return false;
}
return false;
}
}

View File

@@ -1,14 +1,11 @@
package managedbean.profile;
import java.io.Serializable;
import java.util.Properties;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.naming.Context;
import javax.naming.InitialContext;
import ejb.profile.ProfileFacadeRemote;
/**
* ManagedBEan que gestiona la edición y actualización de una especialidad
@@ -17,31 +14,98 @@ import ejb.profile.ProfileFacadeRemote;
* @author mark
*
*/
@Named( "AddPatientMBean")
@Named("addPatientMBean")
@RequestScoped
public class AddPatientMBean extends ProfileMBeanBase implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String nif;
private String name;
private String surname;
private String password;
private String passwordRepeat;
private String email;
private String primaryHealthCareCenter;
private String medicalSpecialty;
/**
* Constructor. Inicializa la conexión con el EJB Remoto
*
* @throws Exception
*/
public AddPatientMBean() throws Exception {
initializeAdminFacadeRemote();
super.initializeProfileFacadeRemote();
}
/**
* Inicializa la conexión con el EJB Remoto
*
* @throws Exception
*/
private void initializeAdminFacadeRemote() throws Exception {
Properties props = System.getProperties();
Context ctx = new InitialContext(props);
remoteManager = (ProfileFacadeRemote) ctx
.lookup("java:app/myHealth.jar/ProfileFacadeBean!ejb.component.ProfileFacadeRemote");
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNif() {
return nif;
}
public void setNif(String nif) {
this.nif = nif;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void save() {
FacesMessage msg;
try {
msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Alta realizada", "El usuario " + name + " " + surname + " se ha registrado correctamente.");
} catch (Exception e) {
msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Se ha producido un error inesperado: " + e.getLocalizedMessage());
}
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public String getPasswordRepeat() {
return passwordRepeat;
}
public void setPasswordRepeat(String passwordRepeat) {
this.passwordRepeat = passwordRepeat;
}
}

View File

@@ -9,11 +9,21 @@ import javax.naming.InitialContext;
import ejb.profile.ProfileFacadeRemote;
/***
*
* @author Marcos García Núñez (mgarcianun@uoc.edu)
*
*/
@Named("profileMBean")
public class ProfileMBeanBase {
@EJB
protected ProfileFacadeRemote remoteManager;
/**
* Inicializa la conexión con el EJB Remoto
*
* @throws Exception
*/
protected void initializeProfileFacadeRemote() throws Exception {
Properties props = System.getProperties();
Context ctx = new InitialContext(props);

View File

@@ -0,0 +1,228 @@
package managedbean.profile;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.resource.NotSupportedException;
import org.primefaces.PrimeFaces;
import TO.FamilyDoctorTO;
import TO.MedicalSpecialtyTO;
import TO.PatientTO;
import TO.PrimaryHealthCareCenterTO;
import TO.SpecialistDoctorTO;
import ejb.systemAdmin.SystemAdminFacadeRemote;
import managedbean.common.UserType;
import managedbean.common.ValidationUtils;
/**
* ManagedBEan que gestiona la edición y actualización de una especialidad
* médica.
*
* @author mark
*
*/
@Named("registerUser")
@RequestScoped
public class RegisterUserMBean extends ProfileMBeanBase implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String nif;
private String name;
private String surname;
private String password;
private String passwordRepeat;
private String email;
private HashMap<String, String> userTypes;
private String userType;
private String primaryHealthCareCenter;
private String medicalSpecialty;
private Collection<MedicalSpecialtyTO> medicalSpecialities;
private Collection<PrimaryHealthCareCenterTO> healthcareCenters;
/**
* Constructor. Inicializa la conexión con el EJB Remoto
*
* @throws Exception
*/
public RegisterUserMBean() throws Exception {
super.initializeProfileFacadeRemote();
}
@PostConstruct
public void init() {
this.userTypes = new HashMap<String, String>();
this.userTypes.put(UserType.PATIENT.getUserTypename(), UserType.PATIENT.name());
this.userTypes.put(UserType.FAMILY_DOCTOR.getUserTypename(), UserType.FAMILY_DOCTOR.name());
this.userTypes.put(UserType.SPECIALIST_DOCTOR.getUserTypename(), UserType.SPECIALIST_DOCTOR.name());
this.userType = "patient";
}
public HashMap<String, String> getUserTypes() {
return userTypes;
}
public void onUserTypeChange() {
switch (UserType.valueOf(this.userType)) {
case SPECIALIST_DOCTOR:
try {
Properties props = System.getProperties();
Context ctx = new InitialContext(props);
SystemAdminFacadeRemote rman = (SystemAdminFacadeRemote) ctx.lookup("java:app/MyHealth.jar/SystemAdminFacadeBean!ejb.systemAdmin.SystemAdminFacadeRemote");
this.medicalSpecialities = rman.listAllMedicalSpecialities();
PrimeFaces.current().ajax().addCallbackParam("specs", true);
} catch (Exception e) {
this.manageException(e);
}
break;
case FAMILY_DOCTOR:
try {
// TODO: Load Primary Healthcare Centers from remote EJB
this.healthcareCenters = new ArrayList<PrimaryHealthCareCenterTO>();
this.healthcareCenters.add(new PrimaryHealthCareCenterTO("Prueba", "Descripción prueba"));
this.healthcareCenters.add(new PrimaryHealthCareCenterTO("Centro 2", "Centro 2"));
PrimeFaces.current().ajax().addCallbackParam("caps", true);
} catch (Exception e) {
this.manageException(e);
}
break;
case ADMINISTRADOR:
case PATIENT:
break;
}
}
public Collection<MedicalSpecialtyTO> getMedicalSpecialties() {
return medicalSpecialities;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNif() {
return nif;
}
public void setNif(String nif) {
this.nif = nif;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void addNewUser() {
if (ValidationUtils.isValid(nif) == false) {
this.addFacesMessage(FacesMessage.SEVERITY_WARN, "El NIF indicado no es válido", "Por favor, especifique un NIF válido.");
} else {
try {
switch (UserType.valueOf(this.userType)) {
case PATIENT:
PatientTO pat = this.remoteManager.registerPatient(id, nif, name, surname, password, email);
break;
case FAMILY_DOCTOR:
FamilyDoctorTO fd = this.remoteManager.registerFamilyDoctor1(id, nif, name, surname, password, email, null);
break;
case SPECIALIST_DOCTOR:
SpecialistDoctorTO sd = this.remoteManager.registerSpecialistDoctor(id, nif, name, surname, password, email, null);
break;
case ADMINISTRADOR:
throw new NotSupportedException("No se soporta el registro directo de administradores.");
}
this.addFacesMessage(FacesMessage.SEVERITY_INFO, "Alta realizada",
"El usuario " + name + " " + surname + " se ha registrado correctamente. Por favor, comprueba su correo electrónico para verificar su cuenta.");
} catch (Exception e) {
this.manageException(e);
}
}
// FacesContext.getCurrentInstance().addMessage(null, msg);
}
private void addFacesMessage(FacesMessage.Severity severity, String summary, String detail) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, summary, detail));
}
private void manageException(Exception ex) {
this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Se ha producido un error inesperado", "Descripción del error: " + ex.getLocalizedMessage());
}
public String getPasswordRepeat() {
return passwordRepeat;
}
public void setPasswordRepeat(String passwordRepeat) {
this.passwordRepeat = passwordRepeat;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getmedicalSpecialty() {
return medicalSpecialty;
}
public void setmedicalSpecialty(String medicalSpecialty) {
this.medicalSpecialty = medicalSpecialty;
}
}

View File

@@ -3,6 +3,7 @@ package managedbean.systemAdmin;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpSession;