package com.k_int.generic.schema.hdo;

import com.k_int.generic.schema.ref.AllowedSchemaTypeEnum;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.k_int.generic.schema.ref.PropertyTypeEnum;
import java.util.List;

@Entity
@Table(name="SCHEMA_PROPERTY")
public class PropertyHDO implements Comparable<PropertyHDO>
{
	private Long id;
	private String mapping_key = null;
	private String name = null;
	private String description = null;
	private Integer property_type_ordinal = new Integer(PropertyTypeEnum.TEXT.ordinal());
        private Integer allowed_schema_type_ordinal = new Integer(AllowedSchemaTypeEnum.COMMON.ordinal());
	
	public PropertyHDO() {;}
	
	@Id
	@Column(name="ID")
	@GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId()
	{ 
		return id;                          
	} 
	
	protected void setId(Long id)
	{ 
		this.id = id;
	}
	
	@Column(name="MAPPING_KEY")
	public String getMappingKey() 
	{
		return mapping_key;
	}

	public void setMappingKey(String mapping_key) 
	{
		this.mapping_key = mapping_key;
	}
	
	@Column(name="NAME")
	public String getName() 
	{
		return name;
	}

	public void setName(String name) 
	{
		this.name = name;
	}
	
	@Column(name="DESCRIPTION", length=1000)
	@Lob
	public String getDescription() 
	{
		return description;
	}

	public void setDescription(String description) 
	{
		this.description = description;
	}

	@Column(name="PROPERTY_TYPE")
	public Integer getPropertyTypeOrdinal() 
	{
		return property_type_ordinal;
	}

	public void setPropertyTypeOrdinal(Integer property_type_ordinal) 
	{
		this.property_type_ordinal = property_type_ordinal;
	}
        
        @Column(name="ALLOWED_SCHEMA_TYPE")
        public Integer getAllowedSchemaTypeOrdinal()
        {
            return allowed_schema_type_ordinal;
        }
        
        public void setAllowedSchemaTypeOrdinal(Integer allowed_schema_type_ordinal) 
        {
            this.allowed_schema_type_ordinal = allowed_schema_type_ordinal;
        }
		
	public static PropertyHDO lookupOrCreate(Session session, Long id)
	{
		PropertyHDO retval = PropertyHDO.lookupById(session, id);
		
		if (retval == null) 
		{
			try
			{
				retval = new PropertyHDO(); 
				session.save(retval);
				session.flush();				 
			}
			catch (HibernateException he) 
			{
				throw new HibernateException(he);
			}    		 
		}
		return retval;
	}
	
	public static PropertyHDO lookupById(Session session, Long id)
	{		
		if(id == null)
		{
			return null;
		}
			
		try 
		{
			Query q = session.createQuery("Select x from com.k_int.generic.schema.hdo.PropertyHDO x where x.id = ?");
			q.setLong(0, id);
			
			return (PropertyHDO) q.uniqueResult();
		}
		catch (HibernateException he) 
		{
			throw new HibernateException(he);
		}    
	}
	
	public static PropertyHDO lookupByName(Session session, String name)
	{		
		if(name == null)
		{
			return null;
		}
			
		try 
		{
			Query q = session.createQuery("Select x from com.k_int.generic.schema.hdo.PropertyHDO x where x.name = ?");
			q.setParameter(0, name);
			
			return (PropertyHDO) q.uniqueResult();
		}
		catch (HibernateException he) 
		{
			throw new HibernateException(he);
		}    
	}
        
        public static List<PropertyHDO> lookupByAllowedSchemaType(Session session, Integer allowedSchemaTypeOrdinal) throws HibernateException {

            List<PropertyHDO> retval = null;
            
            if ( allowedSchemaTypeOrdinal != null ) {
            
                Query q = session.createQuery("select x from com.k_int.generic.schema.hdo.PropertyHDO x where x.allowedSchemaTypeOrdinal = ?");
                q.setParameter(0, allowedSchemaTypeOrdinal);

                retval = (List<PropertyHDO>)q.list();
            }
            
            return retval;
        }
	
	/* Comparator method used for sorting */
	public int compareTo(PropertyHDO o) 
	{
        return this.name.compareTo(o.name);
    }
}
