/*************************************************************************************************
 * 프로그램명 : HttpAssign.java 프로그램설명 : 보고자 이관 요청에 대한 정보 작성자 : 작성일 : 변경일 :
 **************************************************************************************************/

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

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.MUser;
import kr.co.kihyun.beans.entity.TotReport;
import kr.co.kihyun.beans.entity.TotReportProcess;
import kr.co.kihyun.beans.entity.util.MPersistenceManager;
import kr.co.kihyun.beans.entity.util.*;
import kr.co.kihyun.beans.totsys.report.ReportUpdate;
import kr.co.kihyun.beans.user.HttpSSOLogin;
import kr.co.kihyun.lang.MInteger;
import kr.co.kihyun.lang.MLong;
import kr.co.kihyun.moumi.MoumiConfig;
import kr.co.kihyun.moumi.report.MReport;
import kr.co.kihyun.text.html.ServletUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebServlet("/servlet/kr.co.kihyun.beans.totsys.repoper.HttpNotUpdate")
public class HttpNotUpdate extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger LOG = LoggerFactory.getLogger(HttpNotUpdate.class);

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

		/********** session내의 userId **********/
		String usID = HttpSSOLogin.getLoginID(req);

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

		/********** parameter value **********/
		Long reportID = MLong.parseLong(req.getParameter("reportID"));

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

			MUser mUser = pm.getObjectById(MUser.class, usID);
			TotReport totReport = pm.getObjectById(TotReport.class, reportID);
			totReport.setProcess(TotReportProcess.NOT);
			totReport.setUser(mUser);
                        totReport.setSubmitDate(new Date());
			pm.makePersistent(totReport);
			tx.commit();
			LOG.info(TotReportProcess.NOT + " {}, {}, {}", new Object[] { usID, req.getRemoteAddr(), reportID });
			out.println(ServletUtil
					.getJavaScript("document.location.replace('/totsys/repoper/mydocbox/endbox/report_list.jsp?reportType=COMP')"));
		} catch (Exception ex) {
			LOG.error(TotReportProcess.NOT + MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.forward") + MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.fail") +"  {}, {}, {}", new Object[] { usID, req.getRemoteAddr(), reportID });
			out.println(ServletUtil.alert(TotReportProcess.NOT + MoumiConfig.getMessageBundle().getString("moumi.message.popup.transFail")));
			out.println(ServletUtil.getJavaScript("history.back();"));
			ex.printStackTrace();
		} finally {
			if (tx.isActive())
				tx.rollback();
			pm.close();
		}
	}
}
