package kr.co.kihyun.beans.entity;

import java.sql.Timestamp;

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.PrimaryKey;

import kr.co.kihyun.moumi.MoumiConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class TotLog extends MoumiEntity<Long> {
	/**
	 * 
	 */
        private static final Logger LOG = LoggerFactory.getLogger(TotLog.class);    
	private static final long serialVersionUID = 7530592096523169112L;

	@PrimaryKey
	protected Long num;

	@Persistent
	private Action act;

	@Persistent
	private String des;

	@Persistent
	private String mUser; // COLUMN: user_id

	@Persistent
	private String groupId;

	@Persistent
	private String clientIp;

	@Persistent
	private TotDoc totDoc; // COLUMN: doc_id

	@Persistent
	private String csUri;

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

	@Persistent
	private String mDept; // COLUMN: dept_id

	@Persistent
	private String logLevel;

	@Persistent
	private String message;

	@Persistent
	private String location;

	public TotLog() {
	}

	public Long getId() {
		return this.num;
	}

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

	public static enum Action {
		LOGOUT(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.logout")), LOGIN(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.login")), REGIST_DOC(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc") + MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.registrition")), MODIFY_INFO(
				MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.information")+MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.modify")), MODIFY_FORM(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.input")+MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.formDoc")+MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.modify")), DELETE_REPORT(MoumiConfig.getMessageBundle().getString(
				"moumi.message.tot_report.reportDoc")
				+ MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.delete")), DELETE_DOC(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.docList") + MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.delete")), RELAY_REGIST(
				MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.subDoc")+MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.registrition")), DUMMY0(""), DUMMY1(""), DUMMY2(""), DUMMY3(""), DUMMY4(""), DUMMY5(""), DUMMY6(""), DUMMY7(""), DUMMY8(
				""), DUMMY9(""), DUMMY10(""), DUMMY11(""), REASSIGN_REPORTER(MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.reportTrans")), SAVE_TEMP(MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.saveTemp")), MODIFY_REPORT(
				MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.reportDoc") + MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.modify")), SUBMIT_REPORT(MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.report")+MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.complete")), APPROVE_REPORT(
				MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.doc")+MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.recog")), DENY_REPORT(MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.reportDoc") + MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.giveBack"));

		private String msg;

		private Action(String actMsg) {
			this.msg = actMsg;
		}

      //v2. 5.static final 필드 변조 가능성 : Update by KWON,HAN 
      //private는 같은 클래스 내에서만 호출이 가능하므로 접근제한이 없는 public으로 하고 final을 사용하지 않는다.                   
//		public static final Action asAction(int actNum) {
		public static Action asAction(int actNum) {            
                    LOG.debug("v2. 5.static final 필드 변조 가능성 : TotLog.asAction() Not Test {}", values()[actNum]);                              
			return values()[actNum];
		}
      //==========================================================                  

		@Override
		public String toString() {
			return msg;
		}

	}

	public Action getAct() {
		return this.act;
	}

	public void setAct(Action a) {
		this.act = a;
	}

	public String getDes() {
		return this.des;
	}

	public void setDes(String des) {
		this.des = des;
	}

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

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

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

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

	public String getClientIp() {
		return this.clientIp;
	}

	public void setClientIp(String ip) {
		this.clientIp = ip;
	}

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

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

	public String getCsUri() {
		return this.csUri;
	}

	public void setCsUri(String uri) {
		this.csUri = uri;
	}

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

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

	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 String getLogLevel() {
		return this.logLevel;
	}

	public void setLogLevel(String logLevel) {
		this.logLevel = logLevel;
	}

	public String getMessage() {
		return this.message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getLocation() {
		return this.location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public TotLog(Action act, String des, MUser mUser, String groupId, String clientIp, TotDoc totDoc, String csUri) {
		this.setAct(act);
		this.setDes(des);
		this.setUser(mUser);
		this.setGroupId(groupId);
		this.setClientIp(clientIp);
		this.setTotDoc(totDoc);
		this.setCsUri(csUri);
	}
}
