/*******************************************************************************
 * 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.List;

import org.eclipse.birt.report.model.adapter.oda.ModelOdaAdapter;
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.elements.structures.OdaDataSetParameter;
import org.eclipse.birt.report.model.api.metadata.PropertyValueException;
import org.eclipse.datatools.connectivity.oda.design.DataElementAttributes;
import org.eclipse.datatools.connectivity.oda.design.DataSetDesign;
import org.eclipse.datatools.connectivity.oda.design.DataSetParameters;
import org.eclipse.datatools.connectivity.oda.design.DataSourceDesign;
import org.eclipse.datatools.connectivity.oda.design.DesignFactory;
import org.eclipse.datatools.connectivity.oda.design.ParameterDefinition;
import org.eclipse.datatools.connectivity.oda.design.ParameterMode;
import org.eclipse.datatools.connectivity.oda.design.Properties;

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

public class DataSetParameterAdapterTest extends BaseTestCase
{

	/**
	 * Tests the algorithm to create unique data set parameter names.
	 * 
	 * @throws Exception
	 */

	public void testDataSetParamNames( ) throws Exception
	{
		createDesign( );

		DataSetDesign setDesign = createDataSetDesignForParamNames( );

		OdaDataSetHandle setHandle = new ModelOdaAdapter( )
				.createDataSetHandle( setDesign, designHandle );

		List params = (List) setHandle
				.getProperty( OdaDataSetHandle.PARAMETERS_PROP );
		OdaDataSetParameter param = (OdaDataSetParameter) params.get( 0 );
		assertEquals( 1, param.getPosition( ).intValue( ) );

		param = (OdaDataSetParameter) params.get( 1 );
		assertEquals( 2, param.getPosition( ).intValue( ) );

		setDesign = createDataSetDesignForParamNames1( );

		try
		{
			setHandle = new ModelOdaAdapter( ).createDataSetHandle( setDesign,
					designHandle );
		}
		catch ( PropertyValueException e )
		{
			assertEquals( PropertyValueException.DESIGN_EXCEPTION_VALUE_EXISTS,
					e.getErrorCode( ) );
		}

	}

	/**
	 * Creates a new <code>DataSetDesign</code>. Parameters positions are
	 * duplicate.
	 * 
	 * @return an object of <code>DataSetDesign</code>.
	 */

	static DataSetDesign createDataSetDesignForParamNames1( )
	{
		DataSetDesign setDesign = DesignFactory.eINSTANCE.createDataSetDesign( );
		setDesign.setName( "myDataSet1" ); //$NON-NLS-1$
		setDesign.setDisplayName( "data set display name" ); //$NON-NLS-1$
		setDesign
				.setOdaExtensionDataSetId( OdaDataSetAdapterTest.DATA_SET_EXTENSIONID );

		Properties props = DesignFactory.eINSTANCE.createProperties( );
		props.setProperty( "queryTimeOut", "new public query time out" ); //$NON-NLS-1$//$NON-NLS-2$
		setDesign.setPublicProperties( props );

		props = DesignFactory.eINSTANCE.createProperties( );
		props.setProperty( "queryTimeOut", "new private query time out" ); //$NON-NLS-1$ //$NON-NLS-2$
		setDesign.setPrivateProperties( props );

		DataSetParameters params = DesignFactory.eINSTANCE
				.createDataSetParameters( );
		ParameterDefinition paramDefn = DesignFactory.eINSTANCE
				.createParameterDefinition( );
		DataElementAttributes dataAttrs = DesignFactory.eINSTANCE
				.createDataElementAttributes( );
		dataAttrs.setName( "nativeName1" ); //$NON-NLS-1$
		dataAttrs.setPosition( 1 );
		dataAttrs.setNativeDataTypeCode( 1 );
		paramDefn.setAttributes( dataAttrs );
		paramDefn.setInOutMode( ParameterMode.get( ParameterMode.IN_OUT ) );

		params.getParameterDefinitions( ).add( paramDefn );

		paramDefn = DesignFactory.eINSTANCE.createParameterDefinition( );
		dataAttrs = DesignFactory.eINSTANCE.createDataElementAttributes( );
		dataAttrs.setName( "" ); //$NON-NLS-1$
		dataAttrs.setPosition( 1 );
		dataAttrs.setNativeDataTypeCode( 1 );
		paramDefn.setAttributes( dataAttrs );
		paramDefn.setInOutMode( ParameterMode.get( ParameterMode.IN_OUT ) );
		params.getParameterDefinitions( ).add( paramDefn );

		paramDefn = DesignFactory.eINSTANCE.createParameterDefinition( );
		dataAttrs = DesignFactory.eINSTANCE.createDataElementAttributes( );
		dataAttrs.setName( "nativeName1" ); //$NON-NLS-1$
		dataAttrs.setPosition( 3 );
		dataAttrs.setNativeDataTypeCode( 1 );
		paramDefn.setAttributes( dataAttrs );
		paramDefn.setInOutMode( ParameterMode.get( ParameterMode.IN_OUT ) );

		params.getParameterDefinitions( ).add( paramDefn );
		setDesign.setParameters( params );

		setDesign.setPrimaryResultSetName( "resultset1" ); //$NON-NLS-1$

		setDesign.setQueryText( "new query text" ); //$NON-NLS-1$

		// create the corresponding data source design

		setDesign.setDataSourceDesign( createDataSourceDesign( ) );
		return setDesign;
	}

	/**
	 * Creates a new <code>DataSourceDesign</code>.
	 * 
	 * @return an object of <code>DataSourceDesign</code>.
	 */

	private static DataSourceDesign createDataSourceDesign( )
	{
		DataSourceDesign sourceDesign = DesignFactory.eINSTANCE
				.createDataSourceDesign( );
		sourceDesign.setName( "myDataSource1" ); //$NON-NLS-1$
		sourceDesign.setDisplayName( "data source display name" ); //$NON-NLS-1$
		sourceDesign
				.setOdaExtensionId( OdaDataSetAdapterTest.DATA_SOURCE_EXTENSIONID );

		Properties props = DesignFactory.eINSTANCE.createProperties( );
		props.setProperty( "odaDriverClass", "new drivers" ); //$NON-NLS-1$//$NON-NLS-2$
		props.setProperty( "odaURL", "jdbc:sqlserver://localhost" ); //$NON-NLS-1$//$NON-NLS-2$
		props.setProperty( "odaUser", "new user" ); //$NON-NLS-1$ //$NON-NLS-2$
		sourceDesign.setPublicProperties( props );

		props = DesignFactory.eINSTANCE.createProperties( );
		props.setProperty( "odaDriverClass", "new drivers" ); //$NON-NLS-1$ //$NON-NLS-2$
		props.setProperty( "odaPassword", "new password" ); //$NON-NLS-1$ //$NON-NLS-2$
		sourceDesign.setPrivateProperties( props );

		return sourceDesign;
	}

	/**
	 * Creates a new <code>DataSetDesign</code>. Parameter positions are not
	 * set.
	 * 
	 * @return an object of <code>DataSetDesign</code>.
	 */

	static DataSetDesign createDataSetDesignForParamNames( )
	{
		DataSetDesign setDesign = DesignFactory.eINSTANCE.createDataSetDesign( );
		setDesign.setName( "myDataSet1" ); //$NON-NLS-1$
		setDesign.setDisplayName( "data set display name" ); //$NON-NLS-1$
		setDesign
				.setOdaExtensionDataSetId( OdaDataSetAdapterTest.DATA_SET_EXTENSIONID );

		Properties props = DesignFactory.eINSTANCE.createProperties( );
		props.setProperty( "queryTimeOut", "new public query time out" ); //$NON-NLS-1$//$NON-NLS-2$
		setDesign.setPublicProperties( props );

		props = DesignFactory.eINSTANCE.createProperties( );
		props.setProperty( "queryTimeOut", "new private query time out" ); //$NON-NLS-1$ //$NON-NLS-2$
		setDesign.setPrivateProperties( props );

		DataSetParameters params = DesignFactory.eINSTANCE
				.createDataSetParameters( );
		ParameterDefinition paramDefn = DesignFactory.eINSTANCE
				.createParameterDefinition( );
		DataElementAttributes dataAttrs = DesignFactory.eINSTANCE
				.createDataElementAttributes( );
		dataAttrs.setName( "nativeName1" ); //$NON-NLS-1$
		dataAttrs.setPosition( 0 );
		dataAttrs.setNativeDataTypeCode( 1 );
		paramDefn.setAttributes( dataAttrs );
		paramDefn.setInOutMode( ParameterMode.get( ParameterMode.IN_OUT ) );

		params.getParameterDefinitions( ).add( paramDefn );

		paramDefn = DesignFactory.eINSTANCE.createParameterDefinition( );
		dataAttrs = DesignFactory.eINSTANCE.createDataElementAttributes( );
		dataAttrs.setName( "" ); //$NON-NLS-1$
		dataAttrs.setPosition( 0 );
		dataAttrs.setNativeDataTypeCode( 1 );
		paramDefn.setAttributes( dataAttrs );
		paramDefn.setInOutMode( ParameterMode.get( ParameterMode.IN_OUT ) );
		params.getParameterDefinitions( ).add( paramDefn );

		params.getParameterDefinitions( ).add( paramDefn );
		setDesign.setParameters( params );

		setDesign.setPrimaryResultSetName( "resultset1" ); //$NON-NLS-1$

		setDesign.setQueryText( "new query text" ); //$NON-NLS-1$

		// create the corresponding data source design

		setDesign.setDataSourceDesign( createDataSourceDesign( ) );
		return setDesign;
	}
}
