package com.k_int.aggr2.mimsy.data.hdo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

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 javax.persistence.Transient;

import org.hibernate.Query;
import org.hibernate.Session;

import com.k_int.aggr2.mimsy.data.MimsyDataDTO;
import com.k_int.mimsy.ref.DataException;


@Entity
@Table(name="CIIM_PUBLISHED_DATA")
public class PublishedDataHDO 
{
	private Long id;
	private String mimsy_identifier;
	private byte[] bytes;
	
	protected PublishedDataHDO()
	{;}
	
	
	
	@Id
	@Column(name="ID")
	@GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId() 
	{
		return id;
	}
	
	protected void setId(Long id)
	{ 
		this.id = id;
	}
	
	@Column(name="IDENTIFIER", length=255,unique=true)
	public String getMimsyIdentifier()
	{
		return mimsy_identifier;
	}
	
	protected void setMimsyIdentifier(String mimsy_identifier)
	{
		this.mimsy_identifier=mimsy_identifier;
	}
	
	 @Lob
	 @Column(name = "DATA_ARRAY", length = Integer.MAX_VALUE - 1)
	 protected byte[] getBytes() { // not exposed
	        return bytes;
	 }

	 protected void setBytes(byte[] bytes )
			 { // not exposed
	        this.bytes = bytes;
	 }

	 @Transient
	 public MimsyDataDTO getPublishedData()throws DataException
	 {
		 try
		 {
			 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
	
			 ObjectInputStream ois = new ObjectInputStream(bais);
	
			 MimsyDataDTO retval = (MimsyDataDTO)ois.readObject();
			 return retval;
		 }
		 catch(Exception e)
		 {
			 throw new DataException(e);
		 }

	 }

	 public void setPublishedData(MimsyDataDTO data) throws DataException
	 {
		 try
		 {
			 ByteArrayOutputStream bos = new ByteArrayOutputStream();
			 ObjectOutputStream oos = new ObjectOutputStream(bos);
			 oos.writeObject(data);
			 oos.flush();
			 oos.close();
			 bos.close();
			 bytes = bos.toByteArray();
		 }
		 catch(Exception e)
		 {
			 throw new DataException(e);
		 }
	 }
	 
	 public static PublishedDataHDO lookup(Session sess, String mimsy_identifier)throws DataException
	 {
		 PublishedDataHDO retval=null;
		 try
		 {
			 Query q = sess.createQuery("select x from com.k_int.aggr2.mimsy.data.hdo.PublishedDataHDO x where x.mimsyIdentifier=:mimsyIdentifier");
			 q= q.setParameter("mimsyIdentifier", mimsy_identifier);		  
			 
			 retval = (PublishedDataHDO)q.uniqueResult();
			 return retval;
		 }
		 catch(Exception e)
		 {
			 throw new DataException(e);
		 }
	 }
	 
	 public static PublishedDataHDO lookupOrCreatePublishedData(Session sess,String mimsy_identifier)throws DataException
	 {
		 try
		 {
			 PublishedDataHDO retval=lookup(sess,mimsy_identifier);
			
			 if(retval==null)
			 {
				 retval = new PublishedDataHDO();
				 retval.setMimsyIdentifier(mimsy_identifier);
				 sess.save(retval);
				 sess.flush();
			 }
			 return retval;
		 }
		 catch(Exception e)
		 {
			 throw new DataException(e);
		 }
	 }

}
