/*
 * Created on Feb 7, 2004
 */
package LumiSoft.Data;

/**
 * 
 */
public class DataTable 
{
	private DataSet              m_pDataSet     = null;
	private String               m_TableName    = "";
	private DataColumnCollection m_pColumns     = null;
	private DataRowCollection    m_pRows        = null;
	private DataView             m_pDefaultView = null;
	
	public DataTable()
	{
		this(new DataSet());
	}
	
	public DataTable(DataSet dataset)
	{
		m_pDataSet = dataset;
		m_pColumns = new DataColumnCollection(this);
		m_pRows    = new DataRowCollection(this);
	}
	
	
	/**
	 * Creates a new DataRow with the same schema as the table.
	 * @return
	 */
	public DataRow NewRow()
	{ 					
		return new DataRow(this,new Object[m_pColumns.getCount()]);
	}
	
	/**
	 * Clears the DataTable of all data.
	 *
	 */
	public void Clear()
	{
		m_pRows.Clear();
	}
	
	/**
	 * Clones the structure of the DataTable.
	 *
	 */
	public DataTable Clone()
	{
		DataTable dt = new DataTable();		
		for(DataColumn col : this.getColumns()){
			DataColumn colNew = dt.getColumns().Add(col.getColumnName(),col.getDataType());		
		}
		return dt;
	}
	
	/**
	 * Copies both the structure and data for this DataTable.
	 *
	 */
	public DataTable Copy()
	{
		DataTable dt = Clone();
		for(DataRow dr : dt.getRows()){
			dt.getRows().Add(dr.getItemArray());
		}
		return dt;
	}
	
	/**
	 * Gets a customized view of the table which may include a filtered view, or a cursor position.
	 * @return
	 */
	public DataView getDefaultView()
	{
		if(m_pDefaultView == null){		
			return new DataView(this);
		}
		else{
			return m_pDefaultView;
		}
	}	
	
	/**
	 * Gets table name.
	 * @return
	 */
	public String getTableName()
	{
		return m_TableName;
	}
	/**
	 * Sets table name.
	 * @param value
	 */
	public void setTableName(String value)
	{
		// TODO: Check if table with same name exists
		
		m_TableName = value;		
	}
	
	/**
	 * Gets the collection of columns that belong to this table.
	 * @return
	 */
	public DataColumnCollection getColumns()
	{
		return m_pColumns;
	}
	
	/**
	 * Gets the collection of rows that belong to this table.
	 * @return
	 */
	public DataRowCollection getRows()
	{
		return m_pRows;
	}
	
	/**
	 * Gets the DataSet that this table belongs to.
	 * @return
	 */
	public DataSet getDataSet()
	{
		return m_pDataSet;
	}
	
}
