package edu.umd.cs.findbugs.flybush;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringEscapeUtils;

public class WebPageServlet extends AbstractFlybushServlet {
    private static final DateFormat TIMESTAMP_FORMAT = new SimpleDateFormat("MMM d, ''yy 'at' h:mm aa z");

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getRequestURI().startsWith("/issues/")) {
            handleIssueRequest(getPersistenceManager(), req, resp, req.getRequestURI());
        } else {
            show404(resp);
        }

    }

    @SuppressWarnings({"unchecked"})
    private void handleIssueRequest(PersistenceManager pm, HttpServletRequest req, HttpServletResponse resp, String uri)
            throws IOException {
        if (req.getParameter("embed") == null) {
            show404(resp);
            return;
        }
        Pattern p = Pattern.compile("/issues/([^/?]+)");
        Matcher m = p.matcher(uri);
        if (!m.matches()) {
            show404(resp);
            return;
        }
        String hash = m.group(1);
        Map<String,DbIssue> map = persistenceHelper.findIssues(pm, Lists.<String>newArrayList(hash));
        DbIssue issue = map.get(hash);
        if (issue == null) {
            printHtmlPreface(resp);
            resp.getWriter().println("<p>This issue has not been submitted to the " + getCloudName() + "</p>");
            return;
        }
        List<DbEvaluation> list = Lists.newArrayList(sortAndFilterEvaluations(issue.getEvaluations()));
        if (list.isEmpty()) {
            printHtmlPreface(resp);
            resp.getWriter().println("<p>No comments have been submitted for this issue</p>");
            return;
        }
        Collections.reverse(list);

        PrintWriter out = resp.getWriter();
        printHtmlPreface(resp);
        out.println("<table border=0 class=popup-evals cellspacing=15>");
        for (DbEvaluation evaluation : list) {
            out.println("<tr>");
            out.println("<td>" + StringEscapeUtils.escapeHtml(evaluation.getEmail())
                    + "<br><span class=timestamp>" + TIMESTAMP_FORMAT.format(new Date(evaluation.getWhen())) + " </span></td>");

            out.println("<td><strong>" + StringEscapeUtils.escapeHtml(evaluation.getDesignation()) + "</strong>" +
                    " &mdash; " + StringEscapeUtils.escapeHtml(evaluation.getComment()) + "</td>");
            out.println("</tr>");
        }
        out.println("</table>");
    }

    private void printHtmlPreface(HttpServletResponse resp) throws IOException {
        resp.setStatus(200);
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html");
        out.println("<html>");
        out.println("<head>");
        out.println("<style type=text/css>");
        out.println("p, table.popup-evals td { font-family: sans-serif; vertical-align: top }");
        out.println(".timestamp { font-size: x-small }");
        out.println("</style>");
        out.println("</head>");
        out.println("<body>");
    }

    @Override
    protected void handlePost(PersistenceManager pm, HttpServletRequest req, HttpServletResponse resp, String uri)
            throws IOException {
    }
}

@@ -1950,32 +1950,74 @@
 e() + %22%3C/p%3E%22);%0D%0A
+            LOGGER.info(%22Not in cloud%22);%0D%0A
             retu
@@ -2024,32 +2024,34 @@
 rn;%0D%0A        %7D%0D%0A
+%0D%0A
         List%3CDbE
@@ -2135,24 +2135,118 @@
 tions()));%0D%0A
+        LOGGER.info(%22Issue %22 + issue.getPrimaryClass() + %22 - %22 + list.size() + %22 comments%22);%0D%0A
         if (

Improve App Engine logging for hudson embedded iframe calls