/*
 * Header.java
 *
 * Created on Oct 6, 2008, 10:35:36 AM
 */
 
package planosactuacao;

import com.sun.rave.web.ui.appbase.AbstractFragmentBean;
import com.sun.webui.jsf.component.ImageComponent;
import com.sun.webui.jsf.component.StaticText;
import db.entidades.Utilizador;
import global.Global;
import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import utils.JSFHTTPUtils;
import utils.JSFUtils;

/**
 * <p>Fragment bean that corresponds to a similarly named JSP page
 * fragment.  This class contains component definitions (and initialization
 * code) for all components that you have defined on this fragment, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @author lluis
 */
public class Header extends AbstractFragmentBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization. <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private StaticText stDataPlano = new StaticText();

    public StaticText getStDataPlano() {
        return stDataPlano;
    }

    public void setStDataPlano(StaticText st) {
        this.stDataPlano = st;
    }
    private ImageComponent imgLogoEmpresa = new ImageComponent();

    public ImageComponent getImgLogoEmpresa() {
        return imgLogoEmpresa;
    }

    public void setImgLogoEmpresa(ImageComponent ic) {
        this.imgLogoEmpresa = ic;
    }
    // </editor-fold>

    public Header() {
    }

    /**
     * <p>Callback method that is called whenever a page containing
     * this page fragment is navigated to, either directly via a URL,
     * or indirectly via page navigation.  Override this method to acquire
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        FacesContext fc = getFacesContext();
        String referer = JSFHTTPUtils.getReferer(fc);
        System.out.println("HEADER - REFERER : " + referer);
        
        // <editor-fold defaultstate="collapsed" desc="Visual-Web-managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
        
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called.  Override this
     * method to release resources acquired in the <code>init()</code>
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void destroy() {
    }

    public String lnkLogout_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        FacesContext fc = getFacesContext();
       try
       {
            JSFUtils.logout(fc);
            //JSFUtils.navigateTo(fc, "login");
            try
            {
                //JSFUtils.redirect(fc, "http://localhost:8084/AnaliseAcidentesTrabalho/faces/Login.jsp");
                JSFUtils.redirect(fc, Global.ANALISE_ACIDENTES_URL + "Login.jsp");                
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
            
       }
       catch(Exception ex)
       {
           
       }        
        return null;
    }

    public String lnkAnaliseAcidentes_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        
        FacesContext fc = getFacesContext();
        SessionBean1 session = JSFUtils.getSessionBean(fc);
        Utilizador u = session.getCurrentUser();
        
        String url = Global.ANALISE_ACIDENTES_URL;
        int tipo = u.getTipo().intValue();
        switch(tipo)
        {
            case Global.RESPONSAVEL_SEGURANCA:
                url += "FormSeguranca.jsp?user=" + u.getId().intValue();                
                break;
                
            case Global.RH:
                url += "FormRH.jsp?user=" + u.getId().intValue();                                
                break;
                
            case Global.TECNICO_HS:
                url += "FormHS.jsp?user=" + u.getId().intValue();                
                break;
                
            case Global.GESTOR:
                url += "FormGestor.jsp?user=" + u.getId().intValue();                                
                break;                
                
            case Global.DIRECTOR_LOJA:
                url += "FormGestor.jsp?user=" + u.getId().intValue();
                break;
            case Global.DIRECTOR_GERAL_RH:
                url += "FormDirGerRh.jsp?user=" + u.getId().intValue();
                break;
                
            case Global.DIRECTOR_SIPRP:
                url += "FormDirSiprp.jsp?user=" + u.getId().intValue();
                break;                
                
            case Global.DIRECTOR_NACIONAL_SEGURANCA:
                url += "FormGestor.jsp?user=" + u.getId().intValue();                
                break;
        }

        try
        {
            JSFUtils.redirect(fc.getCurrentInstance(), url);            
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }        
        return null;
    }

    public String lnkPlanosActuacao1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "lista_planos";
    }

    public String lnkGerirUtilizadores1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        FacesContext fc = getFacesContext();
        SessionBean1 session = JSFUtils.getSessionBean(fc);
        Utilizador u = session.getCurrentUser();  
        
        String url = Global.ANALISE_ACIDENTES_URL;
        int tipo = u.getTipo().intValue();

        url += "GestaoUtilizadores.jsp?user=" + u.getId().intValue();                
        try
        {
            JSFUtils.redirect(fc.getCurrentInstance(), url);            
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }        
        
        return null;
    }

    public String lnkDadosUtilizador1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        FacesContext fc = getFacesContext();
        SessionBean1 session = JSFUtils.getSessionBean(fc);
        Utilizador u = session.getCurrentUser();  
        
        String url = Global.ANALISE_ACIDENTES_URL;
        int tipo = u.getTipo().intValue();

        url += "FichaUtilizador.jsp?user=" + u.getId().intValue() + "&mode=edit";                
        try
        {
            JSFUtils.redirect(fc.getCurrentInstance(), url);            
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }        
        
        return null;
    }

    public String lnkAjuda_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }
   

}
