
package org.eclipse.birt.test.performance;

import java.util.HashMap;
import java.util.List;

import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.RenderOptionBase;
import org.eclipse.birt.test.performance.parser.ReportDesignNode;
import org.eclipse.birt.test.performance.parser.Utility;

/**
 * RunRenderThread - represent a thread to perform engine RunRender task
 */
public class RunRenderThread extends BaseTaskThread
{

	/**
	 * Constructor
	 * 
	 * @param threadName -
	 *            thread identifier, use to identify this thread
	 * @param id
	 * @param context
	 * @param semaphore -
	 *            semaphore instance, use to call back thread monitor
	 * @param basePath -
	 *            configured basePath to input and output folder
	 */
	public RunRenderThread( String threadName, int id,
			ExecutionContext context, Semaphore semaphore )
	{
		super( threadName, id, context, semaphore );
	}

	public void run( )
	{
		String basedir = Constants.BASE_PATH;

		String locale = this.testcase.getLocale( );
		String format = this.testcase.getFormat( );
		String encoding = this.testcase.getEncoding( );
		List inputs = this.testcase.getReports( );

		try
		{
			long beginTime;
			long endTime;

			for ( int i = 0; i < inputs.size( ); i++ )
			{
				// execution, task begin
				ReportDesignNode reportdesign = (ReportDesignNode) inputs.get( i );

				String reportfile = reportdesign.getName( );
				int repeat = reportdesign.getRepeat( );

				System.out.println( "Begin, Thread[name=\"" + this.getName( ) + "\", id=\"" + this.id + "\"] report[" + reportfile + "]" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

				beginTime = System.currentTimeMillis( );

				for ( int counter = 0; counter < repeat; counter++ )
				{
					String reportfullpath = basedir
							+ Constants.INPUT_PATH + reportfile;

					IReportRunnable runnable = engine.openReportDesign( reportfullpath );

					IRunAndRenderTask task = engine.createRunAndRenderTask( runnable );
					task.setLocale( Utility.getLocale( locale ) );

					IRenderOption options = null;
					if ( format.equalsIgnoreCase( "pdf" ) ) //$NON-NLS-1$
					{
						options = new RenderOptionBase( );
						String outputFile = basedir
								+ Constants.RUNRENDER_PATH + reportfile + id
								+ Constants.PDF_EXT;
						options.setOutputFileName( outputFile );
					}
					else
					{
						options = new HTMLRenderOption( );
						String outputFile = basedir
								+ Constants.RUNRENDER_PATH + reportfile + id
								+ Constants.HTML_EXT;
						options.setOutputFileName( outputFile );

						HTMLRenderContext renderContext = new HTMLRenderContext( );
						renderContext.setImageDirectory( "image" ); //$NON-NLS-1$
						HashMap appContext = new HashMap( );
						appContext.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
								renderContext );
						task.setAppContext( appContext );
					}

					options.setOutputFormat( format );
					options.getOutputSetting( )
							.put( HTMLRenderOption.URL_ENCODING, encoding );

					task.setRenderOption( options );
					task.run( );
					task.close( );
				}

				// execution, task end
				endTime = System.currentTimeMillis( );

				// record single average task time
				long timespan = ( endTime - beginTime ) / repeat;
				this.testcase.getCaseLogger( ).logResult( this.getName( ),
						reportdesign,
						this.id,
						timespan );

				System.out.println( "End, Thread[name=\"" + this.getName( ) + "\", id=\"" + this.id + "\"] report[" + reportfile + "] timespan[" + timespan + "]" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

			}

			this.semaphore.release( );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			System.exit( -1 );
		}
	}

}