1package computedColumn;

import java.io.IOException;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.DesignFileException;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.SlotHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;

public class CColumn 
{
	private PropertyHandle computedColumns = null;
	private SessionHandle sessionHandle;
	private ReportDesignHandle reportDesignHandle;
	private ElementFactory elementFactory;
	private DesignElementHandle designElementHandle;
	private OdaDataSourceHandle odaDataSourceHandle;
	private OdaDataSetHandle odaDataSetHandle;
	private EngineConfig engineConfig;
	
	private String reportFileName = null;
	private String dataSrc = null;
	private String dataSet = null;

	/*
	 * Constructor saves away report design file name, data source and data set info
	 * @param:
	 * fileName: Name of report design file
	 * dataSrc:  Name of data source
	 * dataSet:  Name of data set
	 */
	CColumn (String fileName, String dataSrc, String dataSet)
	{
		this.reportFileName = fileName;
		this.dataSrc = dataSrc;
		this.dataSet = dataSet;
	}
	
	/*
	 * Initializes the variables
	 */ 
	public void Initialize ()
	{
		// Setup config (assumes Eclipse installed in c:\eclipse
		engineConfig = new EngineConfig( );
		engineConfig.setEngineHome
		                ("C:\\eclipse\\plugins\\org.eclipse.birt.report.viewer_1.0.1\\birt");

		// Create a session handle. This is used to manage all open designs.
		// Your app need create the session only once.
		sessionHandle = DesignEngine.newSession(null);
		
		// Open a report design.
		try {
			reportDesignHandle = sessionHandle.openDesign(reportFileName);
		} catch (DesignFileException e) {
			e.printStackTrace();
		}
		
		// The element factory creates instances of the various BIRT elements.
		elementFactory = reportDesignHandle.getElementFactory();
		
		return;
	}
	
	/*
	 * Connects to specified data source and data set
	 */
	public void connectDS ()
	{
		String tmp;
		int ii;
		
		SlotHandle sloth = reportDesignHandle.getDataSources();
		java.util.Iterator iter = sloth.iterator();
			
		// Search for specified data source
		while (iter.hasNext() == true)
		{
			odaDataSourceHandle = (OdaDataSourceHandle) iter.next();
			tmp = odaDataSourceHandle.getName();
			if (tmp.compareTo(dataSrc) == 0)
				break;
		}
			
		// Search for specified data set
		sloth = reportDesignHandle.getDataSets();
		iter = sloth.iterator();
		while (iter.hasNext() == true)
		{
			odaDataSetHandle = (OdaDataSetHandle) iter.next();
			tmp = odaDataSetHandle.getName();
			if (tmp.compareTo(dataSet) == 0)
				break;
		}

		return;
	}
	
	/*
	 * @param
	 *  colName: Name of the computed column
	 *  colExpr: Expression for the computed column 
	 */
	public void addComputedColumn (String colName, String colExpr) 
	     throws IOException, SemanticException
	{
		// Create a computed column
		ComputedColumn newColumn = new ComputedColumn();
		newColumn.setName(colName);
		newColumn.setExpression(colExpr);

		computedColumns = 
			odaDataSetHandle.getPropertyHandle (DataSetHandle.COMPUTED_COLUMNS_PROP);
		computedColumns.addItem(newColumn);
		
		return;
	}

	/*
	 * Saves the changes to a report design file
	 */
	public void saveReport ()
	{
		// Save the design and close it.
		try {
			reportDesignHandle.saveAs(reportFileName);
		} catch (IOException e)	{
			e.printStackTrace();
		}
		
		reportDesignHandle.close ();
		
		return;
	}
}
