package zall.action.media;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import zall.action.ZalleryAction;
import zall.bean.Comment;
import zall.bean.Media;
import zall.bean.User;
import zutil.db.DBConnection;
import zutil.ui.UserMessageManager;
import zutil.ui.UserMessageManager.MessageLevel;
import zutil.ui.UserMessageManager.UserMessage;

public class CommentAction extends ZalleryAction{

	public CommentAction() {
		super("comment", true);
	}

	@Override
	public void handleRequest(DBConnection db, HttpServletRequest request, HttpServletResponse response, HttpSession session,
			PrintWriter out, User user, UserMessageManager msgs) throws SQLException, IOException {

		// Comments for media
		if( request.getParameter("id") == null || request.getParameter("type") == null ){
			msgs.add(new UserMessage(MessageLevel.ERROR, "Missing parameters!"));
			return;
		}
		long id =  Long.parseLong(request.getParameter("id"));
		Media media = Media.load(db, request.getParameter("type"), id);

		Comment cm = new Comment();
		cm.setUser( user );
		cm.setMessage( request.getParameter("msg") );
		media.addComment( cm );
		media.save( db );

		msgs.add(new UserMessage(MessageLevel.INFO, "Comment saved."));

	}

}
