package kr.co.kihyun.beans.entity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NullValue;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.Serialized;
import kr.co.kihyun.beans.entity.ecross.IRemotePersistable;
import kr.co.kihyun.io.IUploadable;
import kr.co.kihyun.lang.MString;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Board extends LongEntity implements IUploadable, IRemotePersistable {
	/**
	 *
	 */
	private static final long serialVersionUID = 8641238241124185801L;

	@Persistent
	private String mUser;

	@Persistent
	private String password;

	@Persistent(nullValue = NullValue.EXCEPTION)
	private String title;

	@Persistent
	private String contents;

	@Persistent(customValueStrategy = "timestamp", nullValue = NullValue.EXCEPTION)
	private Date writeDate;

	@Persistent
	@Serialized
	private Map<String, List<Byte>> attachments;

	@Persistent(nullValue = NullValue.EXCEPTION)
	private int visit = 0;

	@Persistent(nullValue = NullValue.EXCEPTION)
	private int step = 0;

	@Persistent(nullValue = NullValue.EXCEPTION)
	private int repLevel = 0;

	@Persistent(nullValue = NullValue.EXCEPTION)
	private Category category;

	@Persistent
	private TotDoc totDoc; // COLUMN: doc_id

	@Persistent
	private Board upperBoard;

	@Persistent(mappedBy = "upperBoard")
	private List<Board> childBoards = new ArrayList<Board>();

    @Persistent(nullValue = NullValue.EXCEPTION)
    private String popup_yn = "N";

	public Board() {

	}

	public Board(String title) {
		this.setTitle(title);
	}

	public MUser getUser(PersistenceManager pm) {
		try {
			return pm.getObjectById(MUser.class, mUser);
		} catch (JDOObjectNotFoundException jonfe) {
			return null;
		}
	}

	public void setUser(MUser mUser) {
		if (mUser == null)
			this.mUser = null;
		else
			this.mUser = mUser.getId();
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getTitle() {
		return this.title;
	}

	public final void setTitle(String title) {
		if (MString.isNull(title))
			title = " ";
		this.title = title;
	}

	public String getContents() {
		return this.contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public Date getWriteDate() {
		return this.writeDate;
	}

	public void setWriteDate(Date writeDate) {
		this.writeDate = writeDate;
	}

	public Map<String, List<Byte>> getAttachments() {
		if (this.attachments == null)
			return Collections.unmodifiableMap(new HashMap<String, List<Byte>>());
		else
			return Collections.unmodifiableMap(attachments);
	}

	public void addAttachment(String key, List<Byte> value) {
		if (this.attachments == null )
			this.attachments = new HashMap<String, List<Byte>>();
		this.attachments.put(key, value);
	}

	public List<Byte> removeAttachment(String key) {
//		return null;
		return this.attachments.remove(key);
	}

	public int getVisit() {
		return this.visit;
	}

	public void setVisit(int visit) {
		this.visit = visit;
	}

	public int getStep() {
		return this.step;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public int getRepLevel() {
		return this.repLevel;
	}

	public void setRepLevel(int level) {
		this.repLevel = level;
	}

	public Category getCategory() {
		return this.category;
	}

	public void setCategory(Category groupId) {
		this.category = groupId;
	}

	public TotDoc getTotDoc() {
		return this.totDoc;
	}

	public void setTotDoc(TotDoc doc) {
		this.totDoc = doc;
	}

	public Board getUpperBoard() {
		return this.upperBoard;
	}

	public void setUpperBoard(Board upperBoard) {
		this.upperBoard = upperBoard;
	}

	public void setChildBoards(List<Board> childBoards) {
		this.childBoards = childBoards;
	}

	public List<Board> getChildBoards() {
		return childBoards;
	}

	public MDept getOwnerParty(PersistenceManager pm) {
		return this.getTotDoc().getDept(pm).getRootDept(pm);
	}

    public void setPopupYn(String flag)
    {
        this.popup_yn = flag;
    }

    public String getPopupYn()
    {
        return popup_yn;
    }
}
