Clase base para todos los managedBeans con soporte para mensajes JSF,

conexión a los FacadeRemote de los EJB.
Mejoras en el login de usuario.
Corregido problema en registro de usuario.
This commit is contained in:
Marcos Garcia Nuñez
2019-12-08 16:56:06 +01:00
parent 1dff77f32a
commit 54cc9da998
19 changed files with 264 additions and 146 deletions

View File

@@ -0,0 +1,103 @@
package managedbean.common;
import java.util.Properties;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import ejb.medicalTest.MedicalTestFacadeRemote;
import ejb.profile.ProfileFacadeRemote;
import ejb.systemAdmin.SystemAdminFacadeRemote;
import ejb.visit.VisitFacadeRemote;
/***
*
* @author Marcos García Núñez (mgarcianun@uoc.edu)
*
*/
public class ManagedBeanBase {
@EJB
protected ProfileFacadeRemote remoteManagerProfile;
@EJB
protected SystemAdminFacadeRemote remoteManagerSystemAdmin;
@EJB
protected VisitFacadeRemote remoteManagerVisit;
@EJB
protected MedicalTestFacadeRemote remoteManagerMedicalTest;
/**
* Inicializa la conexión con el EJB Remoto
*
* @return
*
* @throws Exception
*/
private <T> T getContext(String name, Class<T> type) {
T retObj = null;
try {
Properties props = System.getProperties();
Context ctx = new InitialContext(props);
retObj = type.cast(ctx.lookup(name));
} catch (NamingException e) {
this.manageException(e);
}
return retObj;
}
protected ProfileFacadeRemote getRemoteManagerProfile() {
if (remoteManagerProfile == null) {
remoteManagerProfile = this.getContext("java:app/MyHealth.jar/ProfileFacadeBean!ejb.profile.ProfileFacadeRemote", ProfileFacadeRemote.class);
}
return remoteManagerProfile;
}
protected SystemAdminFacadeRemote getRemoteManagerSystemAdmin() {
if (remoteManagerSystemAdmin == null) {
remoteManagerSystemAdmin = this.getContext("java:app/MyHealth.jar/SystemAdminFacadeBean!ejb.systemAdmin.SystemAdminFacadeRemote", SystemAdminFacadeRemote.class);
}
return remoteManagerSystemAdmin;
}
protected VisitFacadeRemote getRemoteManagerVisit() {
if (remoteManagerVisit == null) {
remoteManagerVisit = this.getContext("java:app/MyHealth.jar/VisitFacadeBean!ejb.systemAdmin.VisitFacadeRemote", VisitFacadeRemote.class);
}
return remoteManagerVisit;
}
protected MedicalTestFacadeRemote getRemoteManagerMedicalTest() {
if (remoteManagerMedicalTest == null) {
remoteManagerMedicalTest = this.getContext("java:app/MyHealth.jar/MedicalTestFacadeBean!ejb.systemAdmin.MedicalTestFacadeRemote", MedicalTestFacadeRemote.class);
}
return remoteManagerMedicalTest;
}
protected void addFacesMessageKeep(FacesMessage.Severity severity, String summary, String detail) {
FacesContext context = FacesContext.getCurrentInstance();
this.addFacesMessage(FacesContext.getCurrentInstance(), severity, summary, detail);
context.getExternalContext().getFlash().setKeepMessages(true);
}
protected void addFacesMessage(FacesMessage.Severity severity, String summary, String detail) {
this.addFacesMessage(FacesContext.getCurrentInstance(), severity, summary, detail);
}
protected void addFacesMessage(FacesContext context, FacesMessage.Severity severity, String summary, String detail) {
context.addMessage(null, new FacesMessage(severity, summary, detail));
}
protected void manageException(Exception ex) {
this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Se ha producido un error inesperado", "Descripción del error: " + ex.getLocalizedMessage());
}
}

View File

@@ -5,8 +5,14 @@ import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import TO.LoggedUserTO;
public class SessionUtils {
public static final String SESSION_VAR_USERNAME = "userName";
public static final String SESSION_VAR_USERID = "userId";
public static final String SESSION_VAR_USERTYPE = "userType";
public static final String SESSION_VAR_USER = "loggedInUser";
public static HttpSession getSession() {
return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
@@ -15,6 +21,19 @@ public class SessionUtils {
public static HttpServletRequest getRequest() {
return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
}
public static void CreateSession(LoggedUserTO usr) {
HttpSession ses = getSession();
ses.setAttribute(SessionUtils.SESSION_VAR_USERNAME, usr.getName());
ses.setAttribute(SessionUtils.SESSION_VAR_USERID, usr.getId());
ses.setAttribute(SessionUtils.SESSION_VAR_USERTYPE, usr.getUserType());
ses.setAttribute(SessionUtils.SESSION_VAR_USER, usr);
}
public static void DestroySession() {
HttpSession ses = getSession();
ses.invalidate();
}
public static boolean isLogedIn() {
if (getUserId() == "")
@@ -25,16 +44,16 @@ public class SessionUtils {
public static String getUserName() {
HttpSession session = getSession();
if (session != null && session.getAttribute("username") != null)
return session.getAttribute("username").toString();
if (session != null && session.getAttribute(SessionUtils.SESSION_VAR_USERNAME) != null)
return session.getAttribute(SessionUtils.SESSION_VAR_USERNAME).toString();
else
return "";
}
public static String getUserId() {
HttpSession session = getSession();
if (session != null && session.getAttribute("userid") != null)
return session.getAttribute("userid").toString();
if (session != null && session.getAttribute(SessionUtils.SESSION_VAR_USERID) != null)
return session.getAttribute(SessionUtils.SESSION_VAR_USERID).toString();
else
return "";
}

View File

@@ -5,6 +5,8 @@ import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import managedbean.common.ManagedBeanBase;
/***
*
* @author Marcos García Núñez (mgarcianun@uoc.edu)
@@ -12,7 +14,7 @@ import javax.inject.Named;
*/
@Named("AddFamilyDoctorMBean")
@RequestScoped
public class AddFamilyDoctorMBean extends ProfileMBeanBase implements Serializable {
public class AddFamilyDoctorMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;
@@ -21,8 +23,7 @@ public class AddFamilyDoctorMBean extends ProfileMBeanBase implements Serializab
*
* @throws Exception
*/
public AddFamilyDoctorMBean() throws Exception {
super.initializeProfileFacadeRemote();
public AddFamilyDoctorMBean() {
}
}

View File

@@ -7,6 +7,8 @@ import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import managedbean.common.ManagedBeanBase;
/**
* ManagedBEan que gestiona la edición y actualización de una especialidad
* médica.
@@ -16,7 +18,7 @@ import javax.inject.Named;
*/
@Named("addPatientMBean")
@RequestScoped
public class AddPatientMBean extends ProfileMBeanBase implements Serializable {
public class AddPatientMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;
@@ -35,8 +37,7 @@ public class AddPatientMBean extends ProfileMBeanBase implements Serializable {
*
* @throws Exception
*/
public AddPatientMBean() throws Exception {
super.initializeProfileFacadeRemote();
public AddPatientMBean() {
}
public String getEmail() {

View File

@@ -7,6 +7,7 @@ import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import ejb.profile.ProfileFacadeRemote;
import managedbean.common.ManagedBeanBase;
/***
*
@@ -15,7 +16,7 @@ import ejb.profile.ProfileFacadeRemote;
*/
@Named( "AddSpecialistDoctorMBean")
@RequestScoped
public class AddSpecialistDoctorMBean extends ProfileMBeanBase implements Serializable {
public class AddSpecialistDoctorMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;
@@ -27,8 +28,7 @@ public class AddSpecialistDoctorMBean extends ProfileMBeanBase implements Serial
*
* @throws Exception
*/
public AddSpecialistDoctorMBean() throws Exception {
super.initializeProfileFacadeRemote();
public AddSpecialistDoctorMBean() {
}
}

View File

@@ -10,6 +10,7 @@ import javax.naming.Context;
import javax.naming.InitialContext;
import ejb.profile.ProfileFacadeRemote;
import managedbean.common.ManagedBeanBase;
/***
*
@@ -18,7 +19,7 @@ import ejb.profile.ProfileFacadeRemote;
*/
@Named( "ChangeFamilyDoctorMBean")
@RequestScoped
public class ChangeFamilyDoctorMBean extends ProfileMBeanBase implements Serializable {
public class ChangeFamilyDoctorMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -10,6 +10,7 @@ import javax.naming.Context;
import javax.naming.InitialContext;
import ejb.profile.ProfileFacadeRemote;
import managedbean.common.ManagedBeanBase;
/***
*
@@ -18,7 +19,7 @@ import ejb.profile.ProfileFacadeRemote;
*/
@Named( "ChangePrimaryHealthCareCenterMBean")
@RequestScoped
public class ChangePrimaryHealthCareCenterMBean extends ProfileMBeanBase implements Serializable {
public class ChangePrimaryHealthCareCenterMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -1,42 +0,0 @@
package managedbean.profile;
import java.util.Properties;
import javax.ejb.EJB;
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;
/***
*
* @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);
remoteManager = (ProfileFacadeRemote) ctx.lookup("java:app/MyHealth.jar/ProfileFacadeBean!ejb.profile.ProfileFacadeRemote");
}
protected void addFacesMessage(FacesMessage.Severity severity, String summary, String detail) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, summary, detail));
}
protected void manageException(Exception ex) {
this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Se ha producido un error inesperado", "Descripción del error: " + ex.getLocalizedMessage());
}
}

View File

@@ -8,6 +8,8 @@ import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import javax.naming.Context;
@@ -24,6 +26,7 @@ import TO.PrimaryHealthCareCenterTO;
import TO.SpecialistDoctorTO;
import common.UserType;
import ejb.systemAdmin.SystemAdminFacadeRemote;
import managedbean.common.ManagedBeanBase;
import managedbean.common.ValidationUtils;
/**
@@ -35,17 +38,19 @@ import managedbean.common.ValidationUtils;
*/
@Named("registerUser")
@ViewScoped
public class RegisterUserMBean extends ProfileMBeanBase implements Serializable {
public class RegisterUserMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private int id;
private String nif;
private String name;
private String surname;
private String password;
private String passwordRepeat;
private String email;
private boolean registered;
// private HashMap<String, String> userTypes;
private List<UserType> userTypes;
private String userType;
@@ -59,8 +64,8 @@ public class RegisterUserMBean extends ProfileMBeanBase implements Serializable
*
* @throws Exception
*/
public RegisterUserMBean() throws Exception {
super.initializeProfileFacadeRemote();
public RegisterUserMBean() {
}
@PostConstruct
@@ -69,20 +74,12 @@ public class RegisterUserMBean extends ProfileMBeanBase implements Serializable
this.userTypes.add(UserType.PATIENT);
this.userTypes.add(UserType.FAMILY_DOCTOR);
this.userTypes.add(UserType.SPECIALIST_DOCTOR);
this.registered = false;
this.userType = UserType.PATIENT.name();
Properties props = System.getProperties();
Context ctx;
try {
ctx = new InitialContext(props);
SystemAdminFacadeRemote rman = (SystemAdminFacadeRemote) ctx.lookup("java:app/MyHealth.jar/SystemAdminFacadeBean!ejb.systemAdmin.SystemAdminFacadeRemote");
this.medicalSpecialitiesList = rman.listAllMedicalSpecialities();
this.primaryHealthCareCentersList = rman.listAllCAPs();
} catch (NamingException e) {
this.manageException(e);
}
this.medicalSpecialitiesList = this.getRemoteManagerSystemAdmin().listAllMedicalSpecialities();
this.primaryHealthCareCentersList = this.getRemoteManagerSystemAdmin().listAllCAPs();
}
public List<UserType> getUserTypes() {
@@ -115,19 +112,10 @@ public class RegisterUserMBean extends ProfileMBeanBase implements Serializable
public Collection<MedicalSpecialtyTO> getMedicalSpecialtiesList() {
return medicalSpecialitiesList;
}
// public void setMedicalSpecialtiesList(Collection<MedicalSpecialtyTO> list) {
// medicalSpecialitiesList = list;
// }
public Collection<PrimaryHealthCareCenterTO> getPhcList() {
return primaryHealthCareCentersList;
}
// public void setPhcList(Collection<PrimaryHealthCareCenterTO> list) {
// primaryHealthCareCentersList = list;
// }
public boolean isPatient() {
return (UserType.valueOf(this.userType) == UserType.PATIENT);
}
@@ -143,7 +131,7 @@ public class RegisterUserMBean extends ProfileMBeanBase implements Serializable
public boolean isDoctor() {
return (isFamilyDoctor() || isSpecialistDoctor());
}
public String getEmail() {
return email;
}
@@ -191,8 +179,8 @@ public class RegisterUserMBean extends ProfileMBeanBase implements Serializable
public void setId(int id) {
this.id = id;
}
public String addNewUser() {
public void addNewUser() {
int error = 0;
if (this.isFamilyDoctor() && this.primaryHealthCareCenter == null) {
@@ -212,34 +200,31 @@ public class RegisterUserMBean extends ProfileMBeanBase implements Serializable
try {
switch (UserType.valueOf(this.userType)) {
case PATIENT:
PatientTO pat = this.remoteManager.registerPatient(id, nif, name, surname, password, email);
PatientTO pat = this.getRemoteManagerProfile().registerPatient(id, nif, name, surname, password, email);
this.id = pat.getId();
break;
case FAMILY_DOCTOR:
FamilyDoctorTO fd = this.remoteManager.registerFamilyDoctor1(id, nif, name, surname, password, email, this.primaryHealthCareCenter);
FamilyDoctorTO fd = this.getRemoteManagerProfile().registerFamilyDoctor1(id, nif, name, surname, password, email, this.primaryHealthCareCenter);
this.id = fd.getId();
break;
case SPECIALIST_DOCTOR:
SpecialistDoctorTO sd = this.remoteManager.registerSpecialistDoctor(id, nif, name, surname, password, email, this.medicalSpecialty);
SpecialistDoctorTO sd = this.getRemoteManagerProfile().registerSpecialistDoctor(id, nif, name, surname, password, email, this.medicalSpecialty);
this.id = sd.getId();
break;
case ADMINISTRADOR:
throw new NotSupportedException("No se soporta el registro directo de administradores.");
}
this.registered = true;
this.addFacesMessage(FacesMessage.SEVERITY_INFO, "Registro realizado", "El usuario " + name + " " + surname + " se ha registrado correctamente.");
this.addFacesMessage(FacesMessage.SEVERITY_INFO, "Registro realizado",
"El usuario " + name + " " + surname + " se ha registrado correctamente. Por favor, comprueba su correo electrónico para verificar su cuenta.");
return "RegisterUserResult";
} catch (Exception e) {
this.manageException(e);
}
}
return "";
}
public String getPasswordRepeat() {
@@ -274,4 +259,8 @@ public class RegisterUserMBean extends ProfileMBeanBase implements Serializable
this.primaryHealthCareCenter = primaryHealthCareCenter;
}
public boolean isRegistered() {
return registered;
}
}

View File

@@ -10,6 +10,7 @@ import javax.naming.Context;
import javax.naming.InitialContext;
import ejb.profile.ProfileFacadeRemote;
import managedbean.common.ManagedBeanBase;
/***
*
@@ -18,7 +19,7 @@ import ejb.profile.ProfileFacadeRemote;
*/
@Named( "ShowFamilyDoctorMBean")
@RequestScoped
public class ShowFamilyDoctorMBean extends ProfileMBeanBase implements Serializable {
public class ShowFamilyDoctorMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -10,6 +10,7 @@ import javax.naming.Context;
import javax.naming.InitialContext;
import ejb.profile.ProfileFacadeRemote;
import managedbean.common.ManagedBeanBase;
/***
*
@@ -18,7 +19,7 @@ import ejb.profile.ProfileFacadeRemote;
*/
@Named("ShowPatientMBean")
@RequestScoped
public class ShowPatientMBean extends ProfileMBeanBase implements Serializable {
public class ShowPatientMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -10,6 +10,7 @@ import javax.naming.Context;
import javax.naming.InitialContext;
import ejb.profile.ProfileFacadeRemote;
import managedbean.common.ManagedBeanBase;
/***
*
@@ -18,7 +19,7 @@ import ejb.profile.ProfileFacadeRemote;
*/
@Named( "ShowSpecialistDoctorMBean")
@RequestScoped
public class ShowSpecialistDoctorMBean extends ProfileMBeanBase implements Serializable {
public class ShowSpecialistDoctorMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -10,6 +10,7 @@ import javax.naming.Context;
import javax.naming.InitialContext;
import ejb.profile.ProfileFacadeRemote;
import managedbean.common.ManagedBeanBase;
/***
*
@@ -18,7 +19,7 @@ import ejb.profile.ProfileFacadeRemote;
*/
@Named("UpdateFamilyDoctorMBean")
@RequestScoped
public class UpdateFamilyDoctorMBean extends ProfileMBeanBase implements Serializable {
public class UpdateFamilyDoctorMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -10,6 +10,7 @@ import javax.naming.Context;
import javax.naming.InitialContext;
import ejb.profile.ProfileFacadeRemote;
import managedbean.common.ManagedBeanBase;
/***
*
@@ -18,7 +19,7 @@ import ejb.profile.ProfileFacadeRemote;
*/
@Named("UpdatePatientMBean")
@RequestScoped
public class UpdatePatientMBean extends ProfileMBeanBase implements Serializable {
public class UpdatePatientMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -10,6 +10,7 @@ import javax.naming.Context;
import javax.naming.InitialContext;
import ejb.profile.ProfileFacadeRemote;
import managedbean.common.ManagedBeanBase;
/***
*
@@ -18,7 +19,7 @@ import ejb.profile.ProfileFacadeRemote;
*/
@Named("UpdateSpecialistDoctorMBean")
@RequestScoped
public class UpdateSpecialistDoctorMBean extends ProfileMBeanBase implements Serializable {
public class UpdateSpecialistDoctorMBean extends ManagedBeanBase implements Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -14,11 +14,12 @@ import org.primefaces.PrimeFaces;
import TO.LoggedUserTO;
import ejb.systemAdmin.SystemAdminFacadeRemote;
import managedbean.common.ManagedBeanBase;
import managedbean.common.SessionUtils;
@Named("loginView")
@RequestScoped
public class LoginMBean {
public class LoginMBean extends ManagedBeanBase {
private String username;
private String password;
@@ -55,23 +56,19 @@ public class LoginMBean {
if (usr != null) {
loggedIn = true;
HttpSession session = SessionUtils.getSession();
session.setAttribute("username", usr.getName());
session.setAttribute("userid", "1");
session.setAttribute("userid", usr.getUserType());
session.setAttribute("loggedInUser", usr);
SessionUtils.CreateSession(usr);
message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Bienvenido", usr.getName());
this.addFacesMessageKeep(FacesMessage.SEVERITY_INFO, "Login correcto", "Bienvenido " + usr.getName());
return ("home?faces-redirect=true");
} else
message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error", "El usuario o la contraseña son incorrectos");
this.addFacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error", "El usuario o la contraseña son incorrectos");
} catch (Exception ex) {
message = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Error fatal", ex.getMessage());
this.addFacesMessage(FacesMessage.SEVERITY_FATAL, "Error fatal", ex.getMessage());
}
} else
message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error", "El usuario o la contraseña son incorrectos");
this.addFacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error", "El usuario o la contraseña son incorrectos");
FacesContext.getCurrentInstance().addMessage(null, message);
PrimeFaces.current().ajax().addCallbackParam("loggedIn", loggedIn);
@@ -81,11 +78,9 @@ public class LoginMBean {
// logout event, invalidate session
public String logout() {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Sessión cerrada", "Ha cerrado correctament su ssesión. Hasta la vista");
FacesContext.getCurrentInstance().addMessage(null, message);
this.addFacesMessageKeep(FacesMessage.SEVERITY_INFO, "Sessión cerrada", "Ha cerrado correctament su ssesión. Hasta la vista");
HttpSession session = SessionUtils.getSession();
session.invalidate();
SessionUtils.DestroySession();
return "home?faces-redirect=true";
}