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

import java.util.ArrayList;
import java.util.Map;

import org.eclipse.birt.core.data.ExpressionUtil;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.data.engine.api.IBaseQueryDefinition;
import org.eclipse.birt.data.engine.api.IBinding;
import org.eclipse.birt.data.engine.api.IQueryDefinition;
import org.eclipse.birt.data.engine.api.querydefn.Binding;
import org.eclipse.birt.data.engine.api.querydefn.QueryDefinition;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;
import org.eclipse.birt.data.engine.api.querydefn.SubqueryLocator;
import org.eclipse.birt.data.engine.core.DataException;

/**
 * 
 */

public class PreparedIVDataExtractionQuery extends PreparedIVQuerySourceQuery
{

	PreparedIVDataExtractionQuery( DataEngineImpl dataEngine,
			IQueryDefinition queryDefn, Map appContext, IQueryContextVisitor visitor ) throws DataException
	{
		super( dataEngine, queryDefn, appContext, visitor );
		// TODO Auto-generated constructor stub
	}

	protected void prepareQuery( ) throws DataException
	{
		try
		{
			IBinding[] bindings = null;
			if ( this.queryDefn.getSourceQuery( ) instanceof SubqueryLocator )
			{
				this.queryResults = engine.getQueryResults( getParentQueryResultsID( (SubqueryLocator) ( queryDefn.getSourceQuery( ) ) ) );
				IQueryDefinition queryDefinition = queryResults.getPreparedQuery( )
						.getReportQueryDefn( );
				if ( queryDefn.getSourceQuery( ) instanceof SubqueryLocator )
				{
					ArrayList<IBinding> bindingList = new ArrayList<IBinding>( );
					getSubQueryBindings( queryDefinition,
							( (SubqueryLocator) queryDefn.getSourceQuery( ) ).getName( ), bindingList );
					addQueryBindings( bindingList, queryDefinition.getBindings( ) );
					bindings = bindingList.toArray( new IBinding[0] );
				}
				else
				{
					bindings = (IBinding[]) ( queryDefinition.getBindings( )
							.values( ).toArray( new IBinding[0] ) );
				}
			}
			else
			{
				if( ( (IQueryDefinition) queryDefn.getSourceQuery( ) ).getQueryResultsID( ) == null )
				{
					newPreDataEnige( );
					this.queryResults = PreparedQueryUtil.newInstance( preDataEngine,
						(IQueryDefinition) queryDefn.getSourceQuery( ),
						this.appContext ).execute( null );
				}
				else
				{
					this.queryResults = PreparedQueryUtil.newInstance( engine,
							(IQueryDefinition) queryDefn.getSourceQuery( ),
							this.appContext ).execute( null );
				}

				if ( queryResults != null
						&& queryResults.getPreparedQuery( ) != null )
				{
					IQueryDefinition queryDefinition = queryResults.getPreparedQuery( )
							.getReportQueryDefn( );
					bindings = (IBinding[]) queryDefinition.getBindings( )
							.values( )
							.toArray( new IBinding[0] );
				}
				else
				{
					bindings = new IBinding[0];
				}
			}
			
			if ( !hasBinding )
			{
				for ( int i = 0; i < bindings.length; i++ )
				{
					IBinding binding = bindings[i];
					if ( !this.queryDefn.getBindings( ).containsKey( binding.getBindingName( ) ))
						this.queryDefn.addBinding( new Binding( binding.getBindingName( ),
							new ScriptExpression( ExpressionUtil.createJSDataSetRowExpression( binding.getBindingName( ) ),
										binding.getDataType( ) ) ) );
				}
			}
		}
		catch ( BirtException e )
		{
			throw DataException.wrap( e );
		}
	}

	/**
	 * 
	 * @param subqueryLocator
	 * @return
	 */
	private String getParentQueryResultsID( SubqueryLocator subqueryLocator )
	{
		IBaseQueryDefinition baseQueryDefinition = subqueryLocator.getParentQuery( );
		while ( !( baseQueryDefinition instanceof QueryDefinition ) )
		{
			baseQueryDefinition = baseQueryDefinition.getParentQuery( );
		}
		return ( (QueryDefinition) baseQueryDefinition ).getQueryResultsID( );
	}
}

@@ -962,75 +962,8 @@
 ng;%0A
-import org.eclipse.birt.data.engine.api.querydefn.QueryDefinition;%0A
 impo
@@ -1022,24 +1022,24 @@
 Expression;%0A
+
 import org.e
@@ -1244,16 +1244,91 @@
 uery%0A%7B%0A%0A
+%09private static final String DATA_STREAM_POST_FIX = %22TabularCursorData%22;%0A%09%0A
 %09Prepare
@@ -1761,126 +1761,555 @@
 %09%09%09%09
-this.queryResults = engine.getQueryResults( getParentQueryResultsID( (SubqueryLocator) ( queryDefn.getSourceQuery( ) )
+IQueryDefinition baseQueryDefn = getBaseQueryDefinition( (SubqueryLocator) ( queryDefn.getSourceQuery( ) ) );%0A%09%09%09%09// temp solution to get result from tabular cursor stream.%0A%09%09%09%09if ( engine.getContext( )%0A%09%09%09%09%09%09.getDocReader( )%0A%09%09%09%09%09%09.exists( baseQueryDefn.getQueryResultsID( )%0A%09%09%09%09%09%09%09%09+ %22/%22 + DATA_STREAM_POST_FIX ) )%0A%09%09%09%09%7B%0A%09%09%09%09%09this.queryResults = PreparedQueryUtil.newInstance( engine,%0A%09%09%09%09%09%09%09baseQueryDefn,%0A%09%09%09%09%09%09%09this.appContext ).execute( null );%0A%09%09%09%09%7D%0A%09%09%09%09else%0A%09%09%09%09%09this.queryResults = engine.getQueryResults( baseQueryDefn.getQueryResultsID(
  ) )
@@ -4383,38 +4383,47 @@
 ate 
-String getParentQueryResultsID
+IQueryDefinition getBaseQueryDefinition
 ( Su
@@ -4581,16 +4581,17 @@
 tanceof 
+I
 QueryDef
@@ -4686,16 +4686,17 @@
 turn ( (
+I
 QueryDef
@@ -4723,37 +4723,16 @@
 finition
- ).getQueryResultsID(
  );%0A%09%7D%0A%7D

Fix issue Failed to bring list of values to filter chart from detail table[65131]