/*******************************************************************************
 * 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>
 * Total.rank(row["column"], 1) returns ranks in descending order. It returns
 * the same results as Total.rank(row["column"], 0) or Total.rank(row["column"],
 * false)
 * <P>
 * To get ascending order, the following expressions work:
 * <P>
 * Total.rank(row["column"], true) Total.rank(row["column"], null)
 * <p>
 * <b>Test description</b>
 * <p>
 * <li>Create a new scripted dataset.
 * <li>New two expressions "Total.rank(dataSetRow.Data1,false)" and
 * "Total.rank(dataSetRow.Data1,true)" on two column bindings.
 * <li>printout and compare it.
 * 
 */

public class Regression_143816 extends APITestCase
{

	public void testRegression143816_TotalRankSortExpression( ) 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*11+count;" + "return true;" );
		String[] scriptColumnNames = new String[]{"Data1", "Rank1", "Rank2"};
		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( "Total.rank(row.Data1,false)" ),
				new ScriptExpression( "Total.rank(row.Data1,true)" )};

		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 qr = pq.execute( jsScope );
		IResultIterator ri = qr.getResultIterator( );

		// print the result
		int rowIndex = 0;
		String outputTile = "";
		for ( int i = 0; i < scriptColumnNames.length; i++ )
		{
			outputTile += scriptColumnNames[i] + "		";
		}
		testPrintln( outputTile );
		while ( ri.next( ) )
		{
			String outputStr = "";

			for ( int i = 0; i < scriptColumnNames.length; i++ )
			{
				Object value = ri.getValue( scriptColumnNames[i] );
				outputStr += " " + value.toString( ) + "		";
			}
			rowIndex++;
			testPrintln( outputStr );
		}
		dataEngine.shutdown( );
		checkOutputFile( );
	}

	protected DataSourceInfo getDataSourceInfo( )
	{
		return null;
	}

}
