package siprp.planoactuacao.print;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.util.Date;
import java.util.Vector;

public class LogotiposImporter
{
	public static void main( String args[] )
		throws Exception
	{
		System.out.println( "Importer: " + new Date() );
		load( args[ 0 ] );
	}

	public static void load( String path )
		throws Exception
	{
		File dir = new File( path );
		File files[] = dir.listFiles( new FilenameFilter(){
	
			@Override
			public boolean accept( File dir, String name )
			{
				return name.length() >= 4 && ".jpg".equals( name.substring( name.length() - 4, name.length() ) );
			}
		} );
		for( File file : files )
		{
			FileInputStream fis = new FileInputStream( file );
			int ret = 0;
			Vector<byte[]> buffers = new Vector<byte[]>();
			Vector<Integer> sizes = new Vector<Integer>();
			byte data[];
			int size = 0;
			do
			{
				byte buff[] = new byte[ 1024 ];
				ret = fis.read( buff, 0, buff.length );
				if( ret > 0 )
				{
					size += ret;
					buffers.add( buff );
					sizes.add( ret );
				}
			} while( ret >= 0 );
			fis.close();
			data = new byte[ size ];
			int off = 0;
			for( int n = 0; n < buffers.size(); n++ )
			{
				byte buff[] = buffers.get( n );
				int s = sizes.get( n );
				System.arraycopy( buff, 0, data, off, s );
				off += s;
			}
			Integer id = new Integer( file.getName().split( "[.]" )[ 0 ] );
	
			PlanoActuacaoPrintDataProvider.getProvider().updateLogotipo( id, data );
		}
	}
}
