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

import org.eclipse.birt.data.engine.api.DataEngineContext;
import org.eclipse.birt.data.engine.api.IBaseQueryDefinition;
import org.eclipse.birt.data.engine.core.DataException;

/**
 * Provide the sevice to new instance of RDSave and RDLoad
 */
public final class RDUtil
{
	/**
	 * @param context
	 * @param queryDefn
	 * @param rowCount
	 * @param queryResultInfo
	 * @return
	 * @throws DataException
	 */
	public static IRDSave newSave( DataEngineContext context,
			IBaseQueryDefinition queryDefn, int rowCount,
			QueryResultInfo queryResultInfo ) throws DataException
	{
		QueryResultInfo newQueryResultInfo = getRealQueryResultInfo( queryResultInfo );

		if ( context.getMode( ) == DataEngineContext.MODE_GENERATION )
			return new RDSave( context, queryDefn, rowCount, newQueryResultInfo );
		else if ( context.getMode( ) == DataEngineContext.MODE_UPDATE )
			return new RDSave2( context,
					queryDefn,
					rowCount,
					newQueryResultInfo );
		else
			assert false;

		return null;
	}

	/**
	 * @param queryResultInfo
	 * @return
	 */
	private static QueryResultInfo getRealQueryResultInfo(
			QueryResultInfo queryResultInfo )
	{
		String rootQueryResultID = null;
		String selfQueryResultID = null;

		selfQueryResultID = QueryResultIDUtil.get2PartID( queryResultInfo.getQueryResultID( ) );
		if ( selfQueryResultID == null )
			selfQueryResultID = queryResultInfo.getQueryResultID( );
		else
			rootQueryResultID = QueryResultIDUtil.get1PartID( queryResultInfo.getQueryResultID( ) );

		return new QueryResultInfo( rootQueryResultID,
				null,
				selfQueryResultID,
				queryResultInfo.getSubQueryName( ),
				queryResultInfo.getIndex( ) );
	}
	
	/**
	 * @param context
	 * @param queryResultInfo
	 * @return
	 * @throws DataException
	 */
	public static RDLoad newLoad( DataEngineContext context,
			QueryResultInfo queryResultInfo ) throws DataException
	{
		return new RDLoad( context, queryResultInfo );
	}

}

@@ -1302,16 +1302,20 @@
 ATION )%0A
+%09%09%7B%0A
 %09%09%09retur
@@ -1376,24 +1376,28 @@
 sultInfo );%0A
+%09%09%7D%0A
 %09%09else if ( 
@@ -1450,16 +1450,183 @@
 PDATE )%0A
+%09%09%7B%0A%09%09%09if ( newQueryResultInfo.getRootQueryResultID( ) == null )%0A%09%09%09%09return new RDSave( context,%0A%09%09%09%09%09%09queryDefn,%0A%09%09%09%09%09%09rowCount,%0A%09%09%09%09%09%09newQueryResultInfo );%0A%09%09%09else%0A%09
 %09%09%09retur
@@ -1650,24 +1650,25 @@
 ntext,%0A%09%09%09%09%09
+%09
 queryDefn,%0A%09
@@ -1662,24 +1662,25 @@
 %09queryDefn,%0A
+%09
 %09%09%09%09%09rowCoun
@@ -1683,24 +1683,25 @@
 Count,%0A%09%09%09%09%09
+%09
 newQueryResu
@@ -1706,24 +1706,28 @@
 sultInfo );%0A
+%09%09%7D%0A
 %09%09else%0A%09%09%09as

Support regroup when query is running on report document