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

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

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

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.HttpTransfer3")
public class HttpTransfer3 extends HttpServlet {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger LOG = LoggerFactory.getLogger(HttpTransfer5.class);
	String assignInfo = "";

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
		res.setContentType("text/html;charset=UTF-8");
		PrintWriter out = res.getWriter();
		if (!HttpSSOLogin.isLogin(req))
        {
			String connURL = "location='/totsys/login/login.jsp';";
			out.println(ServletUtil.getJavaScript(connURL));
			return;
		}
		String user = HttpSSOLogin.getLoginID(req);
		String userID = Encoder.toJava(req.getParameter("userID"));
        String reportType = Encoder.toJava(req.getParameter("reportType"));
        String reportStr=req.getParameter("reportID");
        String rptReportidStr=req.getParameter("rptReportID");

        //System.out.println("reportStr::::::"+reportStr+"::::rptReportidStr::::"+rptReportidStr);
		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);
            //StringTokenizer tok=new StringTokenizer(reportStr,",");
            StringTokenizer tok=new StringTokenizer(rptReportidStr,",");

            while(tok.hasMoreTokens())
            {
                Long reportID=MLong.parseLong(tok.nextToken());
                //System.out.println("reportID 스트링 자른거::::"+reportID);
                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().getId());
                    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")));
	
           if(reportType.equals("COMP")) out.println(ServletUtil.getJavaScript("parent.opener.location='/totsys/repoper/mydocbox/compbox/report_list.jsp?reportType=COMP';"));
           else if(reportType.equals("END")) out.println(ServletUtil.getJavaScript("parent.opener.location='/totsys/repoper/mydocbox/endbox/report_list.jsp?reportType=END';"));
		   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();
		}
	}
}
