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

import java.util.Iterator;
import java.util.List;

import org.eclipse.birt.core.script.BirtHashMap;
import org.eclipse.birt.report.model.api.DesignElementHandle;

/**
 * Represents the scriptable object for <code>ReportDefinition</code>.
 */

public class NativeReportDefinition extends NativeElementDefinition
{

	/**
	 * The class name in JavaScript.
	 */

	public static final String CLASS_NAME = "ReportDefn"; //$NON-NLS-1$

	/**
	 * The zero-argument constructor used by Rhino runtime to create instances
	 */

	public NativeReportDefinition( )
	{
	}

	/**
	 * JavaScript function that is the constructor .
	 * 
	 * @param reportDefn
	 *            a report definition instance
	 */

	public void jsConstructor( Object reportDefn )
	{
		super.jsConstructor( reportDefn );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mozilla.javascript.Scriptable#getClassName()
	 */

	public String getClassName( )
	{
		return CLASS_NAME;
	}

	public Object jsFunction_findStyle( String name )
	{
		return ( (ReportDefinition) elementDefn ).findStyle( name );
	}

	public Object jsFunction_findReportItem( String name )
	{
		return ( (ReportDefinition) elementDefn ).findReportItem( name );
	}

	public Object jsFunction_findDataSource( String name )
	{
		return ( (ReportDefinition) elementDefn ).findDataSource( name );
	}

	public Object jsFunction_findDataSet( String name )
	{
		return ( (ReportDefinition) elementDefn ).findDataSet( name );
	}

	public Object jsFunction_findParameter( String name )
	{
		return ( (ReportDefinition) elementDefn ).findParameter( name );
	}

	public Object jsGet_styles( )
	{
		List list = ( (ReportDefinition) elementDefn ).getStyles( );
		return convertToBirtHashMap( list );
	}

	public Object jsGet_dataSources( )
	{
		List list = ( (ReportDefinition) elementDefn ).getDataSourses( );
		return convertToBirtHashMap( list );
	}

	public Object jsGet_dataSets( )
	{
		List list = ( (ReportDefinition) elementDefn ).getDataSets( );
		return convertToBirtHashMap( list );
	}

	public Object jsGet_parameters( )
	{
		List list = ( (ReportDefinition) elementDefn ).getParameters( );
		return convertToBirtHashMap( list );
	}

	/**
	 * 
	 * @param list
	 * @return
	 */
	private BirtHashMap convertToBirtHashMap( List list )
	{

		BirtHashMap map = new BirtHashMap( );
		Iterator itr = list.iterator( );
		while ( itr.hasNext( ) )
		{
			DesignElementHandle elementHandle = (DesignElementHandle) itr
					.next( );
			assert elementHandle.getName( )!= null;
			map.put( elementHandle.getName( ), new ElementDefinition(
					elementHandle ) );
		}
		return map;
	}

}