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

package utils;

import com.evolute.utils.error.ErrorLogger;
import db.providers.RelatoriosDataProvider;
import global.Global;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

/**
 *
 * @author lluis
 */
public class Logos {
    
    public String getLogo(Integer empresaId)
    {
        return null;
    }
    
    public String getLogo(FacesContext fc, Integer empresaId)
    {
        try
        {
            //get image from db :
            RelatoriosDataProvider rdp = new RelatoriosDataProvider(); 
            byte[] logo = rdp.getLogoByEmpresa(empresaId);
            int len = logo.length;
            
            ServletContext context = (ServletContext) fc.getExternalContext().getContext();
            File logoFolder =  new File(context.getRealPath(Global.LOGOS_FOLDER));

            String logoFilename = logoFolder + "/logo_empresa" + empresaId.toString() + ".jpg";
            
            OutputStream outImej = new FileOutputStream(logoFilename);
            outImej.write(logo, 0, len);
            System.out.println("LOGO PATH : " + logoFilename);
            outImej.close();   
            return Global.LOGOS_FOLDER + "/logo_empresa" + empresaId.toString() + ".jpg";
                            }
            catch(Exception ex)
            {
                ErrorLogger.logException( ex );
                return null;
            }
        
    }

}
