/*
 * JSFUtils.java
 * 
 * Created on Sep 21, 2007, 3:40:46 PM
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.ellectra.gui.utils;

import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.context.FacesContext;
import javax.faces.el.VariableResolver;

/**
 * Utility class for JSF Pages
 */


public class JSFUtils 
{

	
		
	
 public void loadManagedBean(String mbeanName){
if(mbeanName==null||mbeanName.length()<=0){
return;
}
getVariableResolver().resolveVariable(FacesContext.getCurrentInstance(), mbeanName);
}

public void loadManagedBeans(String[] mbeanNameArray){
if(mbeanNameArray==null||mbeanNameArray.length<=0){
return;
}

VariableResolver vr=getVariableResolver();
for(String mbeanName : mbeanNameArray){
if(mbeanName==null||mbeanName.length()<=0){
continue;
}
vr.resolveVariable(FacesContext.getCurrentInstance(), mbeanName);
}
}

private VariableResolver getVariableResolver(){
return FacesContext.getCurrentInstance().getApplication().getVariableResolver();

}
   
  public static ValueBinding getValueBinding(String expression)
  {
    FacesContext context = FacesContext.getCurrentInstance();
    return context.getApplication().createValueBinding(expression);
  }
  
  public static String getValueBindingString(String expression)
  {
    FacesContext context = FacesContext.getCurrentInstance();
    ValueBinding currentBinding =  context.getApplication().createValueBinding(expression);
    return (String) currentBinding.getValue(context);
    
  }
    
   public static Object getManagedObject(String objectName)
  {
    FacesContext context = FacesContext.getCurrentInstance();
    Object requestedObject =  context.getApplication().getVariableResolver().resolveVariable(context, objectName);
    return  requestedObject;
  }
  
 
  public static void storeOnSession(FacesContext ctx, String key, Object object) {
      Map sessionState = ctx.getExternalContext().getSessionMap();
      sessionState.put(key, object);
  }
  
  public static Object retrieveOnSession(FacesContext ctx, String key) {
      Map sessionState = ctx.getExternalContext().getSessionMap();
      return sessionState.get(key);
  }

}
