package kr.co.kihyun.beans.entity;

import java.util.Date;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.annotations.*;
import kr.co.kihyun.beans.entity.ecross.IRemotePersistable;

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

	@Persistent
	private String groupId;

	@Persistent
	// COLUMN: request_user_id
	private String requestUser;

	@Persistent
	// COLUMN: response_user_id
	private String responseUser;

	@Persistent
	private String contents;

	@Persistent
	private TotDoc totDoc;

	@Persistent
	private TotReport totReport;

	@Persistent
	// COLUMN: request_dept_id
	private String requestDept;

	@Persistent
	// COLUMN: response_dept_id
	private String responseDept;

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

	public Memo() {
	}

	public String getGroupId() {
		return this.groupId;
	}

	public void setGroupId(String gid) {
		this.groupId = gid;
	}

	public MUser getRequestUser(PersistenceManager pm) {
		try {
			return pm.getObjectById(MUser.class, this.requestUser);
		} catch (JDOObjectNotFoundException jonfe) {
			return null;
		}
	}

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

	public MUser getResponseUser(PersistenceManager pm) {
		try {
			return pm.getObjectById(MUser.class, this.responseUser);
		} catch (JDOObjectNotFoundException jonfe) {
			return null;
		}
	}

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

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

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

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

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

	public TotReport getTotReport() {
		return this.totReport;
	}

	public void setTotReport(TotReport totReport) {
		this.totReport = totReport;
	}

	public String getRequestDept() {
		return this.requestDept;
	}

	public MDept getRequestDept(PersistenceManager pm) {
		try {
			return pm.getObjectById(MDept.class, this.requestDept);
		} catch (JDOObjectNotFoundException jonfe) {
			return null;
		}
	}

	public void setRequestDept(MDept requestDept) {
		if (requestDept == null)
			this.requestDept = null;
		else
			this.requestDept = requestDept.getId();
	}

	public String getResponseDept() {
		return this.responseDept;
	}

	public MDept getResponseDept(PersistenceManager pm) {
		try {
			return pm.getObjectById(MDept.class, this.responseDept);
		} catch (JDOObjectNotFoundException jonfe) {
			return null;
		}
	}

	public void setResponseDept(MDept responseDept) {
		if (responseDept == null)
			this.responseDept = null;
		else
			this.responseDept = responseDept.getId();
	}

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

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

	public Memo(String gid, MUser requestUser, MUser responseUser, String contents, TotDoc totDoc, MDept requestDept,
			MDept responseDept) {
		this.setGroupId(gid);
		this.setRequestUser(requestUser);
		this.setResponseUser(responseUser);

		this.setContents(contents);
		this.setTotDoc(totDoc);

		this.setRequestDept(requestDept);
		this.setResponseDept(responseDept);
		this.setWriteDate(new Date(System.currentTimeMillis()));
	}

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