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

import java.util.ArrayList;
import java.util.List;

import org.eclipse.birt.data.engine.api.IJoinCondition;
import org.eclipse.birt.data.engine.api.IResultIterator;
import org.eclipse.birt.data.engine.api.querydefn.JoinCondition;
import org.eclipse.birt.data.engine.api.querydefn.JointDataSetDesign;
import org.eclipse.birt.data.engine.api.querydefn.OdaDataSetDesign;
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>Regression test case description:</b>
 * <p>
 * Error occurs when Preview Results of Inner Join datasets
 * </p>
 * <p>
 * Description:
 * </p>
 * Create a sample datasource and dataset, and then create a Joint dataset.
 * Error occurs when preview results in the data set dialog.
 * <p>
 * Steps to reproduce:
 * </p>
 * <ol>
 * <li>Create a sample datasource, and a dataset with sql statement "select *
 * from CLASSICMODELS.CUSTOMERS".</li>
 * <li>Create a Joint dataset, the left and right dataset is the same.</li>
 * <li>Preview result in the dataset dialog, error pops up continuely.</li>
 * </p>
 * </ol>
 * <b>Test description:</b>
 * <p>
 * Create a joint dataSet from dSet1 and generate the table, comparing it with
 * golden file
 * 
 * </p>
 */

public class Regression_135490 extends APITestCase
{

	String callsTableName;

	protected void setUp( ) throws Exception
	{
		super.setUp( );

		prepareDataSet( new DataSourceInfo( ConfigText
				.getString( "Api.TestData.TableName" ), ConfigText
				.getString( "Api.TestData.TableSQL" ), ConfigText
				.getString( "Api.TestData.TestDataFileName" ) ) );

		callsTableName = ConfigText.getString( "Api.TestData.TableName" );
	}

	/*
	 * @see org.eclipse.birt.data.engine.api.APITestCase#getDataSourceInfo()
	 */
	protected DataSourceInfo getDataSourceInfo( )
	{
		return new DataSourceInfo( ConfigText
				.getString( "Api.TestData.TableName" ), ConfigText
				.getString( "Api.TestData.TableSQL" ), ConfigText
				.getString( "Api.TestData.TestDataFileName" ) );
	}

	public void testBirtStr( ) throws Exception
	{
		int joinType = 0;

		OdaDataSetDesign dset = newDataSet( "dSet1",
				"Select COUNTRY, CITY, AMOUNT FROM " + this.getTestTableName( ) );

		/*
		 * dset1.addComputedColumn( new ComputedColumn( "CP1", "row.ID + 10") );
		 * dset2.addComputedColumn( new ComputedColumn( "CP2", "row.ID + 20") );
		 */
		List a = new ArrayList( );
		a.add( new JoinCondition( new ScriptExpression( "dataSetRow.COUNTRY" ),
				new ScriptExpression( "dataSetRow.COUNTRY" ),
				IJoinCondition.OP_EQ ) );

		JointDataSetDesign dset2 = new JointDataSetDesign( "dset2", dset
				.getName( ), dset.getName( ), joinType, a );
		dataEngine.defineDataSet( dset2 );

		QueryDefinition queryDefn = this.newReportQuery( dset2 );

		String[] bindingNameRow = new String[6];
		bindingNameRow[0] = "ROW1_COUNTRY";
		bindingNameRow[1] = "ROW1_CITY";
		bindingNameRow[2] = "ROW1_AMOUNT";
		bindingNameRow[3] = "ROW12_COUNTRY";
		bindingNameRow[4] = "ROW12_CITY";
		bindingNameRow[5] = "ROW12_AMOUNT";
		ScriptExpression[] bindingExprRow = new ScriptExpression[]{

		new ScriptExpression( "dataSetRow[\"dSet11::COUNTRY\"]" ),
				new ScriptExpression( "dataSetRow[\"dSet11::CITY\"]" ),
				new ScriptExpression( "dataSetRow[\"dSet11::AMOUNT\"]" ),
				new ScriptExpression( "dataSetRow[\"dSet12::COUNTRY\"]" ),
				new ScriptExpression( "dataSetRow[\"dSet12::CITY\"]" ),
				new ScriptExpression( "dataSetRow[\"dSet12::AMOUNT\"]" ),};

		for ( int i = 0; i < bindingNameRow.length; i++ )
			queryDefn.addResultSetExpression( bindingNameRow[i],
					bindingExprRow[i] );

		IResultIterator resultIter = this.executeQuery( queryDefn );

		// Iterator it = (Iterator) resultIter;
		while ( resultIter.next( ) )
		{
			String outputStr = "";
			for ( int i = 0; i < bindingNameRow.length; i++ )
			{
				Object value = resultIter.getValue( bindingNameRow[i] );
				outputStr += bindingExprRow[i].getText( ).replaceAll(
						"dataSetRow", "row" )
						+ " " + value.toString( ) + "    ";
			}
			testPrintln( outputStr );
		}

	}
}