package src.hibernatebb.web;

import src.hibernatebb.discussion.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*; // for List and Iterator

/**
 * 
 * @author Jason Fillo
 * @author Peter Deschere
 * @version 1.0
 *
 */
public class PostTopicServlet extends HttpServlet {

    public void doPost(HttpServletRequest request,
		       HttpServletResponse response)
	throws IOException, ServletException {
	
	RequestDispatcher view;
	HttpSession session = request.getSession(false);
	  
	String userName = request.getParameter("name");
	String tname = request.getParameter("topicname");
	BoardManager bm = new BoardManager();
	List<String> topics = new ArrayList<String>();
	
	Topic t = new Topic();
	t.setID(tname);
	try {	
		bm.saveTopic(t);
	


	    List tl = bm.getTopics();
	    Iterator it = tl.iterator();
	    
	    
	    while(it.hasNext()) {
		topics.add(((Topic)it.next()).getID());
	    }
	    
	}
	catch(Exception e) {
	    topics.add(new String(e.toString()));
	    }
	
	
	request.setAttribute("topics", topics);	
    request.setAttribute("name", userName);

	view = request.getRequestDispatcher("main.jsp");	

	view.forward(request, response);
    }
}

