/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.web;

import com.ejb.Notice;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jms.Connection;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "PostNotice", urlPatterns = {"/PostNotice"})
public class PostNotice extends HttpServlet {
    @Resource(mappedName = "jms/Notice")
    private javax.jms.Queue notice;
    @Resource(mappedName = "jms/NoticeFactory")
    private ConnectionFactory noticeFactory;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String title=request.getParameter("title");
        String body=request.getParameter("body");
        String author=request.getParameter("author");
        if(author==null)author="";
        if(author.equals(""))author="匿名";
        if ((title!=null) && (body!=null)) {
            try {
                Connection connection =  noticeFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(notice);
                ObjectMessage message = session.createObjectMessage();
                Notice e = new Notice();
                e.setTitle(title);
                e.setBody(body);
                e.setAuthor(author);
                e.setPublishDate(new java.sql.Timestamp(System.currentTimeMillis()));
                message.setObject(e);
                messageProducer.send(message);
                messageProducer.close();
                connection.close();
                response.sendRedirect("NoticeList");
                } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }
        PrintWriter out = response.getWriter();
        out.println("<html>");
      out.println("<meta http-equiv='Content-Type' content='text/html'; charset='UTF-8'>");
        out.println("<head>");
        out.println("<title>公告发布栏</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<form>");
        out.println("作者: <input type='text' name='author'><br/>");
        out.println("标题: <input type='text' name='title'><br/>");
        out.println("内容: <textarea name='body'></textarea><br/>");
        out.println("<input type='submit' value='发布'><br/>");
        out.println("</form>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private Message createJMSMessageForjmsNotice(Session session, Object messageData) throws JMSException {
        // TODO create and populate message to send
        TextMessage tm = session.createTextMessage();
        tm.setText(messageData.toString());
        return tm;
    }

    private void sendJMSMessageToNotice(Object messageData) throws JMSException {
        Connection connection = null;
        Session session = null;
        try {
            connection = noticeFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(notice);
            messageProducer.send(createJMSMessageForjmsNotice(session, messageData));
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }

  
}
