/*
 * Created on Feb 7, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package LumiSoft.Data;

import java.util.*;

/**
 * @author Ivar
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class DataTableCollection implements Iterable<DataTable>
{
	private DataSet              m_pDataSet = null;
	private ArrayList<DataTable> m_pTables  = null;
	
	protected DataTableCollection(DataSet ds)
	{
		m_pDataSet = ds;
		m_pTables  = new ArrayList<DataTable>();
	}
	
	
	/**
	 * Creates a DataTable object with the specified name and adds it to the collection.
	 * @param tableName
	 * @return
	 */
	public DataTable Add(String tableName) throws Exception
	{
		if(Contains(tableName)){
			throw new Exception("DataTable with specified name '" + tableName + "' already exists in collection.");
		}
		
		DataTable table = new DataTable(m_pDataSet);
		table.setTableName(tableName);
		m_pTables.add(table);
		
		return table;
	}
	
	/**
	 * Checks whether the collection contains a table with the specified name.
	 * @param tableName
	 * @return
	 */
	public boolean Contains(String tableName)	
	{
		for(int i=0;i<m_pTables.size();i++){
			DataTable dt = (DataTable)m_pTables.get(i);
			if(dt.getTableName().toLowerCase().equals(tableName.toLowerCase())){
				return true;
			}
		}
			
		return false;
	}
	
	
	/**
	 * Gets the total number of tables in a collection.
	 * @return
	 */
	public int getCount()
	{
		return m_pTables.size();
	}
	
	/**
	 * Gets the DataTable object at the specified index.
	 * @param index
	 * @return
	 */
	public DataTable get(int index)
	{
		return m_pTables.get(index);
	}
	/**
	 * Gets the DataTable object with the specified name.
	 * @param tableName
	 * @return
	 */
	public DataTable get(String tableName)
	{
		for(int i=0;i<m_pTables.size();i++){
			DataTable dt = (DataTable)m_pTables.get(i);
			if(dt.getTableName().toLowerCase().equals(tableName.toLowerCase())){
				return dt;
			}
		}
			
		return null;
	}
	
	
	/**
	 * Gets iterator.
	 */
	public Iterator<DataTable> iterator()
	{
		return m_pTables.iterator();
	}
	
}
