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

import java.util.Map;

import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Wrapper;

public class ScriptableParameters extends BaseScriptable
{

	private Map parameters;
	private final String JS_CLASS_NAME = "ScriptableParameters";

	public ScriptableParameters( Map parameters, Scriptable scope )
	{
		setParentScope( scope );
		this.parameters = parameters;
	}

	public Object get( String name, Scriptable start )
	{
		Object result = getScriptableParameter( name );
		if ( result == null )
		{
			String errorMessage = "Report parameter \"" + name + "\" does not exist.";
			throw new JavaScriptException( errorMessage, "<unknown>", -1 );
		}
		return result;
	}

	private ScriptableParameter getScriptableParameter( String name )
	{
		if ( parameters.containsKey( name ) )
		{
			return new ScriptableParameter( parameters, name, getParentScope( ) );
		}
		return null;
	}

	public boolean has( String name, Scriptable start )
	{
		return parameters.get( name ) != null;
	}

	/**
	 * Support setting parameter value by following methods:
	 * <li> params["a"] = new ParameterAttribute( "value", "displayText");
	 * <li> params["a"] = params["b"]
	 * <li> params["a"] = "value"
	 */
	public void put( String name, Scriptable start, Object value )
	{
		ParameterAttribute entry = (ParameterAttribute) parameters
				.get( name );
		if ( entry == null )
		{
			entry = new ParameterAttribute( );
			parameters.put( name, entry );
		}

		if ( value instanceof ScriptableParameter )
		{
			ScriptableParameter scriptableParameter = (ScriptableParameter) value;
			Object paramValue = scriptableParameter.get( "value", this );
			String displayText = (String) scriptableParameter.get(
					"displayText", this );
			entry.setValue( paramValue );
			entry.setDisplayText( displayText );
			return;
		}
		
		if ( value instanceof Wrapper )
		{
			value = ( (Wrapper) value ).unwrap( );
		}

		if ( value instanceof ParameterAttribute )
		{
			ParameterAttribute param = (ParameterAttribute) value;
			entry.setValue( param.getValue( ) );
			entry.setDisplayText( param.getDisplayText( ) );
		}
		else
		{
			entry.setValue( value );
		}
	}

	public String getClassName( )
	{
		return JS_CLASS_NAME;
	}
}

@@ -1184,17 +1184,22 @@
 %22 + name
- 
+%0A%09%09%09%09%09
 + %22%5C%22 do
@@ -1210,17 +1210,16 @@
 ot exist
-.
 %22;%0A%09%09%09th
@@ -1276,32 +1276,32 @@
 wn%3E%22, -1 );%0A%09%09%7D%0A
-
 %09%09return result;
@@ -1305,16 +1305,121 @@
 lt;%0A%09%7D%0A%0A
+%09public Object get( int index, Scriptable start )%0A%09%7B%0A%09%09return get( String.valueOf( index ), start );%0A%09%7D%0A%0A
 %09private

Fix bugzilla bug 261161 : Exception is thrown out when preview a report with a parameter with number as its name [0705].