forked from Coded/SIPRP
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.0 KiB
96 lines
2.0 KiB
/*
|
|
* 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.AnalisesDataProvider;
|
|
import global.Global;
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.OutputStream;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import javax.faces.context.FacesContext;
|
|
import javax.servlet.ServletContext;
|
|
|
|
/**
|
|
*
|
|
* @author lluis
|
|
*/
|
|
public class Logos
|
|
{
|
|
|
|
private static Logos INSTANCE = null;
|
|
|
|
private Logos()
|
|
{
|
|
|
|
}
|
|
|
|
public static synchronized Logos getInstance()
|
|
{
|
|
if ( INSTANCE == null )
|
|
{
|
|
INSTANCE = new Logos();
|
|
}
|
|
return INSTANCE;
|
|
}
|
|
|
|
|
|
public String getLogo( Integer empresaID )
|
|
{
|
|
return null;
|
|
}
|
|
|
|
|
|
|
|
private final Map< Integer, String > LOGOS = new HashMap< Integer, String >();
|
|
|
|
public String getLogo( FacesContext fc, Integer empresaID )
|
|
{
|
|
if ( ! LOGOS.containsKey( empresaID ) )
|
|
{
|
|
fetchLogoFromDB( fc, empresaID );
|
|
}
|
|
String result = LOGOS.get( empresaID );
|
|
System.out.println( "LOGO PATH : " + result );
|
|
return result;
|
|
}
|
|
|
|
|
|
private void fetchLogoFromDB( FacesContext fc, Integer empresaID )
|
|
{
|
|
String filepath = Global.LOGOS_FOLDER + "/logo_empresa" + empresaID + ".jpg";
|
|
File f = new File( filepath );
|
|
if ( ! f.isFile() )
|
|
{
|
|
try
|
|
{
|
|
byte[] logo = AnalisesDataProvider.getInstance().getLogoByEmpresa( empresaID );
|
|
if ( logo != null )
|
|
{
|
|
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 );
|
|
outImej.flush();
|
|
outImej.close();
|
|
|
|
LOGOS.put( empresaID, filepath );
|
|
}
|
|
}
|
|
catch ( Exception e )
|
|
{
|
|
ErrorLogger.logException( e );
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|