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

import org.eclipse.birt.core.data.DataType;
import org.eclipse.birt.data.aggregation.api.IBuildInAggregation;
import org.eclipse.birt.data.aggregation.i18n.Messages;
import org.eclipse.birt.data.engine.aggregation.RunningAccumulator;
import org.eclipse.birt.data.engine.api.aggregation.Accumulator;
import org.eclipse.birt.data.engine.api.aggregation.IParameterDefn;
import org.eclipse.birt.data.engine.core.DataException;

/**
 * Implements build-in running count function.
 */
public class TotalRunningCount extends AggrFunction
{

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.birt.data.engine.api.aggregation.IAggregation#getName()
	 */
	public String getName( )
	{
		return IBuildInAggregation.TOTAL_RUNNINGCOUNT_FUNC;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.birt.data.engine.api.aggregation.IAggregation#getType()
	 */
	public int getType( )
	{
		return RUNNING_AGGR;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.birt.data.engine.api.aggregation.IAggregation#getDateType()
	 */
	public int getDataType( )
	{
		return DataType.INTEGER_TYPE;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.birt.data.engine.api.aggregation.IAggregation#getParameterDefn()
	 */
	public IParameterDefn[] getParameterDefn( )
	{
		return new IParameterDefn[]{};
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.birt.data.engine.api.aggregation.IAggregation#newAccumulator()
	 */
	public Accumulator newAccumulator( )
	{
		return new MyAccumulator( );
	}

	private class MyAccumulator extends RunningAccumulator
	{

		private int count;

		/*
		 * (non-Javadoc)
		 * @see org.eclipse.birt.data.engine.api.aggregation.Accumulator#start()
		 */
		public void start( ) throws DataException
		{
			count = 0;
		}

		/*
		 * (non-Javadoc)
		 * @see org.eclipse.birt.data.engine.api.aggregation.Accumulator#onRow(java.lang.Object[])
		 */
		public void onRow( Object[] args ) throws DataException
		{
			count++;
		}

		/*
		 * (non-Javadoc)
		 * @see org.eclipse.birt.data.engine.api.aggregation.Accumulator#getValue()
		 */
		public Object getValue( ) throws DataException
		{
			return new Integer( count );
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.birt.data.engine.api.aggregation.IAggrFunction#getDescription()
	 */
	public String getDescription( )
	{
		return Messages.getString( "TotalRunningCount.description" ); //$NON-NLS-1$
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.birt.data.engine.api.aggregation.IAggrFunction#getDisplayName()
	 */
	public String getDisplayName( )
	{
		return Messages.getString( "TotalRunningCount.displayName" ); //$NON-NLS-1$
	}
	
}

@@ -1800,38 +1800,235 @@
 %0A%09%7B%0A
-%09%09return new IParameterDefn%5B%5D%7B
+    %09// one parameter definition%0A%09%09return new IParameterDefn%5B%5D%7B%0A%09%09%09new ParameterDefn( Constants.EXPRESSION_NAME,%0A%09%09%09%09%09Constants.EXPRESSION_DISPLAY_NAME,%0A%09%09%09%09%09true,%0A%09%09%09%09%09true,%0A%09%09%09%09%09SupportedDataTypes.INTEGER_DOUBLE,%0A%09%09%09%09%09%22%22 )%0A%09%09
 %7D;%0A%09
@@ -2297,16 +2297,48 @@
 t count;
+%0A%09%09boolean countByColumn = true;
 %0A%0A%09%09/*%0A%09
@@ -2692,19 +2692,246 @@
 %09%09%7B%0A
-%09%09%09count++;
+        %09if ( !countByColumn %7C%7C args == null %7C%7C args.length == 0 )%0A%09%09%09%7B%0A        %09%09if( countByColumn )%0A%09%09%09%09%7B%0A        %09%09%09countByColumn = false;%0A%09%09%09%09%7D%0A%09%09%09%09++count;%0A%09%09%09%7D%0A%09%09%09else if ( args.length %3E 0 && args%5B0%5D != null )%0A%09%09%09%7B%0A%09%09%09%09++count;%0A%09%09%09%7D
 %0A%09%09%7D

Fix Bugzilla Bug 226255 RunningCount should also have data field argument