/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kr.co.kihyun.beans.entity.ecross;

import java.text.SimpleDateFormat;
import java.util.Calendar;
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.AcceptanceLimitStrategy;
import kr.co.kihyun.beans.entity.AccessAuth;
import kr.co.kihyun.beans.entity.ExecutionCode;
import kr.co.kihyun.beans.entity.MDept;
import kr.co.kihyun.beans.entity.MUser;
import kr.co.kihyun.beans.entity.TotDoc;
import kr.co.kihyun.beans.entity.TotDoc.TotRange;
import kr.co.kihyun.beans.entity.TotDocAppendType;
import kr.co.kihyun.beans.entity.TotDocProcess;
import kr.co.kihyun.beans.entity.TotDocType;
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 ECrossTotDoc extends ECross<TotDoc> {

	private static final Logger LOG = LoggerFactory.getLogger(ECrossTotDoc.class);
	@Persistent(nullValue = NullValue.EXCEPTION)
	private Long acceptanceLimitStrategy;
	@Persistent(nullValue = NullValue.EXCEPTION)
	private AccessAuth accessAuth = AccessAuth.PER;
	@Persistent(nullValue = NullValue.EXCEPTION)
	private TotDocAppendType appendType = TotDocAppendType.COPY;
	private transient String decName;
	@Persistent(nullValue = NullValue.EXCEPTION)
	private Date endDate = Calendar.getInstance().getTime();
	@Persistent
	private String executionCode;
	@Persistent(nullValue = NullValue.EXCEPTION)
	private int fixNum = 0;
	@Persistent
	private String form;
	@Persistent
	private Long group;
	@Persistent
	private String handyName;
	@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 masterDept;
	@Persistent
	private Long masterTotDoc;
	@Persistent
	private String name;
	@Persistent
	private String ownerEmail;
	@Persistent
	private String ownerName;
	@Persistent
	private String ownerPhone;
	@Persistent(customValueStrategy = "timestamp", nullValue = NullValue.EXCEPTION)
	private Date regDate;
	@Persistent(nullValue = NullValue.EXCEPTION)
	private Date startDate = Calendar.getInstance().getTime();
	@Persistent(nullValue = NullValue.EXCEPTION)
	private TotRange totRange = TotRange.INTERNAL;
	@Persistent
	private TotDocType type = TotDocType.TOT_DOC;

	public ECrossTotDoc(TotDoc totDoc) {
		super(totDoc);
		this.setAcceptanceLimitStrategy(totDoc.getAcceptanceLimitStrategy());
		this.setAccessAuth(totDoc.getAccessAuth());
		this.setAppendType(totDoc.getAppendType());
 		this.setDeleted(totDoc.isDeleted());
 		this.setDept(totDoc.getDept());
		this.setEndDate(totDoc.getEndDate());
 		this.setExecutionCode(totDoc.getExecutionCode());
		this.setFixNum(totDoc.getFixNum());
		this.setForm(totDoc.getForm());
		this.setGroup(totDoc.getGroup());
  		this.setHandyName(totDoc.getHandyName());
    		this.setId(totDoc.getId());
     		this.setManagementCode(totDoc.getManagementCode());
       		this.setMasterTotDoc(totDoc.getMasterTotDoc());
   		this.setName(totDoc.getName());
  		this.setUser(null);
		this.setOwnerEmail(totDoc.getOwnerEmail());
   		this.setOwnerName(totDoc.getOwnerName());
 		this.setOwnerPhone(totDoc.getOwnerPhone());
  		this.setRegDate(totDoc.getRegDate());
		this.setRepDept(totDoc.getRepDept());
 		this.setStartDate(totDoc.getStartDate());
		this.setTotRange(totDoc.getTotRange());
		this.setType(totDoc.getType());
	}

	@Override
	protected TotDoc setFieldValues(MPersistenceManager pm, TotDoc totDoc) {
            //System.out.println("**************************************************** acceptanceLimitStrategy;"+acceptanceLimitStrategy);
		totDoc.setAcceptanceLimitStrategy(AcceptanceLimitStrategy.create("LimitByWriteDateStrategy", 0L));
		totDoc.setAccessAuth(accessAuth);
		totDoc.setAppendType(appendType);
		totDoc.setDeleted(isDeleted);
		totDoc.setDept(mDept == null ? null : pm.getObjectById(MDept.class, mDept));
		totDoc.setEndDate(endDate);
		totDoc.setExecutionCode(MString.isNull(executionCode) ? null : ExecutionCode.valueOf(executionCode));
		totDoc.setFixNum(fixNum);
		totDoc.setForm(form);
		totDoc.setGroup(group);
		totDoc.setHandyName(handyName);
                totDoc.setId(id);
		totDoc.setMasterTotDoc(masterTotDoc == null ? null : pm.getObjectById(TotDoc.class, masterTotDoc));
		totDoc.setName(name);
		totDoc.setRegDate(regDate);
		totDoc.setRepDept(masterDept == null ? null : pm.getObjectById(MDept.class, masterDept));
		totDoc.setStartDate(startDate);
		totDoc.setTotRange(totRange);
		totDoc.setType(type);
		totDoc.setUser(null);
		totDoc.setOwnerEmail(ownerEmail);
		totDoc.setOwnerName(ownerName);
		totDoc.setOwnerPhone(ownerPhone);

		return totDoc;
	}

	public AccessAuth getAccessAuth() {
		return this.accessAuth;
	}

	public TotDocAppendType getAppendType() {
		return appendType;
	}

	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;
	}

	public Date getEndDate() {
		return this.endDate;
	}

	public final ExecutionCode getExecutionCode() {
		try {
			if (executionCode != null) {
				return ExecutionCode.valueOf(executionCode);
			} else {
				return new ExecutionCode(executionCode, null, null);
			}
		} catch (Exception e) {
			LOG.error("Unable to parse execution code for {}", executionCode);
			return new ExecutionCode(executionCode, null, null);
		}
	}

	public int getFixNum() {
		return this.fixNum;
	}

	public String getForm() {
		return this.form;
	}

	public Long getGroup() {
		return group;
	}

	public String getHandyName() {
		return this.handyName;
	}

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

	public final String getManagementCode() {
		return managementCode;
	}

	public String getName() {
		return this.name;
	}

	/**
	 * @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();
	}

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

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

	public Date getRegDate() {
		return this.regDate;
	}

	public MDept getRepDept(PersistenceManager pm) {
		return pm.getObjectById(MDept.class, this.masterDept);
	}

	public Date getStartDate() {
		return this.startDate;
	}

	public TotRange getTotRange() {
		return this.totRange;
	}

	public TotDocType 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 setAcceptanceLimitStrategy(AcceptanceLimitStrategy acceptanceLimitStrategy) {
		this.acceptanceLimitStrategy = acceptanceLimitStrategy == null ? null : acceptanceLimitStrategy.getId();
	}

	public void setAccessAuth(AccessAuth accAuth) {
		this.accessAuth = accAuth;
	}

	public void setAppendType(TotDocAppendType appendType) {
		this.appendType = appendType;
	}

	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;
	}

	public void setEndDate(Date edDate) {
		this.endDate = edDate;
	}

	public final void setExecutionCode(ExecutionCode executionCode) {
		this.executionCode = executionCode == null ? null : executionCode.toString();
	}

	public void setFixNum(int num) {
		this.fixNum = num;
	}

	public final void setForm(String form) {
		this.form = form;
	}

	public void setGroup(Long group) {
		this.group = group;
	}

	public void setHandyName(String name) {
		this.handyName = name;
	}

	/**
	 * @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 setMasterTotDoc(TotDoc doc) {
		this.masterTotDoc = doc == null ? null : doc.getId();
	}

	public final void setName(String name) {
		this.name = name;
	}

	/**
	 * @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 setRegDate(Date regDate) {
		this.regDate = regDate;
	}

	public void setRepDept(String masterDept) {
		this.masterDept = masterDept;
	}

	public void setStartDate(Date stDate) {
		this.startDate = stDate;
	}

	public void setTotRange(TotRange r) {
		this.totRange = r;
	}

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

	public final void setUser(MUser mUser) {
		if (mUser == null) {
			this.mUser = null;
			setOwnerEmail(null);
			setOwnerName(null);
			setOwnerPhone(null);
		} else {
			LOG.debug("tot doc user id: {}", mUser.getId());
			this.mUser = mUser.getId();
			setOwnerEmail(mUser.getEmail());
			setOwnerName(mUser.getEncName());
			setOwnerPhone(mUser.getPhone());
		}
	}
}
