/*******************************************************************************
 * 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.report.model.adapter.oda.api;

import java.util.Iterator;

import org.eclipse.birt.report.model.adapter.oda.impl.ModelOdaAdapter;
import org.eclipse.birt.report.model.adapter.oda.model.DesignValues;
import org.eclipse.birt.report.model.adapter.oda.model.ModelFactory;
import org.eclipse.birt.report.model.adapter.oda.util.BaseTestCase;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSetParameterHandle;
import org.eclipse.birt.report.model.api.ScalarParameterHandle;
import org.eclipse.datatools.connectivity.oda.design.DataElementAttributes;
import org.eclipse.datatools.connectivity.oda.design.DataElementUIHints;
import org.eclipse.datatools.connectivity.oda.design.DataSetDesign;
import org.eclipse.datatools.connectivity.oda.design.DataSetParameters;
import org.eclipse.datatools.connectivity.oda.design.DesignFactory;
import org.eclipse.datatools.connectivity.oda.design.DynamicValuesQuery;
import org.eclipse.datatools.connectivity.oda.design.ElementNullability;
import org.eclipse.datatools.connectivity.oda.design.InputElementAttributes;
import org.eclipse.datatools.connectivity.oda.design.InputElementUIHints;
import org.eclipse.datatools.connectivity.oda.design.InputParameterAttributes;
import org.eclipse.datatools.connectivity.oda.design.InputPromptControlStyle;
import org.eclipse.datatools.connectivity.oda.design.ParameterDefinition;
import org.eclipse.datatools.connectivity.oda.design.ParameterMode;
import org.eclipse.datatools.connectivity.oda.design.ScalarValueChoices;
import org.eclipse.datatools.connectivity.oda.design.ScalarValueDefinition;

/**
 * Test cases to convert Oda data set parameters and ROM data set parameter and
 * linked report parameters.
 * 
 */

public class ReportParameterAdapterTest extends BaseTestCase
{

	/**
	 * Converts ROM data set parameters with report parameters to ODA data set
	 * parameters.
	 * 
	 * @throws Exception
	 */

	public void testToODADataSetParamWithReportParam( ) throws Exception
	{
		// create oda set handle.

		openDesign( "OdaDataSetConvertReportParamTest.xml" ); //$NON-NLS-1$
		OdaDataSetHandle setHandle = (OdaDataSetHandle) designHandle
				.findDataSet( "myDataSet1" ); //$NON-NLS-1$

		DataSetDesign setDesign = new ModelOdaAdapter( )
				.createDataSetDesign( setHandle );

		DesignValues values = ModelFactory.eINSTANCE.createDesignValues( );
		values.setDataSetParameters( setDesign.getParameters( ) );

		saveDesignValuesToFile( values, "DataSetParamWithReportParam_out.xml" ); //$NON-NLS-1$

		assertTrue( compareTextFile( "DataSetParamWithReportParam_golden.xml", //$NON-NLS-1$
				"DataSetParamWithReportParam_out.xml" ) ); //$NON-NLS-1$

	}

	/**
	 * Converts ODA data set parameters to ROM data set parameters.
	 * 
	 * <ul>
	 * <li>no the latest design session reponse, update ROM values. Default
	 * value is not copied to data set parameter since it has linked report
	 * parameter.
	 * <li>if the latest design session reponse changed, update ROM values.
	 * <li>if the latest design session reponse didn't change and ROM values
	 * changed, don't update ROM values.
	 * <li>update native type and default value on data set parameter
	 * </ul>
	 * 
	 * @throws Exception
	 */

	public void testToROMDataSetParamWithReportParam( ) throws Exception
	{
		// create oda set handle.

		openDesign( "OdaDataSetConvertReportParamTest.xml" ); //$NON-NLS-1$
		OdaDataSetHandle setHandle = (OdaDataSetHandle) designHandle
				.findDataSet( "myDataSet1" ); //$NON-NLS-1$

		// get the latest data set design.

		DataSetDesign setDesign = new ModelOdaAdapter( )
				.createDataSetDesign( setHandle );

		// oda data set design changed, update ROM values. still keep report
		// parameter link.

		DataSetParameters params = setDesign.getParameters( );
		ParameterDefinition param = (ParameterDefinition) params
				.getParameterDefinitions( ).get( 0 );

		updateParameterDefinition1( param );

		new ModelOdaAdapter( )
				.updateDataSetHandle( setDesign, setHandle, false );

		saveAs( "OdaDataSetConvertReportParamTest_out.xml" ); //$NON-NLS-1$
		compareTextFile( "OdaDataSetConvertReportParamTest_golden.xml", //$NON-NLS-1$
				"OdaDataSetConvertReportParamTest_out.xml" ); //$NON-NLS-1$

		openDesign( "OdaDataSetConvertReportParamTest_1.xml" ); //$NON-NLS-1$
		setHandle = (OdaDataSetHandle) designHandle.findDataSet( "myDataSet1" ); //$NON-NLS-1$

		// get the latest data set design.

		setDesign = new ModelOdaAdapter( ).createDataSetDesign( setHandle );

		// oda data set design changed, update ROM values. still keep report
		// parameter link.

		params = setDesign.getParameters( );
		param = (ParameterDefinition) params.getParameterDefinitions( ).get( 0 );

		// oda data set design changed, update ROM values. still keep report
		// parameter link.

		updateParameterDefinition1( param );
		new ModelOdaAdapter( )
				.updateDataSetHandle( setDesign, setHandle, false );

		saveAs( "OdaDataSetConvertReportParamTest_1_out.xml" ); //$NON-NLS-1$
		compareTextFile( "OdaDataSetConvertReportParamTest_1_golden.xml", //$NON-NLS-1$
				"OdaDataSetConvertReportParamTest_1_out.xml" ); //$NON-NLS-1$

		openDesign( "OdaDataSetConvertReportParamTest_1.xml" ); //$NON-NLS-1$
		setHandle = (OdaDataSetHandle) designHandle.findDataSet( "myDataSet1" ); //$NON-NLS-1$

		// get the latest data set design.

		setDesign = new ModelOdaAdapter( ).createDataSetDesign( setHandle );

		// oda data set design changed, update ROM values. still keep report
		// parameter link.

		params = setDesign.getParameters( );
		param = (ParameterDefinition) params.getParameterDefinitions( ).get( 0 );

		// change the direction of parameter, do not keep report parameter link.

		updateParameterDefinition2( param );
		new ModelOdaAdapter( )
				.updateDataSetHandle( setDesign, setHandle, false );

		saveAs( "OdaDataSetConvertReportParamTest_2_out.xml" ); //$NON-NLS-1$
		compareTextFile( "OdaDataSetConvertReportParamTest_2_golden.xml", //$NON-NLS-1$
				"OdaDataSetConvertReportParamTest_2_out.xml" ); //$NON-NLS-1$

		// the oda data set design is not changed. ROM values are changed.
		// Should keep rom values.

		openDesign( "OdaDataSetConvertReportParamTest_1.xml" ); //$NON-NLS-1$
		setHandle = (OdaDataSetHandle) designHandle.findDataSet( "myDataSet1" ); //$NON-NLS-1$

		// get the latest data set design.

		setDesign = new ModelOdaAdapter( ).createDataSetDesign( setHandle );

		Iterator paramHandles = setHandle.parametersIterator( );
		OdaDataSetParameterHandle paramHandle = (OdaDataSetParameterHandle) paramHandles
				.next( );
		paramHandle.setIsOutput( true );
		paramHandle.setIsInput( false );
		paramHandle.setDefaultValue( "not updated default value" ); //$NON-NLS-1$

		String reportParamName = paramHandle.getParamName( );
		ScalarParameterHandle reportParam = (ScalarParameterHandle) designHandle
				.findParameter( reportParamName );
		reportParam.setPromptText( "not updated prompt text" ); //$NON-NLS-1$
		reportParam.setDefaultValue( "not updated default value" ); //$NON-NLS-1$

		new ModelOdaAdapter( )
				.updateDataSetHandle( setDesign, setHandle, false );

		saveAs( "OdaDataSetConvertReportParamTest_3_out.xml" ); //$NON-NLS-1$
		compareTextFile( "OdaDataSetConvertReportParamTest_3_golden.xml", //$NON-NLS-1$
				"OdaDataSetConvertReportParamTest_3_out.xml" ); //$NON-NLS-1$
	}

	/**
	 * Updates a oda parameter definition. Keep the direction.
	 * 
	 * @param param
	 */

	private void updateParameterDefinition1( ParameterDefinition param )
	{
		DataElementAttributes dataAttrs = param.getAttributes( );
		dataAttrs.setNullability( ElementNullability
				.get( ElementNullability.NOT_NULLABLE ) );

		DataElementUIHints dataUIHints = dataAttrs.getUiHints( );
		dataUIHints.setDisplayName( "new prompt text for report param 1" ); //$NON-NLS-1$
		dataUIHints.setDescription( "new help text for report param 1" ); //$NON-NLS-1$

		InputParameterAttributes paramAttrs = param.getInputAttributes( );
		InputElementAttributes elementAttrs = paramAttrs.getElementAttributes( );

		elementAttrs
				.setDefaultScalarValue( "new default value for report param 1" ); //$NON-NLS-1$
		elementAttrs.setOptional( true );
		elementAttrs.setMasksValue( false );

		ScalarValueChoices chocies = DesignFactory.eINSTANCE
				.createScalarValueChoices( );

		ScalarValueDefinition choice = DesignFactory.eINSTANCE
				.createScalarValueDefinition( );
		choice.setDisplayName( "new choice display name 1" ); //$NON-NLS-1$
		choice.setValue( "new choice value 1" ); //$NON-NLS-1$

		chocies.getScalarValues( ).add( choice );

		elementAttrs.setStaticValueChoices( chocies );

		DynamicValuesQuery dynamicValue = DesignFactory.eINSTANCE
				.createDynamicValuesQuery( );
		dynamicValue.setValueColumn( "new value column 1" ); //$NON-NLS-1$
		dynamicValue.setDisplayNameColumn( "new lable column 1" ); //$NON-NLS-1$
		dynamicValue.setDataSetDesign( OdaDataSetAdapterTest
				.createDataSetDesign( ) );

		elementAttrs.setDynamicValueChoices( dynamicValue );

		InputElementUIHints elementUIHints = DesignFactory.eINSTANCE
				.createInputElementUIHints( );
		elementUIHints.setPromptStyle( InputPromptControlStyle
				.get( InputPromptControlStyle.RADIO_BUTTON ) );
		elementAttrs.setUiHints( elementUIHints );

	}

	/**
	 * Updates a oda parameter definition. Change the direction from input to
	 * output.
	 * 
	 * @param param
	 */

	private void updateParameterDefinition2( ParameterDefinition param )
	{
		param.setInOutMode( ParameterMode.get( ParameterMode.OUT ) );
	}
}
