/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.test.performance;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.core.framework.PlatformConfig;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.test.performance.parser.TestCaseNode;

public class ExecutionContext
{
	private String timeStamp = null;
	private IReportEngine engine = null;

	final static SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat(
			"yyyyMMdd_hhmmss" ); //$NON-NLS-1$

	private TestCaseNode textcase = null;

	public ExecutionContext( )
	{
		this.timeStamp = "_" + DATETIME_FORMATTER.format( Calendar.getInstance( ).getTime( ) ) + ".log"; //$NON-NLS-1$//$NON-NLS-2$

		EngineConfig config = new EngineConfig( );
		try
		{
			this.engine = this.createReportEngine( config );
		}
		catch ( BirtException e )
		{
			e.printStackTrace( );
		}
	}

	IReportEngine createReportEngine( EngineConfig config )
			throws BirtException
	{
		if ( config == null )
		{
			config = new EngineConfig( );
		}

		Platform.startup( new PlatformConfig( ) );
		// assume we has in the platform
		Object factory = Platform
				.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
		if ( factory instanceof IReportEngineFactory )
		{
			return ( (IReportEngineFactory) factory )
					.createReportEngine( config );
		}
		return null;
	}
	
	public TestCaseNode getTestcase( )
	{
		return textcase;
	}

	public void setTestcase( TestCaseNode testcase )
	{
		this.textcase = testcase;
	}

	public IReportEngine getEngine( )
	{
		return engine;
	}

	public String getTimeStamp( )
	{
		return timeStamp;
	}
}
