/*******************************************************************************
 * Copyright (c) 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.report.engine.executor;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.InstanceID;
import org.eclipse.birt.report.engine.content.IContent;
import org.eclipse.birt.report.engine.data.dte.DocumentDataSource;
import org.eclipse.birt.report.engine.extension.IBaseResultSet;
import org.eclipse.birt.report.engine.extension.IReportItemExecutor;
import org.eclipse.birt.report.engine.ir.Report;
import org.eclipse.birt.report.engine.ir.ReportItemDesign;

public class ReportletItemExecutor extends ReportItemExecutor
{

	boolean hasNext = true;
	ReportletQuery reportletQuery;

	protected ReportletItemExecutor( ExecutorManager manager )
	{
		super( manager, ExecutorManager.REPORTLETITEM );
		DocumentDataSource ds = context.getDataSource( );
		InstanceID instanceID2 = null;
		if ( ds != null )
		{
			instanceID2 = ds.getInstanceID( );
		}
		reportletQuery = new ReportletQuery( context, instanceID2 );
	}

	public void close( )
	{
		try
		{
			reportletQuery.closeReportletQueries( );
		}
		catch ( EngineException ex )
		{
			context.addException( ex );
		}
		super.close( );
	}

	public IBaseResultSet[] getQueryResults( )
	{
		return reportletQuery.getQueryResults( );
	}
	
	public IContent execute( )
	{
		try
		{
			reportletQuery.openReportletQueries( );
		}
		catch ( BirtException ex )
		{
			context.addException( ex );
		}
		return null;
	}

	public boolean hasNextChild( )
	{
		return hasNext;
	}

	public IReportItemExecutor getNextChild( )
	{
		if ( hasNext )
		{
			hasNext = false;
			DocumentDataSource ds = context.getDataSource( );
			long designId = ds.getElementID( );
			Report report = context.getReport( );
			ReportItemDesign design = (ReportItemDesign) report
					.getReportItemByID( designId );

			return manager.createExecutor( this, design );
		}
		return null;
	}
}
