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

package web;

import beans.LocationBean;
import ejb.NewsEntity;
import ejb.NewsEntityFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.ejb.EJB;
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 sinem
 */
@WebServlet(name="Edit", urlPatterns={"/Edit"})
public class Edit extends HttpServlet {
    @EJB
    private NewsEntityFacade newsEntityFacade;
    

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Long id = Long.parseLong(request.getQueryString().substring(3));
        NewsEntity ne = newsEntityFacade.find(id);
        LocationBean mybean = new LocationBean();
        Iterator i = mybean.getLocaleNames().iterator();
        try {
            // TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<link href=\"design/css/edit.css\" type=\"text/css\" rel=\"stylesheet\" />");
            out.println("<title>Edit</title>");  
            out.println("</head>");
            out.println("<body>");

            if(ne==null) {
                out.println("Such a message does not exist!");
            }
            else {
                out.println("<font id=\"header\">Edit Message:"+ ne.getTitle() +"</font>"); 
                out.println("<table id=\"general\">");
                out.println("<tr><td>");
                out.println("Posted at: </td><td>"+ne.getDate().toString()+"</td></tr>");
                out.println("<tr><td>Posted by: </td><td>"+ne.getSentBy()+"</td></tr>");
                out.println("<form name=\"saveChangesForm\" action=\"SaveChanges?id="+ne.getId() +"\" method=\"POST\">");
                
String topic=ne.getTopic();
String[] strArray = {"Hardware", "Software", "Programming", "Others(1)", "Plants", "Animals",
    "People", "Others(2)", "Politics", "Culture", "Sports", "Others(3)"};
int ind=0;
out.println("<tr><td>Topic:</td><td> <select name=\"topic\">");
out.println("<optgroup label=\"Computer\">");
if(!topic.equals(strArray[ind++]))
out.println("<option>Hardware</option>");
else
out.println("<option selected>Hardware</option>");
if(!topic.equals(strArray[ind++]))
out.println("<option>Software</option>");
else
out.println("<option selected>Software</option>");
if(!topic.equals(strArray[ind++]))
out.println("<option>Programming</option>");
else
out.println("<option selected>Programming</option>");
if(!topic.equals(strArray[ind++]))
out.println("<option>Others(1)</option>");
else
out.println("<option selected>Others(1)</option>");
out.println("</optgroup>");
out.println("<optgroup label=\"Nature\">");
if(!topic.equals(strArray[ind++]))
out.println("<option>Plants</option>");
else
out.println("<option selected>Plants</option>");
if(!topic.equals(strArray[ind++]))
out.println("<option >Animals</option>");
else
out.println("<option selected>Animals</option>");
if(!topic.equals(strArray[ind++]))
out.println("<option>People</option>");
else
out.println("<option selected>People</option>");
if(!topic.equals(strArray[ind++]))
out.println("<option>Others(2)</option>");
else
out.println("<option selected>Others(2)</option>");
out.println("</optgroup>");
out.println("<optgroup label=\"News\">");
if(!topic.equals(strArray[ind++]))
out.println("<option>Politics</option>");
else
out.println("<option selected>Politics</option>");
if(!topic.equals(strArray[ind++]))
out.println("<option>Culture</option>");
else
out.println("<option selected>Culture</option>");
if(!topic.equals(strArray[ind++]))
out.println("<option>Sports</option>");
else
out.println("<option selected>Sports</option>");
if(!topic.equals(strArray[ind++]))
out.println("<option>Others(3)</option>");
else
out.println("<option selected>Others(3)</option>");
out.println("</optgroup>");
out.println("</select ></td></tr>");
out.println("<tr><td>Title:</td><td> <input type=\"text\" name=\"title\" value=\""+ne.getTitle()+"\" size=\"28\" /></td></tr>");
               out.println("<tr><td>Message:</td><td> <textarea name='body' rows=\"7\" cols=\"33\">"+ne.getBody()+"</textarea></td></tr>");
               out.println("<tr><td>Location:</td><td> <select name='localInfo'>");
        while(i.hasNext()) {
            String temp= i.next().toString();
            if(!temp.equals(ne.getLocation())) {
                out.println("<option>"+temp+"</option>");
            }
            else {
                out.println("<option selected>"+temp+"</option>");
            }
        }
out.println("</select></td></tr>");

            }
            out.println("<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"Save Changes\" name=\"editButton\" /></td></tr>");
            out.println("</form>");
            out.println("</table>");
            out.println("<a href=\"ListNews\"><img id=\"home\" src=\"design/Pictures/home.png\"></a>");
            out.println("</body>");
            out.println("</html>");

        } finally { 
            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>

}



