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

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Defines a default action handler for HTML output format
 */
public class HTMLActionHandler implements IHTMLActionHandler
{

	/** logger */
	protected Logger log = Logger
			.getLogger( HTMLActionHandler.class.getName( ) );

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.engine.api2.IHTMLActionHandler#getURL(org.eclipse.birt.report.engine.api2.IAction,
	 *      java.lang.Object)
	 */
	public String getURL( IAction actionDefn, Object context )
	{
		if ( actionDefn == null )
		{
			return null;
		}
		switch ( actionDefn.getType( ) )
		{
			case IAction.ACTION_BOOKMARK :
				return "#" + actionDefn.getActionString( );
			case IAction.ACTION_HYPERLINK :
				return actionDefn.getActionString( );

			case IAction.ACTION_DRILLTHROUGH :
				return buildDrillAction( actionDefn, context );

		}
		assert false;
		return null;

	}

	/**
	 * builds URL for drillthrough action
	 * 
	 * @param action
	 *            instance of the IAction instance
	 * @param context
	 *            the context for building the action string
	 * @return a URL
	 */
	protected String buildDrillAction( IAction action, Object context )
	{
		String baseURL = null;
		if ( context != null && context instanceof HTMLRenderContext )
		{
			baseURL = ( (HTMLRenderContext) context ).getBaseURL( );
		}
		StringBuffer link = new StringBuffer( );

		String reportName = action.getReportName( );
		if ( reportName != null && !reportName.equals( "" ) )//$NON-NLS-1$
		{
			link.append( baseURL );

			appendReportDesignName( link, reportName );

			//add format
			String format = action.getFormat( );
			appendFormat( link, format );

			//Adds the parameters
			Map params = action.getParameterBindings( );
			if ( params != null )
			{
				Iterator paramsIte = params.entrySet( ).iterator( );
				while ( paramsIte.hasNext( ) )
				{
					Map.Entry entry = (Map.Entry) paramsIte.next( );
					String key = (String) entry.getKey( );
					Object valueObj = entry.getValue( );
					appendParamter( link, key, valueObj );
				}
			}
		}

		//The search rules are not supported yet.
		String bookmark = action.getBookmark( );
		appendBookmark( link, bookmark );

		return link.toString( );
	}

	protected void appendReportDesignName( StringBuffer buffer,
			String reportName )
	{
		buffer.append( "?__report=" ); //$NON-NLS-1$
		try
		{
			buffer.append( URLEncoder.encode( reportName, "UTF-8" ) ); //$NON-NLS-1$
		}
		catch ( UnsupportedEncodingException e1 )
		{
			//It should not happen. Does nothing
		}
	}

	protected void appendFormat( StringBuffer buffer, String format )
	{
		if ( format != null && format.length( ) > 0 )
		{
			buffer.append( "&__format=" + format );//$NON-NLS-1$
		}
	}

	protected void appendParamter( StringBuffer buffer, String key,
			Object valueObj )
	{
		if ( valueObj != null )
		{
			try
			{
				key = URLEncoder.encode( key, "UTF-8" );
				String value = valueObj.toString( );
				value = URLEncoder.encode( value, "UTF-8" );
				buffer.append( "&" );
				buffer.append( key );
				buffer.append( "=" );
				buffer.append( value );
			}
			catch ( UnsupportedEncodingException e )
			{
				//Does nothing
			}
		}
	}

	protected void appendBookmark( StringBuffer buffer, String bookmark )
	{
		if ( bookmark != null && bookmark.length( ) != 0 )
		{
			buffer.append( "#" );//$NON-NLS-1$
			buffer.append( bookmark );
		}
	}

}
@@ -1246,16 +1246,37 @@
 ll;%0A%09%09%7D%0A
+%09%09String url = null;%0A
 %09%09switch
@@ -1344,22 +1344,77 @@
 K :%0A%09%09%09%09
-return
+if ( actionDefn.getActionString( ) != null )%0A%09%09%09%09%7B%0A%09%09%09%09%09url =
  %22#%22 + a
@@ -1443,16 +1443,33 @@
 ing( );%0A
+%09%09%09%09%7D%0A%09%09%09%09break;%0A
 %09%09%09case 
@@ -1499,22 +1499,21 @@
 K :%0A%09%09%09%09
-return
+url =
  actionD
@@ -1532,24 +1532,34 @@
 nString( );%0A
+%09%09%09%09break;
 %0A%09%09%09case IAc
@@ -1589,22 +1589,21 @@
 H :%0A%09%09%09%09
-return
+url =
  buildDr
@@ -1636,23 +1636,44 @@
 text );%0A
-%0A%09%09%7D%0A
+%09%09%09%09break;%0A%09%09%09default :%0A%09
 %09%09
+%09
 assert f
@@ -1678,16 +1678,20 @@
  false;%0A
+%09%09%7D%0A
 %09%09return
@@ -1695,15 +1695,13 @@
 urn 
-nul
+ur
 l;%0A
-%0A
 %09%7D%0A%0A
@@ -2390,16 +2390,17 @@
 ;%0A%0A%09%09%09//
+ 
 add form
@@ -2481,16 +2481,17 @@
 ;%0A%0A%09%09%09//
+ 
 Adds the
@@ -2882,16 +2882,17 @@
 %09%7D%0A%0A%09%09//
+ 
 The sear
@@ -3311,16 +3311,17 @@
 %09%7B%0A%09%09%09//
+ 
 It shoul
@@ -3973,16 +3973,17 @@
 %7B%0A%09%09%09%09//
+ 
 Does not

handle empty bookmark