/*******************************************************************************
 * 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 java.util.Date;

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>
 * If a Data Set's fetch script sets a data set column to a Javascript Date
 * value, the value is not accepted by BIRT engine. It becomes null instead.
 * 
 * See attached report design as an example. The fetch script of the data set
 * does the following:
 * <P>
 * ...
 * <P>
 * row.d = new Date();
 * <p>
 * row.d2 = new Packages.java.util.Date();
 * <P>
 * ...
 * <p>
 * 
 * If you preview the data set, column "d" is empty, while column "d2" has the
 * correct value (the current time). Since Javascript code is supposed to use
 * its native Date class to represent date/time value, it is unacceptable that
 * such values are not accepted by BIRT.
 * 
 * <p>
 * <b>Test description</b>
 * <p>
 * New a scripted dataset,input JSscript :
 * <P>
 * "row.Date_1 = new Date();
 * <P>
 * "row.Date_2 = new Packages.java.util.Date();
 * <P>
 * on fetch method.
 * <P>
 * Confirm that the result of the two column can be got
 * <P>
 * 
 */

public class Regression_123153 extends APITestCase
{

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

		dset.setOpenScript( "count=0;" );
		dset.setFetchScript( "if ( ++count > 1 ) return false;"
				+ "row.Date_1 = new Date(10,10,2006);"
				+ "row.Date_2 = new Packages.java.util.Date(10,10,2006);"
				+ "return true;" );

		String[] scriptColumnNames = new String[]{"Date_1", "Date_2"};

		ArrayList list = new ArrayList( );
		for ( int i = 0; i < scriptColumnNames.length; i++ )
		{
			ColumnDefinition col = new ColumnDefinition( scriptColumnNames[i] );
			col.setDataType( DataType.DATE_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 qr = pq.execute( jsScope );
		IResultIterator ri = qr.getResultIterator( );

		Object date1 = ri.getValue( scriptColumnNames[0] );
		Object date2 = ri.getValue( scriptColumnNames[1] );
		assertNotNull( date1 );
		assertNotNull( date2 );
		assertTrue(date1 instanceof Date);
		assertTrue(date2 instanceof Date);
		
		testPrint( date1 + "\n" + date2 );
		assertEquals( ((Date)date1).getTime( ), ((Date)date2).getTime( ));

	}

	protected DataSourceInfo getDataSourceInfo( )
	{
		return null;
	}

}
