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

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

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.MUser;
import kr.co.kihyun.beans.entity.TotDoc;
import kr.co.kihyun.beans.entity.TotReport;
import kr.co.kihyun.beans.entity.UserHistory;
import kr.co.kihyun.beans.entity.util.MPersistenceManager;
import kr.co.kihyun.beans.entity.util.*;
import kr.co.kihyun.beans.user.HttpSSOLogin;
import kr.co.kihyun.lang.Encoder;
import kr.co.kihyun.moumi.MoumiConfig;
import kr.co.kihyun.lang.MLong;
import kr.co.kihyun.text.html.ServletUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WebServlet("/servlet/kr.co.kihyun.beans.totsys.repoper.HttpTransfer")
public class HttpTransfer extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger LOG = LoggerFactory.getLogger(HttpTransfer.class);
	String assignInfo = "";

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

		/*********** 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"));
		String userID = Encoder.toJava(req.getParameter("userID"));
        String pageFlag = req.getParameter("pageFlag");
        

		/********** regist **********/
		String user = HttpSSOLogin.getLoginID(req);

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

			MUser fromUser = pm.getObjectById(MUser.class, user);
			MUser toUser = pm.getObjectById(MUser.class, userID);
			TotReport totReport = pm.getObjectById(TotReport.class, reportID);
			totReport.setUser(toUser);
			totReport.setDept(toUser.getUpperDept(pm));
			if (totReport.getSlaveTotDoc() != null) {
				TotDoc totDoc = pm.getObjectById(TotDoc.class, totReport.getSlaveTotDoc());
				totDoc.setUser(toUser);
				pm.makePersistent(totDoc);
			}
			pm.makePersistent(totReport);

			UserHistory userHistory = null;
			userHistory = new UserHistory(totReport.getTotDoc(), fromUser, toUser, toUser.getUpperDept(pm));
			pm.makePersistent(userHistory);

			tx.commit();

			out.println(ServletUtil.alert(MoumiConfig.getMessageBundle().getString("moumi.message.popup.chargeTrans")));
			LOG.info(MoumiConfig.getMessageBundle().getString("moumi.message.tot_report.chargeCh")+ MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.success") +" {}, {}, {}", new Object[] { userID, req.getRemoteAddr(), reportID });
//              	out.println(ServletUtil.getJavaScript("parent.opener.location='/totsys/totper/main.jsp';"));
            
            out.println(ServletUtil.getJavaScript("parent.opener.location='/totsys/repoper/mydocbox/tempbox/report_list.jsp?reportType=TEMP';"));
			out.println(ServletUtil.getJavaScript("parent.close();"));
		} catch (Exception ex) {
			ex.printStackTrace();
			out.println(ServletUtil.alert(MoumiConfig.getMessageBundle().getString("moumi.message.popup.reConfirmation")));
			out.println(ServletUtil.getJavaScript("parent.close();"));
		} finally {
			if (tx.isActive())
				tx.rollback();
			pm.close();
		}
	}
}
