
package org.eclipse.birt.tests.data.engine.regression;

import java.io.IOException;

import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IGroupDefinition;
import org.eclipse.birt.data.engine.api.ISortDefinition;
import org.eclipse.birt.data.engine.api.querydefn.GroupDefinition;
import org.eclipse.birt.data.engine.api.querydefn.QueryDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;

import testutil.APITestCase;
import testutil.ConfigText;

/**
 * <b>Bug description</b>
 * <p>
 * Description: Set descending to outer group will affect inner group.
 * 
 * Steps to reproduce:
 * <ol>
 * <li>New a sample datasource and dataset from orders.
 * <li>Drag the dataset to Layout.
 * <li>Add a group on ordernumber, interval 10,descending.
 * <li>Add another group on orderdate, interval day 2, ascending.
 * <li>Preview.
 * </ol>
 * Expected result: Data are grouped correctly.
 * <P>
 * Actual result: Inner group didn't work unless you set outer group's sort to
 * asc.
 * 
 * <p>
 * <b>Test description</b>
 * <p>
 * Set 2-level group:
 * <p>
 * The first level group information:
 * <P>
 * Group key: row.ID Interval type: Interval Range: 5
 * <p>
 * The second level group information:
 * <P>
 * Group key: row.SALE_DATE Interval type: Month Range: 2
 * 
 * And the result should be same as golden file
 */
public class Regression_132805 extends APITestCase
{

	// Use database in test framework
	protected DataSourceInfo getDataSourceInfo( )
	{
		return new DataSourceInfo( ConfigText
				.getString( "Api.TestDataforGroup.TableName" ), ConfigText
				.getString( "Api.TestDataforGroup.TableSQL" ), ConfigText
				.getString( "Api.TestDataforGroup.TestDataFileName" ) );
	}

	/**
	 * Test feature of group on Number Decimal type
	 * 
	 * @throws Exception
	 */

	public void testSortingon2Levelgroup( ) throws Exception, IOException
	{
		// group binding defination
		String[] bindingNameGroup = new String[2];
		bindingNameGroup[0] = "GROUP_GROUP1";
		bindingNameGroup[1] = "GROUP_GROUP2";
		IBaseExpression[] bindingExprGroup = new IBaseExpression[2];

		bindingExprGroup[0] = new ScriptExpression( "dataSetRow.ID" );
		bindingExprGroup[1] = new ScriptExpression( "dataSetRow.SALE_DATE" );

		GroupDefinition[] groupDefn = new GroupDefinition[]{
				new GroupDefinition( "group1" ), new GroupDefinition( "group2" )};

		groupDefn[0].setInterval( IGroupDefinition.NUMERIC_INTERVAL );
		groupDefn[0].setKeyExpression( "row.GROUP_GROUP1" );
		groupDefn[0].setIntervalRange( 5 );
		groupDefn[0].setSortDirection( ISortDefinition.SORT_DESC );

		groupDefn[1].setInterval( IGroupDefinition.MONTH_INTERVAL );
		groupDefn[1].setKeyExpression( "row.GROUP_GROUP2" );
		groupDefn[1].setIntervalRange( 2 );
		groupDefn[1].setSortDirection( ISortDefinition.SORT_ASC );

		// column binding
		String[] bindingNameRow = new String[4];
		bindingNameRow[0] = "ROW_ID";
		bindingNameRow[1] = "ROW_DATE";
		bindingNameRow[2] = "ROW_SHOWID";
		bindingNameRow[3] = "ROW_SHODATE";
		IBaseExpression[] bindingExprRow = new IBaseExpression[4];
		bindingExprRow[0] = new ScriptExpression( "dataSetRow.ID" );
		bindingExprRow[1] = new ScriptExpression( "dataSetRow.SALE_DATE" );
		bindingExprRow[2] = new ScriptExpression( "dataSetRow.ID" );
		bindingExprRow[3] = new ScriptExpression( "dataSetRow.SALE_DATE" );

		String[] columnStr = new String[]{"GROUP_ID", "GROUP_DATE", "ID",
				"SALE_DATE"};

		QueryDefinition qd = this.createQuery( bindingNameGroup,
				bindingExprGroup, groupDefn, null, null, null, null, null,
				null, bindingNameRow, bindingExprRow );
		// Generate a group
		String outputStr = getOutputStrForGroupTest( 30, qd, groupDefn.length,
				bindingNameRow, columnStr );
		// use DtE output framework
		testPrint( outputStr );
		// golden file compare
		this.checkOutputFile( );
	}
}