package mobi.droidcloud.mgmt.model;

import static javax.persistence.FetchType.LAZY;
import static javax.persistence.GenerationType.IDENTITY;

import java.util.Collection;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.isomorphic.jpa.EMF;

@Entity
@Table(name = "T_DOMAIN")
public class Domain {

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(nullable = false)
	private int id;
	@Basic
	private String name;
	@OneToMany(mappedBy = "domain")
	private Collection<DomainManaged> domainManaged;

	//@Column(name = "parentDomainId", nullable=true)
	
	@Transient
	private Integer parentDomainId;

	@ManyToOne(fetch = LAZY)
	private Domain parentDomain;
	
	@OneToMany(mappedBy = "parentDomain", fetch = LAZY)
	private Collection<Domain> subDomains;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setName(String param) {
		this.name = param;
	}

	public String getName() {
		return name;
	}

	public Collection<DomainManaged> getDomainManaged() {
	    return domainManaged;
	}

	public void setDomainManaged(Collection<DomainManaged> param) {
	    this.domainManaged = param;
	}

	public Domain getParentDomain() {
		System.out.println("----------------- getParentDomain -----------------");
	    return parentDomain;
	}

	public void setParentDomain(Domain param) {
		System.out.println("----------------- setParentDomain -----------------");
	    this.parentDomain = param;
	}

	public Integer getParentDomainId() {
		System.out.println("***************** getparentDomainId ********************");
		System.out.println("parentDomain is " + (this.getParentDomain() == null ? "null" : this.getParentDomain().id));
//		if (parentDomainId == null) {
//			parentDomainId = 0;
//		}
//		System.out.println("Returning "+parentDomainId);
//		return parentDomainId;
		if (this.getParentDomain() == null) {
			System.out.println("Returning parentDomainId 0");
			return 0;
		}
		System.out.println("Returning "+this.getParentDomain().id);
		return this.getParentDomain().id;
	}

	public void setParentDomainId(Integer parentDomainId) {
		System.out.println("***************** setparentDomainId() ********************");
//		if (parentDomainId == null) {
//			System.out.println("parentDomainId is null, resetting it to 0");
//			this.parentDomainId = 0;
//		} else {
//			this.parentDomainId = parentDomainId;
//		}
//		System.out.println("set parentDomainId to " + this.parentDomainId);

		if (parentDomainId == null || parentDomainId == 0) {
			this.setParentDomain(null);
			this.parentDomainId = 0;
			return;
		}
		System.out.println("Finding Entity "+parentDomainId);
		EntityManager em = EMF.getEntityManager();
		em.getTransaction().begin();
		Domain parentDomain = em.find(Domain.class, parentDomainId);
		this.setParentDomain(parentDomain);
		em.getTransaction().commit();
		em.close();
		System.out.println("Parent Entity updated/set for "+parentDomainId);
		if (this.getParentDomain() == null)
			this.parentDomainId = 0;
		else
			this.parentDomainId = this.getParentDomain().id;
		System.out.println("parentDomainId updated/set for "+parentDomainId);
	}

	public Collection<Domain> getSubDomains() {
	    return subDomains;
	}

	public void setSubDomains(Collection<Domain> param) {
	    this.subDomains = param;
	}

}