view src/main/java/de/mpiwg/indexmeta/web/beans/AbstractBean.java @ 7:bc57f2660b0f

implementation of web service
author Jorge Urzua <jurzua@mpiwg-berlin.mpg.de>
date Fri, 12 Apr 2013 17:48:42 +0200
parents
children
line wrap: on
line source

package de.mpiwg.indexmeta.web.beans;


import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.apache.commons.lang.StringUtils;

public abstract class AbstractBean {
	
	public static String BEAN_APP = "appBean";
	public static String BEAN_SESSION = "sessionBean";
	
	private String requestContextPath = null;
	
	public AbstractBean(){
	}
	
	public String getContextPath(){
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		return ec.getRequestContextPath();
	}
	
	public String getRequestContextPath(){
		if(StringUtils.isEmpty(requestContextPath)){
			String root = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
			String[] s = root.split(";");	
			requestContextPath = s[0];
		}
		return requestContextPath;
	}
	
	public ApplicationBean getAppBean(){
		ApplicationBean appBean = 
			((ApplicationBean)FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get(BEAN_APP));
		
		if(appBean == null){
			appBean = new ApplicationBean();
			FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().put(BEAN_APP, appBean);
		}
		
		return appBean; 
	}
	
	protected SessionBean getSession(){
		SessionBean session = 
			((SessionBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(BEAN_SESSION));
		
		if(session == null){
			session = new SessionBean();
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(BEAN_SESSION, session);
		}
		
		return session;
	}
	
	protected Object getRequestBean(String name){
		return FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(name);
	}
	
	protected void addMsg(String msg){
		getSession().addMsg(msg);
	}
	
	protected void addError(String error){
		getSession().addError(error);
	}
}