/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

import java.util.Iterator;
import java.util.Map;
import javax.faces.application.Application;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.render.ResponseStateManager;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import planosactuacao.ApplicationBean1;
import planosactuacao.SessionBean1;

/**
 *
 * @author lluis
 */
public class JSFUtils {

    
    public static SessionBean1 getSessionBean(FacesContext fc)
    {
        FacesContext context = fc.getCurrentInstance();
        SessionBean1 session = (SessionBean1) context.getApplication().getELResolver().getValue(context.getELContext(), null, "SessionBean1");
        return session;
    }

    public static ApplicationBean1 getApplicationBean(FacesContext fc)
    {
        FacesContext context = fc.getCurrentInstance();
        System.out.println("CONTEXT : " + context);
        ApplicationBean1 application = (ApplicationBean1) context.getApplication().getELResolver().getValue(context.getELContext(), null, "ApplicationBean1");
        return application;
    }

    
    //
    // isPostBack - JSF 1.1 implementation
    //
    public static boolean isPostBack(FacesContext fc)
    {
        Map parameterMap = fc.getExternalContext().getRequestParameterMap();

        if(parameterMap.size() > 0)
        {
            Iterator iter = parameterMap.keySet().iterator();
            while(iter.hasNext())
            {
                String par = (String) iter.next();
            }
            return true;
        }
        return false;
    }
    
   public static boolean isPostback() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    Map requestScope = (Map) facesContext.getApplication().getExpressionFactory().createValueExpression(facesContext.getELContext(), "#{requestScope}", Map.class).getValue(facesContext.getELContext());                
    //Map requestScope = (Map)facesContext.getApplication().createValueBinding(?#{requestScope}?).getValue(facesContext);
    boolean ispostback = ((Boolean)requestScope.get("ispostback")).booleanValue();
        System.out.println("POSTBACK A : " + ispostback);    
    return ispostback;
  }          
    
    //
    // isPostBack - JSF 1.2 implementation
    //
    public static boolean isPostBack12(FacesContext fc)
    {
        ResponseStateManager rsm = fc.getRenderKit().getResponseStateManager();
        if(rsm.isPostback(fc))
        {
            return true;
        }
        return false;
    }
    
    public static void logout(FacesContext fc) throws Exception
    {
        ExternalContext ectx = fc.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) ectx.getSession(false);
        session.invalidate();
    }
    
    public static void navigateTo(FacesContext fc, String outcome)
    {
        FacesContext context = fc.getCurrentInstance();
        Application app = context.getApplication();
        app.getNavigationHandler().handleNavigation(context, null, outcome);
    } 
    
    public static void redirect(FacesContext context, String url) throws Exception
    {
        HttpServletResponse response = (HttpServletResponse) context.getCurrentInstance().getExternalContext().getResponse();
        response.sendRedirect(url);
        context.responseComplete();
    }    
    
    public static String getRequestParameter(FacesContext fc, String parameter)
    {
        Map parameters = fc.getCurrentInstance().getExternalContext().getRequestParameterMap();   
        return (String) parameters.get(parameter);
    }
    
    
    

}
