/*************************************************************************************************
 * 프로그램명 : HttpReProcess.java 프로그램설명 : 집계 문서 재진행 파라미터 작성자 : 작성일 : 변경일 :
 **************************************************************************************************/

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

import java.io.IOException;
import java.io.PrintWriter;

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.TotDoc;
import kr.co.kihyun.beans.entity.TotDocProcess;
import kr.co.kihyun.beans.entity.TotDocType;
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.MLong;
import kr.co.kihyun.lang.MString;
import kr.co.kihyun.moumi.MoumiConfig;
import kr.co.kihyun.text.html.ServletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebServlet("/servlet/kr.co.kihyun.beans.totsys.totper.HttpReProcess")
public class HttpReProcess extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
        private static final Logger LOG = LoggerFactory.getLogger(HttpReProcess.class);
	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;
		}

		/********** parameter value **********/
		Long docID = MLong.parseLong(req.getParameter("docID"));
		String userID = Encoder.toJava(req.getParameter("userID"));
		String url = MString.checkNull(req.getParameter("url"));
		int sysAuth = HttpSSOLogin.getSysAuth(req);

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

			TotDoc totDoc = pm.getObjectById(TotDoc.class, docID);
			if (sysAuth == MoumiConfig.TOTPER && !userID.equals(totDoc.getUser(pm).getId())) {
				out.println(ServletUtil.alert(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc")
						+ MoumiConfig.getMessageBundle().getString("moumi.message.popup.reProgressFail") + MoumiConfig.getMessageBundle().getString("moumi.message.popup.normalHandle")));
				out.println(ServletUtil
						.getJavaScript("document.location='/totsys/totper/mydocbox/prssbox/doc_list.jsp?docType=END';"));
				return;
			}
			totDoc.setProcess(TotDocProcess.REG);
			pm.makePersistent(totDoc);

			tx.commit();

			out.println(ServletUtil.alert(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc") + MoumiConfig.getMessageBundle().getString("moumi.message.popup.reProgressSuccess")));
			out.println(ServletUtil
					.getJavaScript("document.location='/totsys/totper/mydocbox/prssbox/doc_list.jsp?docType=END';"));
		} catch (Exception e) {
			e.printStackTrace();
			out.println(ServletUtil.alert(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc") + MoumiConfig.getMessageBundle().getString("moumi.message.popup.reProgressFail")));
			out.println(ServletUtil
					.getJavaScript("document.location='/totsys/totper/mydocbox/prssbox/doc_list.jsp?docType=END';"));
		} finally {
			if (tx.isActive())
				tx.rollback();
			pm.close();
		}
	}
}
