/*************************************************************************************************
 *
 **************************************************************************************************/

package kr.co.kihyun.beans.batch;

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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import kr.co.kihyun.beans.entity.util.MPersistenceManager;
import kr.co.kihyun.beans.entity.util.PMF;
import kr.co.kihyun.io.MultipartRequest;
import kr.co.kihyun.text.html.ServletUtil;

@WebServlet("/servlet/kr.co.kihyun.beans.batch.HttpUpdate")
public class HttpUpdate extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = LoggerFactory.getLogger(HttpUpdate.class);
    String assignInfo = "";

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        //
        Transaction tx = null;
        PrintWriter out = null;
        PersistenceManager pm = null;    
        String updateType = req.getParameter("updateType");

        try {
            out = res.getWriter();
            pm = new MPersistenceManager(PMF.get().getPersistenceManager());
            tx = pm.currentTransaction();
            GroupConinfoUp et = new GroupConinfoUp();

            tx.begin();
            System.out.println("start =======   "+updateType);

            if("U".equals(updateType)) {
            	System.out.println("사용자정보 업데이트!");
            	et.getUserSync();
            }
            else  {
            	System.out.println("부서정보 업데이트!");
            	et.getDeptSync();
            }
            
            
            System.out.println("stop =======   ");

            tx.commit();

            out.println(ServletUtil.alert("Success Merge Update"));
            
            String connURL = "location='/totsys/login/top.jsp';";
            out.println(ServletUtil.getJavaScript(connURL));
        } catch (IOException ioex) {
            out.println(ServletUtil.alert("Fail Meger Update"));
            out.println(ServletUtil.getJavaScript("history.back();"));
            ioex.printStackTrace();
        } catch (Exception ex) {
            out.println(ServletUtil.alert("Fail Meger Update"));
            out.println(ServletUtil.getJavaScript("history.back();"));
            ex.printStackTrace();
        } finally {
            if (tx.isActive())
                tx.rollback();
            pm.close();
        }
        //================================================

    }
}
