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

package utils;

import analiseacidentestrabalho.SessionBean1;
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.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @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;
//    }
    
    //
    // isPostBack - JSF 1.1 implementation
    //
//    public static boolean isPostBack(FacesContext fc)
//    {
//        Map parameterMap = fc.getExternalContext().getRequestParameterMap();
//        if(parameterMap.size() > 0)
//        {
//            return true;
//        }
//        return false;
//    }
    
    public static SessionBean1 getSessionBean(FacesContext fc)
    {
        FacesContext context = fc.getCurrentInstance();
        //HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);        
        //SessionBean1 session = (SessionBean1) context.getApplication().getELResolver().getValue(context.getELContext(), null, "SessionBean1");
        //SessionBean1 session = (SessionBean1) context.getApplication().;      
        
        final StringBuffer valueBinding = new StringBuffer();  
        valueBinding.append('#');  
        valueBinding.append('{');  
        valueBinding.append("SessionBean1");  
        valueBinding.append('}');  
        SessionBean1 session = (SessionBean1) fc.getApplication().  
        createValueBinding(valueBinding.toString()).getValue(fc);          
        return session;
    }    
    
    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;
    }    
    
    //
    // 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 void dispatch(FacesContext context, String url) throws Exception
    {
        HttpServletResponse response = (HttpServletResponse) context.getCurrentInstance().getExternalContext().getResponse();        
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
    }
    
    public static String getRequestParameter(FacesContext fc, String parameter)
    {
        Map parameters = fc.getCurrentInstance().getExternalContext().getRequestParameterMap();   
        return (String) parameters.get(parameter);
    }
    
    public static boolean requestHasParameters(FacesContext fc)
    {
        int size = fc.getCurrentInstance().getExternalContext().getRequestParameterMap().size();           
        if(size > 0)
        {
            return true;
        }
        return false;
    }
    

}
