/*******************************************************************************
 * 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;

import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.impl.document.viewing.ExprMetaUtil;
import org.eclipse.birt.data.engine.odi.IResultIterator;

/**
 * 
 */
public class RowIDUtil
{
	private final static int MODE_NORMAL = 1;
	private final static int MODE_IV = 2;

	private int mode = -1;
	private int rowIDPos;
	
	/**
	 * @param ri
	 * @return
	 * @throws DataException 
	 */
	public int getRowID( IResultIterator ri ) throws DataException
	{
		if ( mode == -1 )
			init( ri );

		if ( mode == MODE_NORMAL )
			return ri.getCurrentResultIndex( );
		else
			return ( (Integer) ri.getCurrentResult( ).getFieldValue( rowIDPos ) ).intValue( );
	}
	
	/**
	 * @throws DataException
	 */
	private void init( IResultIterator ri ) throws DataException
	{
		rowIDPos = ri.getResultClass( ).getFieldCount( );
		if ( rowIDPos > 0
				&& ri.getResultClass( )
						.getFieldName( rowIDPos )
						.equals( ExprMetaUtil.POS_NAME ) )
		{
			mode = MODE_IV;
		}
		else
		{
			mode = MODE_NORMAL;
		}
	}
	
}

@@ -756,16 +756,86 @@
 or;%0A
-%0A/**%0A * 
+import org.eclipse.birt.data.engine.odi.IResultObject;%0A%0A/**%0A * Util for row ID
 %0A */
@@ -1187,16 +1187,20 @@
 ORMAL )%0A
+%09%09%7B%0A
 %09%09%09retur
@@ -1230,16 +1230,20 @@
 dex( );%0A
+%09%09%7D%0A
 %09%09else%0A%09
@@ -1247,50 +1247,125 @@
 e%0A%09%09
-%09return ( (Integer) ri.getCurrentResult( )
+%7B%0A%09%09%09IResultObject ob = ri.getCurrentResult( );%0A%09%09%09if ( ob == null )%0A%09%09%09%09return -1;%0A%09%09%09else%0A%09%09%09%09return ( (Integer) ob
 .get
@@ -1402,16 +1402,20 @@
 lue( );%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A%09/*

Fix a bug for sub query when it is running on a report document