/*
 * IReportScriptlet.java
 *
 * Created on 26 settembre 2004, 16.25
 */

package it.businesslogic.ireport;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRScriptletException;

import java.util.*;
/**
 *
 * @author  Administrator
 */
public class IReportScriptlet extends net.sf.jasperreports.engine.JRAbstractScriptlet
{
        // Vector of things to collect...
        HashMap series = new java.util.HashMap();
        HashMap groupStarted = new java.util.HashMap();
        
	/**
	 *
	 */
	public void beforeReportInit() throws JRScriptletException
	{
	}


	/**
	 *
	 */
	public void afterReportInit() throws JRScriptletException
	{
               
        }


	/**
	 *
	 */
	public void beforePageInit() throws JRScriptletException
	{

	}


	/**
	 *
	 */
	public void afterPageInit() throws JRScriptletException
	{
	}


	/**
	 *
	 */
	public void beforeColumnInit() throws JRScriptletException
	{
	}


	/**
	 *
	 */
	public void afterColumnInit() throws JRScriptletException
	{
	}


	/**
	 *
	 */
	public void beforeGroupInit(String groupName) throws JRScriptletException
	{
            
	}


	/**
	 *
	 */
	public void afterGroupInit(String groupName) throws JRScriptletException
	{
           resetSeries(groupName);
        }


	/**
	 *
	 */
	public void beforeDetailEval() throws JRScriptletException
	{
	}


	/**
	 *
	 */
	public void afterDetailEval() throws JRScriptletException
	{
            processSeries();
	}
        
        protected void processSeries()
        {
            // Looking for serie_to_calc in variables...
            Set vars = variablesMap.keySet();
            Iterator iter = vars.iterator();
            while( iter.hasNext())
            {
                String key = (String)iter.next();
                if (key.startsWith("SERIE_"))
                {
                    Vector serie = (Vector)series.get(key);
                    if (serie == null)
                    {
                        serie = new Vector();
                        series.put(key,serie);
                    }
                    try {
                        serie.add( getVariableValue(key) );
                    } catch (Exception ex) {}
                }
            }
        }
        
        protected void resetSeries(String group)
        {
            // Looking for serie_to_calc in variables...
            Set vars = variablesMap.keySet();
            Iterator iter = vars.iterator();
            while( iter.hasNext())
            {
                String key = (String)iter.next();
                if (key.startsWith("SERIE_") && key.indexOf("G_" + group)> 0)
                {
                    series.remove(key);
                }
            }
        }
        
        public String getSerieAsString(String name)
        {
            Vector v =  (Vector)series.get(name);
            Enumeration enum_v = v.elements();
            String tot = "";
            while (enum_v.hasMoreElements())
            {
                String s = ""+enum_v.nextElement();
                tot += s + "\n";
            }
            
            return tot;
        }
        
        public Vector getSerie(String serieName)
        {
            
            Vector v = (Vector)series.get(serieName);
            if (v==null)
            {
                v = new Vector();
                series.put(serieName, v);
                
            }
            return v;
        }    
        
        
        public Boolean addValueToSerie(String serieName, Object value)
        {
            Vector v = getSerie(serieName);
            v.add( value );
            return new Boolean(false);
        }
        
        public Boolean resetSerie(String serieName)
        {
            series.remove(serieName);
            return new Boolean(false);
        }
}