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

import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.odi.IResultIterator;
import org.eclipse.birt.data.engine.odi.IResultObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

/**
 * 
 */
public class JSIVRowObject extends ScriptableObject
{
	// bound result iterator
	private IResultIterator resultIterator;
	private IResultObject resultObject;
	
	/** */
	private static final long serialVersionUID = 7349038479602826317L;
	
	/**
	 * @param resultIterator
	 */
	public void setResultSetIterator( IResultIterator resultIterator )
	{
		this.resultIterator = resultIterator;
		this.resultObject = null;
	}

	/**
	 * @param resultIterator
	 */
	public void setResultObject( IResultObject resultObject )
	{
		this.resultObject = resultObject;
		this.resultIterator = null;
	}
	
	/*
	 * @see org.mozilla.javascript.ScriptableObject#getClassName()
	 */
	public String getClassName( )
	{
		return "row";
	}
	
	/*
	 * @see org.mozilla.javascript.ScriptableObject#get(java.lang.String,
	 *      org.mozilla.javascript.Scriptable)
	 */
	public Object get( String name, Scriptable start )
	{
		try
		{
			if ( resultObject != null )
				return resultObject.getFieldValue( name );
			
			return resultIterator.getCurrentResult( ).getFieldValue( name );
		}
		catch ( DataException e )
		{
			return null;
		}
	}

}
