/*************************************************************************************************
 * 프로그램명 : HttpDocCancle.java 프로그램설명 : 집계취소 파라미터 작성자 : 작성일 : 변경일 :
 **************************************************************************************************/

package kr.co.kihyun.beans.totsys.repoadm;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.kihyun.beans.entity.Recog;
import kr.co.kihyun.beans.entity.Repoadm;
import kr.co.kihyun.beans.entity.TotDocType;
import kr.co.kihyun.beans.entity.TotReport;
import kr.co.kihyun.beans.entity.TotReportProcess;
import kr.co.kihyun.beans.entity.util.*;
import kr.co.kihyun.beans.user.HttpSSOLogin;
import kr.co.kihyun.lang.Encoder;
import kr.co.kihyun.lang.MInteger;
import kr.co.kihyun.lang.MLong;
import kr.co.kihyun.lang.MString;
import kr.co.kihyun.moumi.MoumiConfig;
import kr.co.kihyun.text.html.ServletUtil;
@WebServlet("/servlet/kr.co.kihyun.beans.totsys.repoadm.HttpRecogUpdate")
public class HttpRecogUpdate extends HttpServlet {
	private static final long serialVersionUID = -3115363713832399885L;

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		res.setContentType("text/html;charset=UTF-8");
		PrintWriter out = res.getWriter();

		/*********** User HttpSSOLogin check ***********/
		if (!HttpSSOLogin.isLogin(req)) {
			String connURL = "location='/totsys/login/login.jsp';";
			out.println(ServletUtil.getJavaScript(connURL));
			return;
		}

		String usID = HttpSSOLogin.getLoginID(req);
		int sysAuth = HttpSSOLogin.getSysAuth(req);

		String contents = Encoder.toJava(MString.checkNull(req.getParameter("contents")));
		Long repoadminID = MLong.parseLong(req.getParameter("repoadminID"));
		int repoadmType = MInteger.parseInt(req.getParameter("repoadmType"), 0);

		PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();

			Repoadm repoadm = pm.getObjectById(Repoadm.class, repoadminID);
			TotReport totReport = pm.getObjectById(TotReport.class, repoadm.getTotReport().getId());

			if (sysAuth == MoumiConfig.TOTPER && !usID.equals(repoadm.getRecogUser(pm).getId())) {
				writeFailMessage(out, repoadmType);
				return;
			}

			if (repoadmType == Recog.NOT_RECOG.ordinal()) {
				repoadm.setRecog(Recog.PRSS);
				totReport.setCurrentRepoadm(totReport.getRepoadms().get(repoadm.getRecogNum() + 1));
			} else if (repoadmType == Recog.RETURN_RECOG.ordinal()) {
				repoadm.setRecog(Recog.RETURN_RECOG);
				repoadm.setHistory(contents);
				totReport.setProcess(TotReportProcess.TEMP);
				totReport.setRecog(Recog.RETURN_RECOG);
			} else if (repoadmType == Recog.ARBITRARY.ordinal()) {
				repoadm.setRecog(Recog.ARBITRARY);
				totReport.setProcess(TotReportProcess.COMP);
			} else if (repoadmType == Recog.FINAL_RECOG.ordinal()) {
				repoadm.setRecog(Recog.FINAL_RECOG);
				totReport.setRecog(Recog.COMP);
				totReport.setProcess(TotReportProcess.COMP);
			}

			repoadm.setDecDate(new Date());
			pm.makePersistent(repoadm);
			pm.makePersistent(totReport);

			tx.commit();

			if (repoadmType == Recog.NOT_RECOG.ordinal()) {
				out.println(ServletUtil.alert(TotDocType.TOT_DOC
						+ MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.recog") + MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.success") +" !"));
			} else if (repoadmType == Recog.RETURN_RECOG.ordinal()) {
				out.println(ServletUtil.alert(TotDocType.TOT_DOC + " " + Recog.RETURN_RECOG + MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.success") +" !"));
			} else if (repoadmType == Recog.ARBITRARY.ordinal()) {
				out.println(ServletUtil.alert(TotDocType.TOT_DOC + " " + Recog.ARBITRARY + MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.success") +" !"));
			} else if (repoadmType == Recog.FINAL_RECOG.ordinal()) {
				out.println(ServletUtil.alert(TotDocType.TOT_DOC + " " + Recog.FINAL_RECOG + MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.success") +" !"));
			}

			out.println(ServletUtil
					.getJavaScript("document.location='/totsys/repoper/mydocbox/tempbox/report_list.jsp?reportType=RECOG';"));

		} catch (Exception e) {
			e.printStackTrace();
			writeFailMessage(out, repoadmType);

		} finally {
			if (tx.isActive())
				tx.rollback();
			pm.close();
		}
	}

	private void writeFailMessage(PrintWriter out, int repoadmType) {
		if (repoadmType == Recog.NOT_RECOG.ordinal()) {
			out.println(ServletUtil.alert(TotDocType.TOT_DOC + " "
					+ MoumiConfig.getMessageBundle().getString("moumi.message.popup.approvalFail")));
		} else if (repoadmType == Recog.RETURN_RECOG.ordinal()) {
			out.println(ServletUtil.alert(TotDocType.TOT_DOC + " " + Recog.RETURN_RECOG
					+ MoumiConfig.getMessageBundle().getString("moumi.message.popup.fail")));
		} else if (repoadmType == Recog.ARBITRARY.ordinal()) {
			out.println(ServletUtil.alert(TotDocType.TOT_DOC + " " + Recog.ARBITRARY
					+ MoumiConfig.getMessageBundle().getString("moumi.message.popup.fail")));
		} else if (repoadmType == Recog.FINAL_RECOG.ordinal()) {
			out.println(ServletUtil.alert(TotDocType.TOT_DOC + " " + Recog.FINAL_RECOG
					+ MoumiConfig.getMessageBundle().getString("moumi.message.popup.fail")));
		}
		out.println(ServletUtil
				.getJavaScript("document.location='/totsys/repoper/mydocbox/tempbox/report_list.jsp?reportType=RECOG';"));
	}
}
