package kr.co.kihyun.beans.entity;

import java.util.Date;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.annotations.*;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class UserHistory extends LongEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2372294792945647578L;

	@Persistent(nullValue = NullValue.EXCEPTION)
	private TotDoc totDoc;

	@Persistent
	private String fromUser; // COLUMN: user_id

	@Persistent
	private String toUser; // COLUMN: change_id

	@Persistent
	private String mDept;

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

	public UserHistory() {
	}

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

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

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

	public void setFromUser(MUser fromUser) {
		if (fromUser == null)
			this.fromUser = null;
		else
			this.fromUser = fromUser.getId();
	}

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

	public void setToUser(MUser toUser) {
		if (toUser == null)
			this.toUser = null;
		else
			this.toUser = toUser.getId();
	}

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

	public void setDept(MDept mDept) {
		if (mDept == null)
			this.mDept = null;
		else
			this.mDept = mDept.getId();
	}

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

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

	public UserHistory(TotDoc totDoc, MUser fromUser, MUser toUser, MDept toUserDept) {
		this.setTotDoc(totDoc);
		this.setFromUser(fromUser);
		this.setToUser(toUser);
		this.setDept(toUserDept);
		this.setWriteDate(new Date(System.currentTimeMillis()));
	}

}
