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

import org.eclipse.birt.data.engine.aggregation.BuiltInAggregationFactory;
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.Aggregation;
import org.eclipse.birt.data.engine.core.DataException;

/**
 * Implements the built-in Total.PercentSum aggregation
 */
public class TotalPercentSum extends Aggregation
{

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.birt.data.engine.aggregation.Aggregation#getName()
     */
    public String getName()
    {
        return BuiltInAggregationFactory.TOTAL_PERCENTSUM_FUNC;
    }

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

    /*
     * (non-Javadoc)
     * @see org.eclipse.birt.data.engine.api.aggregation.MultipassAggregation#getNumberOfPasses()
     */
	public int getNumberOfPasses( )
	{
		return 2;
	}
    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.birt.data.engine.aggregation.Aggregation#getParameterDefn()
     */
    public boolean[] getParameterDefn()
    {
        return new boolean[] { true };
    }

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

    private class MyAccumulator extends RunningAccumulator
    {
        private double sum = 0;
        private int passNo = 0;
        private Object value;
        public void start() throws DataException
        {
            super.start();
            passNo++;
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.birt.data.engine.aggregation.Accumulator#onRow(java.lang.Object[])
         */
        public void onRow(Object[] args) throws DataException
        {
            assert (args.length > 0);
            if( passNo == 1)
            {
            	if (args[0] != null)
                {
	              	Double d = RankAggregationUtil.getNumericValue( args[0] );
	            	if ( d != null )
					{
	            		sum = sum + d.doubleValue( );
					}
	    		}
            }else
            {
            	if (args[0] != null )
            	{	
	            	Double d = RankAggregationUtil.getNumericValue( args[0] );
	            	if( sum == 0 || d == null  )
	        			value = "";
	        		else
	        			value = new Double(d.doubleValue( )/sum);
            	}else
            		value = ""; 
            }	
        }

    	/*
         * (non-Javadoc)
         * 
         * @see org.eclipse.birt.data.engine.aggregation.SummaryAccumulator#getSummaryValue()
         */
        public Object getValue()
        {
        	return value;
        }
    }
}