package idc;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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 idc.comment.command.*;

/**
 * Servlet implementation class IdcCommentFrontController
 */
@WebServlet("*.cm")
public class IdcCommentFrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String COMMENT_PATH = "/jsp/comment/";
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdcCommentFrontController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control","no-Cache");
		response.setHeader("Cache-Control","no-store");
		response.setContentType("charset='utf-8'");
		
		
		request.setCharacterEncoding("UTF-8");
		String requestURI = request.getRequestURI();
		String contextPath = request.getContextPath();
		String cmdURI = requestURI.substring(contextPath.length());
		
		CommentCmd cmd = null;
		String viewPage = null;
		
		Object obj=request.getSession().getAttribute("member");
		if(obj!=null)
		{
			//글 목록 조회 처리
			if(cmdURI.equals("/commentList.cm")){
				cmd = new commentListCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",COMMENT_PATH+"commentList.jsp");
				viewPage = "DefaultPage.jsp";
			}
			
			//글 등록 처리		
			if(cmdURI.equals("/writeComment.cm")){
				cmd = new commentWriteCmd();
				cmd.execute(request, response);
				request.setAttribute("TargetFile",COMMENT_PATH+"commentList.jsp");
				viewPage = "commentList.cm";
				//viewPage = "DefaultPage.jsp";
			}
		}
		else
		{
			request.setAttribute("TargetFile", "jsp/main/IdcLoginForm.jsp");
			viewPage="DefaultPage.jsp";
		}
		RequestDispatcher dis = request.getRequestDispatcher(viewPage);
		dis.forward(request, response);
	}

}
