
/*******************************************************************************
 * Copyright (c) 2004, 2008 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.functionservice.impl;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.script.JavascriptEvalUtil;
import org.eclipse.birt.core.script.functionservice.IScriptFunction;
import org.eclipse.birt.core.script.functionservice.IScriptFunctionCategory;
import org.eclipse.birt.core.script.functionservice.IScriptFunctionContext;
import org.mozilla.javascript.BaseFunction;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.WrappedException;

import com.ibm.icu.text.Collator;

/**
 * This class wraps a function that defined by user using java to a scriptable object
 * that processable by Rhino.
 * 
 */

public class CategoryWrapper extends ScriptableObject
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private transient IScriptFunctionCategory category;
	
	/**
	 * Constructor
	 * 
	 * @param category
	 * @throws BirtException
	 */
	public CategoryWrapper( IScriptFunctionCategory category ) throws BirtException
	{
		assert category != null;
		
		this.category = category;
		IScriptFunction[] functions = category.getFunctions( );
		for ( int i = 0; i < functions.length; i++ )
		{
			final IScriptFunction function = functions[i];
			this.defineProperty( functions[i].getName( ), new BaseFunction( ) {

				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public Object call( Context cx, Scriptable scope,
						Scriptable thisObj, java.lang.Object[] args )
				{
					Object[] convertedArgs = JavascriptEvalUtil.convertToJavaObjects( args );
					try
					{
						final IScriptFunctionContext scriptFunctionContext = getIScriptFunctionContext( scope );
						final Collator collator = getCollator( scope );

						IScriptFunctionContext wrappedScriptFunctionContext = new IScriptFunctionContext( ) {

							public Object findProperty( String name )
							{
								if( "compare_locale".equals( name ) )
									return collator;
								if ( scriptFunctionContext == null )
									return null;
								return scriptFunctionContext.findProperty( name );
							}
						};
						return function.execute( convertedArgs,
								wrappedScriptFunctionContext );
					}
					catch ( BirtException e )
					{
						throw new WrappedException( e );
					}

				}

				private IScriptFunctionContext getIScriptFunctionContext( Scriptable scope )
				{
					if ( scope == null )
						return null;
					Object obj = scope.get( org.eclipse.birt.core.script.functionservice.IScriptFunctionContext.FUNCTION_BEAN_NAME,
							scope );
					if ( obj == org.mozilla.javascript.UniqueTag.NOT_FOUND )
					{
						return getIScriptFunctionContext( scope.getParentScope( ) );
					}
					return ( IScriptFunctionContext )JavascriptEvalUtil.convertJavascriptValue(obj);
				}
				private Collator getCollator( Scriptable scope )
				{
					if ( scope == null )
						return null;
					Scriptable prototype = scope.getPrototype( );
					Object obj = org.mozilla.javascript.UniqueTag.NOT_FOUND;
					if( prototype != null )
						obj = prototype.get( "compare_locale", prototype );
					if ( obj == org.mozilla.javascript.UniqueTag.NOT_FOUND )
					{
						return getCollator( scope.getParentScope( ) );
					}
					return ( Collator )JavascriptEvalUtil.convertJavascriptValue(obj);
				}
			},0 );
		}
	}
	
	

	@Override
	public String getClassName( )
	{
		return this.category.getName( );
	}
	
	
}

@@ -2429,16 +2429,97 @@
 ntext = 
+scriptFunctionContext;%0A%09%09%09%09%09%09if ( scriptFunctionContext != null )%0A%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09
 new IScr
@@ -2538,25 +2538,25 @@
 ontext( ) %7B%0A
-%0A
+%09
 %09%09%09%09%09%09%09publi
@@ -2588,24 +2588,25 @@
 ring name )%0A
+%09
 %09%09%09%09%09%09%09%7B%0A%09%09%09
@@ -2610,18 +2610,20 @@
 %09%09%09%09%09%09%09%09
+%09
 if
+ 
 ( %22compa
@@ -2649,17 +2649,32 @@
 name ) )
-%0A
+ //$NON-NLS-1$%0A%09
 %09%09%09%09%09%09%09%09
@@ -2696,74 +2696,8 @@
 r;%0A%09
-%09%09%09%09%09%09%09if ( scriptFunctionContext == null )%0A%09%09%09%09%09%09%09%09%09return null;%0A
 %09%09%09%09
@@ -2754,26 +2754,28 @@
 e );%0A%09%09%09%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09%09%09%7D;%0A%09%09%09
@@ -2771,16 +2771,24 @@
 %09%09%09%09%09%7D;%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09re

Fix for Bug 456847 - BirtDateTime function in chart's onRender function causes render failure