/*************************************************************************************************
 * 프로그램명 : HttpPartDeleter.java 프로그램설명 : 기관 등록자 작성자 : 조용준 작성일 : 040701 변경일 :
 **************************************************************************************************/
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.annotation.WebServlet;
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.SysAuth;
import kr.co.kihyun.beans.entity.util.*;
import kr.co.kihyun.beans.user.HttpSSOLogin;
import kr.co.kihyun.moumi.Moumi;
import kr.co.kihyun.lang.MLong;
import kr.co.kihyun.text.html.ServletUtil;
@WebServlet("/servlet/kr.co.kihyun.beans.totsys.sysadm.part.HttpPartDeleter")
public class HttpPartDeleter extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	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)) {
			String connURL = "location='/totsys/login/login.jsp';";
			out.println(ServletUtil.getJavaScript(connURL));
			return;
		}

		/********** part 삭제 *************/
		PersistenceManager pm = new MPersistenceManager(PMF.get().getPersistenceManager());
		Transaction tx = pm.currentTransaction();

		try {
			tx.begin();

			MDept mDept = pm.getObjectById(MDept.class, MLong.parseLong(req.getParameter("id")));
			pm.deletePersistent(mDept);

			tx.commit();
			out.println(ServletUtil.alert(Moumi.getMessageBundle().getString("moumi.message.popup.deleteCoporation")));
			out.println(ServletUtil.redirect("/totsys/sysadm/part/part_list.jsp"));
		} catch (Exception e) {
			e.printStackTrace();
			out.println(ServletUtil.alert(Moumi.getMessageBundle().getString("moumi.message.popup.notDeleteCoporationAdmin")));
			out.println(ServletUtil.redirect("/totsys/sysadm/part/part_list.jsp"));
		} finally {
			if (tx.isActive())
				tx.rollback();
			pm.close();
		}
	}
}
