/*
 * Global.java
 *
 * Created on November 23, 2007, 10:08 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package global;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author lluis2
 */
public class Global {
    public final static int ESTADO_SEG = 1;
    public final static int ESTADO_RH1 = 2;    
    public final static int ESTADO_HS = 3;     
    public final static int ESTADO_RH2 = 4;  
    //public final static int ESTADO_MEDICINA = 5;      
    public final static int ESTADO_CONSOLIDACAO = 5;          
    //public final static int ESTADO_ASSINATURAS = 7;            
    public final static int ESTADO_ASSINATURA_SEG = 6;        
    public final static int ESTADO_ASSINATURA_RH = 7;            
    //public final static int ESTADO_IMPRESSAO = 8;        
    public final static int ESTADO_FECHAR = 8;        
    public final static int ESTADO_CONCLUIDO = 9;  
    
    
    public final static int ESTADO_ASSINATURA_1 = 1;            
    public final static int ESTADO_ASSINATURA_2 = 2;                
    public final static int ESTADO_ASSINATURA_3 = 3; 
    
    public final static int TIPO_UTILIZADOR_SEGURANCA = 1;
    public final static int TIPO_UTILIZADOR_RH = 2;    
    public final static int TIPO_UTILIZADOR_HS = 3;    
    public final static int TIPO_UTILIZADOR_MEDICO = 5;    
    public final static int TIPO_UTILIZADOR_GESTOR = 6;    
    public final static int TIPO_UTILIZADOR_DIRECTOR_GERAL_RH = 7;    
    public final static int TIPO_UTILIZADOR_DIRECTOR_SIPRP = 8; 
    
    public final static int AUCHAN = 32; 
    
    
    public final static String SERVIDOR_SMTP = "smtp.netcabo.pt";
    public final static String SERVIDOR_SMTP_PASSWORD = "EVOLUTE";    
    public final static String ENDERECO_ENVIO = "acidentes.auchan@siprp.pt";
    //public final static String ENDERECO_ENVIO = "lluis@evolute.pt";    //testes
    
    public final static String IMAGE_FOLDER = "/home/siprp/imagens_acidentes";    
    //public final static String IMAGE_FOLDER = "/home/lluis/imagens_acidentes";        
    public final static String RESOURCES_IMAGE_FOLDER = "/resources/images";  
    
    public static final String PDF_FOLDER_URL = "/resources/pdf";     
    /** Creates a new instance of Global */
    public Global() {
    }
    
    public void copyImage(File fSource, File fDest)
    {
        try
        {
            InputStream in = new FileInputStream(fSource);
            OutputStream out = new FileOutputStream(fDest);

            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();                    
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public void deleteFolderFiles(File folder)
    {
        File files[] = folder.listFiles();
        for(int i=0;i<files.length;i++)  
        {
            if(files[i].isFile())
            {
                files[i].delete();
            }
        }
    }
    
    public void showPdf(String pdfFilePath) throws Exception
    {
        
         File pdfFile = new File(pdfFilePath);
         FileInputStream fis = new FileInputStream(pdfFile);   
         byte[] buf = new byte[(int)pdfFile.length()];  
         while ((fis.read(buf)) > 0)
         {  
             //fos.write(buf, 0, buf.length);  
         }  
         fis.close();
        FacesContext ctx = FacesContext.getCurrentInstance();
        if (!ctx.getResponseComplete())
        {         
            HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
            String contentType = "application/pdf";            
            response.setContentType(contentType);
            response.setHeader("Content-Disposition","attachment;filename=\"" + "Ajuda.pdf" + "\"");

            ServletOutputStream out = response.getOutputStream();

            //out.write(model.getInfo().getBytes());
            out.write(buf, 0, buf.length); 
            out.flush();
            out.close();
            ctx.responseComplete();  
            ctx.release();
        }
    }    
    
}
