
package org.eclipse.birt.test.performance;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.eclipse.birt.core.archive.FileArchiveWriter;
import org.eclipse.birt.core.archive.FolderArchiveWriter;
import org.eclipse.birt.core.archive.IDocArchiveWriter;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunTask;
import org.eclipse.birt.test.performance.parser.ReportDesignNode;
import org.eclipse.birt.test.performance.parser.Utility;

/**
 * RunThread - represent a thread to perform engine Run task
 */
public class RunThread extends BaseTaskThread
{

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

	public void run( )
	{
		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( );
				String docType = reportdesign.getDocType( );
				int repeat = reportdesign.getRepeat( );
				String basedir = Constants.BASE_PATH;

				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 );
					IRunTask task = engine.createRunTask( runnable );
					task.setAppContext( new HashMap( ) );
					IDocArchiveWriter archive = null;
					String targetFile = null;
					try
					{
						if ( docType.equalsIgnoreCase( "file" ) )
						{
							targetFile = basedir
								+ Constants.RUN_PATH
								+ Utility.getReportName( reportfile ) + id
								+ Constants.DOC_EXT;
							archive = new FileArchiveWriter( targetFile );
						}
						else // folder type doc
						{
							targetFile = basedir
								+ Constants.RUN_PATH
								+ Utility.getReportName( reportfile ) + id
								+ "/";
							archive = new FolderArchiveWriter( targetFile );
						}
					}
					catch ( IOException e )
					{
						e.printStackTrace( );
					}
					task.run( archive );
					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 );
		}
	}
}