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:
103
1.sources/MyHealth/src/managedbean/common/ManagedBeanBase.java
Normal file
103
1.sources/MyHealth/src/managedbean/common/ManagedBeanBase.java
Normal 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user