package com.k_int.aggr2.mimsy.data.hdo;

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.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.k_int.mimsy.ref.SchemaElementTypeEnum;

@Entity
@Table(name="SCHEMA_ELEMENT")
public class SchemaElementHDO 
{
	private Long id;	
	private Integer minOccurs = 0;
	private Integer maxOccurs = 1;
	private Integer maxLength = Integer.MAX_VALUE;
	private String display_name;
	private String description;
	private SchemaElementTypeEnum schemaElementType = SchemaElementTypeEnum.UNKNOWN;
	private ElementDefinitionHDO definition; /* Many to one */
	
	protected SchemaElementHDO(){;}
	
	public SchemaElementHDO(ElementDefinitionHDO definition, String display_name)
	{
		this.display_name = display_name;
		this.definition = definition;
	}

	@Id
	@Column(name="ID")
	@GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId()
	{ 
		return id;                          
	} 
	
	public void setId(Long id)
	{ 
		this.id = id;
	}
	
	@Column(name="MIN_OCCURS")
	public Integer getMinOccurs() 
	{
		return minOccurs;
	}

	public void setMinOccurs(Integer minOccurs) 
	{
		this.minOccurs = minOccurs;
	}

	@Column(name="MAX_OCCURS")
	public Integer getMaxOccurs() 
	{
		return maxOccurs;
	}

	public void setMaxOccurs(Integer maxOccurs) 
	{
		this.maxOccurs = maxOccurs;
	}
	
	@Column(name="MAX_LENGTH")
	public Integer getMaxLength() 
	{
		return maxLength;
	}

	public void setMaxLength(Integer maxLength) 
	{
		this.maxLength = maxLength;
	}

	@Column(name="DISPLAY_NAME")
	public String getDisplayName() 
	{
		return display_name;
	}

	public void setDisplayName(String display_name) 
	{
		this.display_name = display_name;
	}
	
	@Column(name="DESCRIPTION", length=1000)
	@Lob
	public String getDescription() 
	{
		return description;
	}

	public void setDescription(String description) 
	{
		this.description = description;
	}

	@Column(name="SCHEMA_ELEMENT_TYPE")
	public SchemaElementTypeEnum getSchemaElementType() 
	{
		return schemaElementType;
	}

	public void setSchemaElementType(SchemaElementTypeEnum schemaElementType) 
	{
		this.schemaElementType = schemaElementType;
	}
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="ELEMENT_DEFINITION_FK")
	public ElementDefinitionHDO getDefinition()
	{
		return definition;
	}

	public void setDefinition(ElementDefinitionHDO definition) 
	{
		this.definition = definition;
	}
	
	//LOOKUP BY ID
	public static SchemaElementHDO lookupOrCreate(Session session, Long id, ElementDefinitionHDO definition)
	{
		SchemaElementHDO retval = SchemaElementHDO.lookupById(session, id);
		
		if (retval == null) 
		{
			try
			{
				retval = new SchemaElementHDO(); 
				retval.setDefinition(definition);
				session.save(retval);
				session.flush();				 
			}
			catch(Exception e)
			{
		 
			}			 
		}
		return retval;
	}
	
	public static SchemaElementHDO 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.SchemaElementHDO x where x.id = ?");
			q.setLong(0, id);
			
			return (SchemaElementHDO) q.uniqueResult();
		}
		catch (HibernateException he) 
		{
			throw new HibernateException(he);
		}    
	}
}
