package com.k_int.aggr2.mimsy.data.hdo;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="MIMSY_AGENT_REFERENCE")
public class AgentReferenceHDO 
{
	private static final long serialVersionUID = 1L;
	private Long id;
	private MimsyAgentDataHDO ref;
	private String relationship;
	private Integer position;
	private MimsyObjectDataHDO owner;
	
	protected AgentReferenceHDO()
	{
		super();
	}
	
	public AgentReferenceHDO(MimsyAgentDataHDO ref, String relationship)
	{
		this.ref=ref;
		this.relationship=relationship;
	}
	
	public void setOwner(MimsyObjectDataHDO owner)
	{
		this.owner=owner;
	}
	
	@ManyToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="OWNING_OBJECT_FK")
	public MimsyObjectDataHDO getOwner()
	{
		return owner;
	}
	
	@Id
	@Column(name="ID")
	@GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId()
	{
		return id;
	}
	
	protected void setId(Long id)
	{
		this.id = id;
	}
	
	@Transient
	public String getUrn()
	{
		return "urn://mol/"+ref.getMimsyIdentifier();
	}
	
	
	  @ManyToOne( cascade = CascadeType.ALL )
	   @JoinTable(name="MIMSY_AGENT_REF_LINK",
	         joinColumns= @JoinColumn(name="MIMSY_AGENT_FK"),
	         inverseJoinColumns= @JoinColumn(name="REF_ID"))
	public MimsyAgentDataHDO getReference()
	{
		return ref;
	}
	
	protected void setReference(MimsyAgentDataHDO ref)
	{
		this.ref=ref;
	}
	
				
	public void setRelationship(String relationship)
	{
		this.relationship=relationship;
	}	

	@Column(name="RELATIONSHIP")
	public String getRelationship()
	{
		return relationship;
	}
	
	@Column(name="POSITION")
	public Integer getPosition()
	{
		return position;
	}
	
	public void setPosition(Integer position)
	{
		this.position=position;
	}
	
	@Transient
	public int hashCode()
	{
		return this.toString().hashCode();
	}
	
	 public boolean equals(Object o)
	  {
		  boolean retval=false;
		  if(o instanceof AgentReferenceHDO)
		  {
			  retval = (o.toString()).equals(this.toString());
		  }
			  
		  return retval;  
	  }
	
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		if(ref!=null)
			builder.append(ref.getMimsyIdentifier());
		
		if(relationship!=null)
			builder.append(relationship);
		
		return builder.toString();
		
	}
}
