/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kr.co.kihyun.beans.entity.ecross;

import java.util.Date;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.NullValue;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import kr.co.kihyun.beans.entity.AcceptanceCode;
import kr.co.kihyun.beans.entity.AssignType;
import kr.co.kihyun.beans.entity.MDept;
import kr.co.kihyun.beans.entity.MUser;
import kr.co.kihyun.beans.entity.Recog;
import kr.co.kihyun.beans.entity.Repoadm;
import kr.co.kihyun.beans.entity.TotDoc;
import kr.co.kihyun.beans.entity.TotReport;
import kr.co.kihyun.beans.entity.TotReportProcess;
import kr.co.kihyun.beans.entity.TotReportType;
import kr.co.kihyun.beans.entity.util.MPersistenceManager;
import kr.co.kihyun.lang.MString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author bhs
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class ECrossTotReport extends ECross<TotReport> {
	private static final Logger LOG = LoggerFactory.getLogger(ECrossTotReport.class);
	@Persistent
	private String acceptanceCode;
	@Persistent
	private Integer appro = 0;
	@Persistent
	private AssignType assignType = AssignType.ASSIGN_TO_DEPT;
	@Persistent
	private Long currentRepoadm = null;
	@Persistent
	private String data;
	private transient String decName;
	@Persistent
	private Long id;
	private transient boolean isDecrypted;
	@Persistent(nullValue = NullValue.EXCEPTION)
	private String isDeleted = "N";
	@Persistent
	private String mDept;
	@Persistent
	private String mUser;
	@Persistent
	private String managementCode;
	@Persistent
	private String note;
	@Persistent
	private String num;
	@Persistent
	private String ownerEmail;
	@Persistent
	private String ownerName;
	@Persistent
	private String ownerPhone;
	@Persistent(nullValue = NullValue.EXCEPTION)
	private TotReportProcess process = TotReportProcess.ACCEPT;
	@Persistent
	private Recog recog = Recog.NOT_RECOG;
	@Persistent(customValueStrategy = "timestamp")
	private Date regDate;
	@Persistent
	private Long repTotDoc;
	@Persistent
	private Long slaveTotDoc;
	@Persistent
	private Date submitDate;
	@Persistent(nullValue = NullValue.EXCEPTION)
	private Integer tableNum = 1;
	@Persistent
	private String totData;
	@Persistent(nullValue = NullValue.EXCEPTION)
	private Long totDoc;
	@Persistent(nullValue = NullValue.EXCEPTION)
	private TotReportType type = TotReportType.REPORT;


	public ECrossTotReport(TotReport report) {
		super(report);
		this.setAcceptanceCode(report.getAcceptanceCode());
		this.setAppro(report.getAppro());
		this.setAssignType(report.getAssignType());
		this.setCurrentRepoadm(report.getCurrentRepoadm());
		this.setData(report.getData());
		this.setDeleted(report.isDeleted());
		this.setDept(report.getDept());
		this.setId(report.getId());
		this.setManagementCode(report.getManagementCode());
		this.setNote(report.getNote());
		this.setNum(report.getNum());
		this.setUser(null);
		this.setOwnerEmail(report.getOwnerEmail());
		this.setOwnerName(report.getOwnerName());
		this.setOwnerPhone(report.getOwnerPhone());
		this.setProcess(report.getProcess());
		this.setRecog(report.getRecog());
		this.setRegDate(report.getRegDate());
		this.setRepTotDoc(report.getRepTotDoc());
		this.setSlaveTotDoc(report.getSlaveTotDoc());
		this.setSubmitDate(report.getSubmitDate());
		this.setTableNum(report.getTableNum());
		this.setTotData(report.getTotData());
		this.setTotDoc(report.getTotDoc());
		this.setType(report.getType());

	}

	@Override
	protected TotReport setFieldValues(MPersistenceManager pm, TotReport totReport) {
		totReport.setId(id);
		totReport.setAcceptanceCode(MString.isNull(acceptanceCode) ? null : AcceptanceCode.valueOf(acceptanceCode));
		totReport.setAppro(appro);
		totReport.setAssignType(assignType);
		totReport.setCurrentRepoadm(currentRepoadm == null ? null : pm.getObjectById(Repoadm.class, currentRepoadm));
		totReport.setData(data);
		totReport.setDeleted(isDeleted);
		totReport.setDept(mDept == null ? null : pm.getObjectById(MDept.class, mDept));
		totReport.setNote(note);
		totReport.setNum(num);
		totReport.setProcess(process);
		LOG.debug("PROCESS: {}", process);
		totReport.setRecog(recog);
		totReport.setRegDate(regDate);
		totReport.setRepTotDoc(repTotDoc == null ? null : pm.getObjectById(TotDoc.class, repTotDoc));
		totReport.setSlaveTotDoc(slaveTotDoc == null ? null : pm.getObjectById(TotDoc.class, slaveTotDoc));
		totReport.setSubmitDate(submitDate);
		totReport.setTableNum(tableNum);
		totReport.setTotData(totData);
		totReport.setTotDoc(totDoc == null ? null : pm.getObjectById(TotDoc.class, totDoc));
		totReport.setType(type);
		totReport.setUser(null);
		totReport.setOwnerEmail(ownerEmail);
		totReport.setOwnerName(ownerName);
		totReport.setOwnerPhone(ownerPhone);

                totReport.setManagementCode(managementCode);//20160503 추가

		return totReport;
	}

	public final AcceptanceCode getAcceptanceCode() {
		try {
			if (acceptanceCode != null) {
				return AcceptanceCode.valueOf(acceptanceCode);
			} else {
				return new AcceptanceCode(acceptanceCode, null);
			}
		} catch (Exception e) {
			LOG.error("Unable to parse acceptance code for {}", acceptanceCode);
			return new AcceptanceCode(acceptanceCode, null);
		}
	}

	public int getAppro() {
		return this.appro;
	}

	public String getData() {
		return this.data;
	}

	private String getDecName() {
		return this.decName;
	}

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

	private String getEncName() {
		return this.ownerName;
	}

	/**
	 * @return the id
	 */ public Long getId() {
		return id;
	}

	public final String getManagementCode() {
		return managementCode;
	}

	public String getNote() {
		return this.note;
	}

	public String getNum() {
		return this.num;
	}

	/**
	 * @return the ownerEmail
	 */ public String getOwnerEmail() {
		return MString.checkNull(ownerEmail);
	}

	/**
	 * @return the ownerName
	 */ public String getOwnerName(PersistenceManager pm) {
		if (!this.isDecrypted) {
			Query q = pm.newQuery("javax.jdo.query.SQL", "SELECT sp_cm_dec_fu(?) FROM sysibm.sysdummy1");
			q.setUnique(true);
			this.setDecName((String) q.execute(this.getEncName()));
			this.isDecrypted = true;
		}
		return this.getDecName();
	}

	/**
	 * @return the ownerPhone
	 */ public String getOwnerPhone() {
		return MString.checkNull(ownerPhone);
	}

	public TotReportProcess getProcess() {
		return this.process;
	}

	public Recog getRecog() {
		return this.recog;
	}

	public Date getRegDate() {
		return this.regDate == null ? null : new Date(this.regDate.getTime());
	}

	public Date getSubmitDate() {
		return this.submitDate == null ? null : new Date(this.submitDate.getTime());
	}

	public int getTableNum() {
		return this.tableNum;
	}

	public String getTotData() {
		return this.totData;
	}

	public TotReportType getType() {
		return this.type;
	}

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

	public String isDeleted() {
		return this.isDeleted;
	}

	public final void setAcceptanceCode(AcceptanceCode acceptanceCode) {
		this.acceptanceCode = acceptanceCode == null ? null : acceptanceCode.toString();
	}

	public void setAppro(int a) {
		this.appro = a;
	}

	public void setAssignType(AssignType t) {
		this.assignType = t;
	}

	public void setCurrentRepoadm(Repoadm currentRepoadm) {
		this.currentRepoadm = currentRepoadm == null ? null : currentRepoadm.getId();
	}

	public void setData(String data) {
		this.data = data;
	}

	private void setDecName(String decName) {
		this.decName = decName;
	}

	public void setDeleted(String isDeleted) {
		this.isDeleted = isDeleted;
	}

	public final void setDept(String mDept) {
		this.mDept = mDept;
	}

	/**
	 * @param id
	 * the id to set
	 */ public void setId(Long id) {
		this.id = id;
	}

	public final void setManagementCode(String managementCode) {
		this.managementCode = managementCode;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public void setNum(String num) {
		this.num = num;
	}

	/**
	 * @param ownerEmail the ownerEmail to set
	 */ protected void setOwnerEmail(String ownerEmail) {
		this.ownerEmail = ownerEmail;
	}

	/**
	 * @param ownerName the ownerName to set
	 */ protected void setOwnerName(String ownerName) {
		this.ownerName = ownerName;
	}

	/**
	 * @param ownerPhone the ownerPhone to set
	 */ protected void setOwnerPhone(String ownerPhone) {
		this.ownerPhone = ownerPhone;
	}

	public void setProcess(TotReportProcess p) {
		if (p.ordinal() < TotReportProcess.COMP.ordinal()) {
			this.setSubmitDate(null);
		} else if (p == TotReportProcess.COMP) {
			this.setSubmitDate(new Date());
		}
		if (p == TotReportProcess.RETURN && this.slaveTotDoc != null) {
			this.process = TotReportProcess.TEMP;
		} else {
			this.process = p;
		}
	}

	public void setRecog(Recog r) {
		this.recog = r;
	}

	public void setRegDate(Date regDate) {
		this.regDate = regDate == null ? null : new Date(regDate.getTime());
	}

	public void setRepTotDoc(TotDoc doc) {
		this.repTotDoc = doc == null ? null : doc.getId();
	}

	public void setSlaveTotDoc(TotDoc doc) {
		this.slaveTotDoc = doc == null ? null : doc.getId();
	}

	public void setSubmitDate(Date submitDate) {
		this.submitDate = submitDate == null ? null : new Date(submitDate.getTime());
	}

	public void setTableNum(int num) {
		this.tableNum = num;
	}

	public void setTotData(String data) {
		this.totData = data;
	}

	public final void setTotDoc(TotDoc doc) {
		this.totDoc = doc == null ? null : doc.getId();
	}

	public void setType(TotReportType t) {
		this.type = t;
	}

	public void setUser(MUser mUser) {
		if (mUser == null) {
			this.mUser = null;
			setOwnerEmail(null);
			setOwnerName(null);
			setOwnerPhone(null);
		} else {
			this.mUser = mUser.getId();
			setOwnerEmail(mUser.getEmail());
			setOwnerName(mUser.getEncName());
			setOwnerPhone(mUser.getPhone());
		}
	}

}
