/*******************************************************************************
 * 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.tests.data.engine.regression;

import java.util.ArrayList;

import org.eclipse.birt.core.data.DataType;
import org.eclipse.birt.data.engine.api.IPreparedQuery;
import org.eclipse.birt.data.engine.api.IQueryResults;
import org.eclipse.birt.data.engine.api.IResultIterator;
import org.eclipse.birt.data.engine.api.IScriptExpression;
import org.eclipse.birt.data.engine.api.querydefn.ColumnDefinition;
import org.eclipse.birt.data.engine.api.querydefn.QueryDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptDataSetDesign;
import org.eclipse.birt.data.engine.api.querydefn.ScriptDataSourceDesign;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;

import testutil.APITestCase;

/**
 * <b>Bug description</b>
 * <p>
 * Description:
 * <p>
 * In the second time use of scripted data set, the result is null
 * <p>
 * <ol>
 * Steps to reproduce:
 * <li>Create a scripted data set
 * <li>Drag this data set to report design to create a table
 * <li>Do step 4 again to create the second table
 * <li>Preview report, the first table has result but the second table is null
 * </ol>
 * Expected: the first table and the second table have the same result
 * <p>
 * <b>Test description</b>
 * <p>
 * New a script datasource and dataset in the test. Execute the qurey twice, and
 * Confirm all the qurey can generate a table.
 * 
 */

public class Regression_121166 extends APITestCase
{

	public void testRequeryScriptedDataset( ) throws Exception
	{
		ScriptDataSourceDesign dsource = new ScriptDataSourceDesign(
				"ScriptedDataSource" );
		ScriptDataSetDesign dset = new ScriptDataSetDesign( "ScriptedDataSet" );

		dset.setOpenScript( "count=0;" );
		dset.setFetchScript( "if ( ++count > 10 ) return false;"
				+ "row.Data1 = count;" + "row.Data2 = count+100;"
				+ "return true;" );

		String[] scriptColumnNames = new String[]{"Data1", "Data2"};

		ArrayList list = new ArrayList( );
		for ( int i = 0; i < scriptColumnNames.length; i++ )
		{
			ColumnDefinition col = new ColumnDefinition( scriptColumnNames[i] );
			col.setDataType( DataType.ANY_TYPE );

			list.add( col );
		};

		dset.getResultSetHints( ).addAll( list );

		dset.setDataSource( dsource.getName( ) );
		dataEngine.defineDataSource( dsource );
		dataEngine.defineDataSet( dset );

		IScriptExpression[] scriptExprs = new IScriptExpression[]{
				new ScriptExpression( "dataSetRow." + scriptColumnNames[0] ),
				new ScriptExpression( "dataSetRow." + scriptColumnNames[1] )};

		QueryDefinition rqDefn = new QueryDefinition( );
		// add expressions

		rqDefn.setDataSetName( "ScriptedDataSet" );
		for ( int i = 0; i < scriptExprs.length; i++ )
		{
			rqDefn
					.addResultSetExpression( scriptColumnNames[i],
							scriptExprs[i] );
		}

		IPreparedQuery pq = dataEngine.prepare( rqDefn );

		IQueryResults qr1 = pq.execute( jsScope );
		IQueryResults qr2 = pq.execute( jsScope );
		IResultIterator ri[] = {qr1.getResultIterator( ),
				qr2.getResultIterator( )};

		for ( int index = 0; index < ri.length; index++ )
		{

			int rowIndex = 0;
			testPrintln( "\nPrint Table-" + index );
			while ( ri[index].next( ) )
			{
				String outputStr = "";
				for ( int i = 0; i < scriptColumnNames.length; i++ )
				{
					Object value = ri[index].getValue( scriptColumnNames[i] );
					outputStr += scriptExprs[i].getText( ).replaceAll(
							"dataSetRow", "row" )
							+ " value is:" + value.toString( ) + "    ";
				}

				rowIndex++;
				testPrintln( outputStr );
			}

		}
		dataEngine.shutdown( );
		checkOutputFile( );
	}

	protected DataSourceInfo getDataSourceInfo( )
	{
		return null;
	}

}
