import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.etri.groupware.*;
import com.etri.groupware.bbs.*;

public class BBSDeleteItemServlet extends HttpServlet
{

	GroupwareControllerInterface groupwareController=null;
	BBSControllerInterface bbsController = null;
	ResultStatus bbsResultStatus = null;
	Properties pro=null;
	
	public void init(ServletConfig sc) throws ServletException
	{
		super.init(sc);
		
		//그룹웨어 컨트롤러를 생성한다. 먼저 프라퍼티를 생성한다.
		pro = new Properties();

		//DB매너저 구현 클래스 이름 설정
		pro.put("OrganizationDBManagerClassName", "PandaSoftOrganizationDBManager");
		pro.put("BBSDBManagerClassName", "PandaSoftBBSDBManager");
		pro.put("EDADBManagerClassName", "PandaSoftEDADBManager");
		pro.put("MailDBManagerClassName", "PandaSoftMailDBManger");
		
		try
		{
			//그룹웨어 컨트롤러 생성
			groupwareController = GroupwareController.init( pro );
			
			//게시판 관리 컨트롤러를 가져온다.
			bbsController = groupwareController.createBBSController();
		}
		catch(Exception e)
		{}
	}
	
	public void destroy()
	{
		super.destroy();
		DBConnectionManager.instance.release();
	}
	
	/*----------------------------------------------------------------- */
	/**
	 [메소드이름]: doPost<br>
	 [기능]: 게시물을 삭제하는 기능<br>
	 [파라미터]: HttpServletRequest req, HttpServletResponse res<br>
	 [호출자] : 사용자<br>
	 */
	/*--------------------------------------------------------------- */
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
	{
	
		// session 확인하는곳....		
		HttpSession session = req.getSession(true);
		
		boolean authentication = false ;
		String kind = null ;
		String id = null ;
		String name = null ;
		
		if ((session.isNew()))
		{
			session.invalidate();
			res.sendRedirect("../Login.html");
		}
	    else if(session.getValue("Authentication").equals("true")){
	    	authentication = true;
            kind = (String)session.getValue("Kind");
            id = (String)session.getValue("Id");
            name = (String)session.getValue("Name");          
       }
       else {
            session.invalidate();
            res.sendRedirect("../Login.html");
       }
	      
		res.setContentType("text/html;charset=euc-kr");		
		// 캐쉬를 못하도록..
		res.setHeader("cache-control", "no-cache");		
		PrintWriter out =new PrintWriter(new OutputStreamWriter( res.getOutputStream(),"EUC-KR"));
		
		String boardName = req.getParameter("boardName");
		boardName = new String( boardName.getBytes("ISO-8859-1"), "EUC-KR");
		System.out.println( boardName );

		String numberString = req.getParameter("number");
		int number= Integer.parseInt( numberString );
		System.out.println( number);	

		bbsResultStatus = bbsController.deleteItem(boardName, number);
		
		out.println("<html><head></head>");
		
		out.println("<basefont size=2>");
		out.println( "<body bgcolor=white text=\"#0000FF\" link=\"#0000FF\" alink=\"#FF2020\" vlink=\"#000000\">" ) ;
		
		// 게시판 우측에 들어가는 이미지...
		out.println("<p align=right><img src='../image/우측_게시판.gif'></p>");

		if( bbsResultStatus.getResultStatus() == 0 )
		{
			out.println("<p align=center><font size=2>" + name +"님 게시물 삭제를 성공했습니다.</font><br><br>");
			out.println("<a href='/servlet/BBSListServlet?boardName="+ boardName + "'><image src='../image/BBS_list.gif' alt='리스트보기' border=0></a></p>");
		}
		else
		{
			out.println("<p align=center><font size=2>" + name +"님 게시물 삭제를 실패했습니다.<br><br>");
			out.println("비밀번호가 일치하지 않습니다. </font><br><br>");
			out.println("<a href='/servlet/BBSListServlet?boardName="+ boardName + "'><image src='../image/BBS_list.gif' alt='리스트보기' border=0></a></p>");
		}

		out.println("</body></html>");
		
		out.close();
	}
}