/*
 * Created on Mar 1, 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.ArrayList;

/**
 * @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 DataRelationCollection 
{
	private ArrayList<DataRelation> m_pRelations = null;
	
	protected DataRelationCollection()
	{
		m_pRelations = new ArrayList<DataRelation>();
	}
	
	
	/**
	 * Creates a DataRelation with the specified name, and parent and child columns, and adds it to the collection.
	 * @param name
	 * @param parentColumn
	 * @param childColumn
	 * @return The created relation.
	 */
	public DataRelation Add(String name,DataColumn parentColumn,DataColumn childColumn)
	{
		DataRelation rel = new DataRelation(name,parentColumn,childColumn);
		m_pRelations.add(rel);
		
		return rel;
	}
	
	/**
	 * Clears the collection of any relations.
	 */
	public void Clear()
	{
		m_pRelations.clear();
	}
	
	/**
	 * Verifies whether a DataRelation with the given name (case insensitive) exists in the collection.
	 * @param relationName
	 * @return true, if a relation with the specified name exists; otherwise false.
	 */
	public boolean Conatains(String relationName)
	{
		for(int i=0;i<m_pRelations.size();i++){
			DataRelation rel = (DataRelation)m_pRelations.get(i);
			
			if(rel.getRelationName().equals(relationName)){
				return true;
			}
		}
		
		return false;
	}
	
	
	/**
	 * Gets the total number of elements in a collection.
	 * @return
	 */
	public int getCount()
	{
		return m_pRelations.size();
	}
	
	/**
	 * Gets the DataRelation object at the specified index.
	 * @param index
	 * @return
	 */
	public DataRelation get(int index)
	{
		return (DataRelation)m_pRelations.get(index);
	}
	/**
	 * Gets the DataRelation object specified by name.
	 * @param relationName
	 * @return
	 */
	public DataRelation get(String relationName)
	{
		for(int i=0;i<m_pRelations.size();i++){
			DataRelation relation = (DataRelation)m_pRelations.get(i);
			if(relation.getRelationName().equals(relationName)){
				return relation;
			}
		}
		
		return null;
	}
}
