package info.bukova.isspst.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Member implements DataModel {

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	private int id;
	@Column(name = "CREATED")
	private Date created;
	@Column(name = "MODIFIED")
	private Date modified;
	@ManyToMany
	@LazyCollection(LazyCollectionOption.TRUE)
	@JoinTable(name="MEMBER_PARENT", joinColumns={@JoinColumn(name="MEMBER_ID")}, inverseJoinColumns={@JoinColumn(name="PARENT_ID")})
	private List<Workgroup> parents;
	@Transient
	private boolean valid;
	
	public abstract String getFullName();
	public abstract boolean isPerson();

	public int getId() {
		return id;
	}

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

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public Date getModified() {
		return modified;
	}

	public void setModified(Date modified) {
		this.modified = modified;
	}

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	public List<Workgroup> getParents() {
		return parents;
	}

	public void setParents(List<Workgroup> parents) {
		this.parents = parents;
	}
	
	public void addParent(Workgroup parent) {
		if (parents == null) {
			parents = new ArrayList<Workgroup>();
		}
		if (!parents.contains(parent)) {
			parents.add(parent);
		}
	}
	
	public void removeParent(Workgroup parent) {
		if (parents == null) {
			return;
		}
		
		parents.remove(parent);
	}

}
