/*************************************************************************************************
 * 프로그램명 : HttpPartModifier.java 프로그램설명 : 기관 변경자 작성자 : 조용준 작성일 : 040630 변경일 :
 **************************************************************************************************/
package kr.co.kihyun.beans.totsys.sysadm.part;

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

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.kihyun.beans.entity.MDept;
import kr.co.kihyun.beans.entity.util.*;
import kr.co.kihyun.beans.totsys.report.HttpReportRegister;
import kr.co.kihyun.moumi.MoumiConfig;
import kr.co.kihyun.beans.user.HttpSSOLogin;
import kr.co.kihyun.lang.MLong;
import kr.co.kihyun.text.html.ServletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpPartModifier extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5151697788170265809L;
        private static final Logger LOG = LoggerFactory.getLogger(HttpReportRegister.class);
        
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		doPost(req, res);
	}

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

		/*********** User Login check ***********/
		if (!HttpSSOLogin.isLogin(req)) {
			// login 성공후 되돌아올 주소
			String connURL = "location='/totsys/login/login.jsp';";
			out.println(ServletUtil.getJavaScript(connURL));
			return;
		}

		/********** parameter value **************/
		Long id = MLong.parseLong(req.getParameter("id"));
		String name = req.getParameter("name");
		String sysAuth = req.getParameter("sysAuth");
		String des = req.getParameter("des");
		Long upperID = MLong.parseLong(req.getParameter("upperID"));

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

		try {
			tx.begin();

			MDept mDept = pm.getObjectById(MDept.class, id);
//			mDept.setName(name);
//			mDept.setUpperDept(pm.getObjectById(MDept.class, upperID));
//			mDept.setSysAuth(SysAuth.TOTPER);
//			mDept.setAddress(des);
			pm.makePersistent(mDept);

			tx.commit();

			out.println(ServletUtil.alert(MoumiConfig.getMessageBundle().getString("moumi.message.popup.coporationChange")));
			out.println(ServletUtil.redirect("/totsys/sysadm/part/part_list.jsp?findOption=id&findWord=" + id));
		} catch (Exception e) {
			e.printStackTrace();
			out.println(ServletUtil.alert(MoumiConfig.getMessageBundle().getString("moumi.message.popup.notCoporationChangeAdmin")));
			//v2. 9.크로스사이트 스크립트 (PrintWrier) : Update by KWON,HAN
//                        out.println(ServletUtil.redirect("/totsys/sysadm/part/part_list.jsp"));
                        
                        // 수정 : 외부 입력값 필터링
                        String callbackFunc = "/totsys/sysadm/part/part_list.jsp";
                        String filtered_callbackFunc = callbackFunc.replaceAll("<","").replaceAll(">","").replaceAll("&","").replaceAll(",","");
                        LOG.debug("v2 9.크로스사이트 스크립트 (PrintWrier) : HttpPartModifier.doPost() filtered_callbackFunc2={}, Not Test", filtered_callbackFunc);
                        out.println(ServletUtil.redirect(filtered_callbackFunc));      
                        //========================================
            
		} finally {
			if (tx.isActive())
				tx.rollback();
			pm.close();
		}
	}
}
