package web;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;

import java.io.*;
import java.sql.*;
import java.util.*;

public class BBSPostServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String id =(String) session.getAttribute("LOGIN_ID");
		if(id == null){
			throw new ServletException("게시글을 등록하려면 먼저 로그인을 해야합니다");
			
		}
		String title = request.getParameter("TITLE");
		String content = request.getParameter("CONTENT");
		if(title == null || content ==null){
			throw new ServletException("데이터를 입력하십시오");
		}
		int seqNo = 1;
		GregorianCalendar now = new GregorianCalendar();
		Connection conn = null;
		Statement stmt = null;
		try{
			Class.forName("org.apache.commons.dbcp.PoolingDriver"); 
			conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:/wdbpool");
			if(conn == null)
				throw new ServletException("데이터베이스에 연결할 수 없습니다");
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select max(seqno) as max_seqno from bbs");
			if(rs.next())
				seqNo = rs.getInt("max_seqno") +1;
			
			String command = String.format("insert into bbs(seqno,title,content,writer,wdate,wtime) value"+
					 "(%d, '%s', '%s', '%s', '%TF', '%TT'),(seqno,title,content,id,now,now);");
			int rowNum = stmt.executeUpdate(command);
			if(rowNum < 1)
				throw new ServletException("데이터를 db에 입력 할 수 없습니다 ");
		}catch(ClassNotFoundException cnfe){
			throw new ServletException(cnfe);
		}catch(SQLException se){
			throw new ServletException(se);
		}finally{
			try{
				stmt.close();
			}catch(Exception ignored){
				
			}try{
				conn.close();
			}catch(Exception ignored){
				
			}
		}
		response.sendRedirect("bbs-list");
		
	}

}
