package com.k_int.aggr2.mimsy.data.hdo;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.annotations.Cascade;

import com.k_int.mimsy.ref.MimsyDataTypeEnum;

@Entity
@Table(name="SCHEMA_DEFINITION")
public class SchemaDefinitionHDO 
{
	private Long id;
	private String title;
	private String description;
	private MimsyDataTypeEnum applicable_mimsy_data_type;
	private List<SchemaElementHDO> schema_elements = new ArrayList<SchemaElementHDO>(); /* one to many */
	
	protected SchemaDefinitionHDO(){;}
	
	protected SchemaDefinitionHDO(String title)
	{
		this.title = title;
	}

	@Id
	@Column(name="ID")
	@GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId()
	{ 
		return id;                          
	} 
	
	public void setId(Long id)
	{ 
		this.id = id;
	}

	@Column(name="TITLE")
	public String getTitle() 
	{
		return title;
	}

	public void setTitle(String title) 
	{
		this.title = title;
	}

	@Column(name="DESCRIPTION", length=1000)
	@Lob
	public String getDescription() 
	{
		return description;
	}

	public void setDescription(String description) 
	{
		this.description = description;
	}

	@Column(name="MIMSY_DATA_TYPE")
	public MimsyDataTypeEnum getApplicableMimsyDataType() 
	{
		return applicable_mimsy_data_type;
	}

	public void setApplicableMimsyDataType(MimsyDataTypeEnum applicable_mimsy_data_type) 
	{
		this.applicable_mimsy_data_type = applicable_mimsy_data_type;
	}
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	@JoinTable(name="SCHEMA_DEFINITION_ELEMENT_LINK",
	         joinColumns=@JoinColumn(name="SCHEMA_DEFINITION_FK", referencedColumnName="ID"),
	         inverseJoinColumns= @JoinColumn(name="SCHEMA_ELEMENT_FK", referencedColumnName="ID"))  
	@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
	public List<SchemaElementHDO> getSchemaElements() 
	{
		return schema_elements;
	}

	public void setSchemaElements(List<SchemaElementHDO> schema_elements) 
	{
		this.schema_elements = schema_elements;
	}
	
	//LOOKUP BY ID
	public static SchemaDefinitionHDO lookupOrCreate(Session session, Long id)
	{
		SchemaDefinitionHDO retval = SchemaDefinitionHDO.lookupById(session, id);
		
		if (retval == null) 
		{
			try
			{
				retval = new SchemaDefinitionHDO(); 
				session.save(retval);
				session.flush();				 
			}
			catch(Exception e)
			{
		 
			}			 
		}
		return retval;
	}
	
	//LOOKUP BY TITLE
	public static SchemaDefinitionHDO lookupOrCreate(Session session, String title)
	{
		SchemaDefinitionHDO retval = SchemaDefinitionHDO.lookupByTitle(session, title);
		
		if (retval == null) 
		{
			try
			{
				retval = new SchemaDefinitionHDO(title); 
				session.save(retval);
				session.flush();				 
			}
			catch(Exception e)
			{
		 
			}			 
		}
		return retval;
	}
	
	public static SchemaDefinitionHDO lookupById(Session session, Long id)
	{		
		if(id == null)
		{
			return null;
		}
			
		try 
		{
			Query q = session.createQuery("Select x from com.k_int.aggr2.mimsy.data.hdo.SchemaDefinitionHDO x where x.id = ?");
			q.setLong(0, id);
			
			return (SchemaDefinitionHDO) q.uniqueResult();
		}
		catch (HibernateException he) 
		{
			throw new HibernateException(he);
		}    
	}
	
	public static SchemaDefinitionHDO lookupByTitle(Session session, String title)
	{		
		if(title == null)
		{
			return null;
		}
			
		try 
		{
			Query q = session.createQuery("Select x from com.k_int.aggr2.mimsy.data.hdo.SchemaDefinitionHDO x where x.title = ?");
			q.setParameter(0, title);
			
			return (SchemaDefinitionHDO) q.uniqueResult();
		}
		catch (HibernateException he) 
		{
			throw new HibernateException(he);
		}    
	}
}
