// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.


package jblog;

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.net.URLEncoder;

/**
 * display servlet for 
 * a very simple java web log
 *
 * @author Liz Warner
 */

public class Show extends HttpServlet {

    private static int numPerPage = 10;
    private PrintWriter out = null;
    private MySQLConnector mydb = null;

    private void printEntries(int start)
    {
        try
        {
           // create a new MySQLConnector object 
           mydb = new MySQLConnector();
           // get a valid Statement object from the connector
           Statement stmt = mydb.connect();

            // select the most recent log entries from the db
            ResultSet rs = stmt.executeQuery("SELECT data,  DATE_FORMAT(created,'%M %d, %Y %H:%i') as nice_date from blog order by created desc limit " + start + ", " + numPerPage);
            int counter = 0;
            while (rs.next() ) 
            // iterate through the list of entries, and print them to the page 
            {
                out.println("<b>" + rs.getString("nice_date") + "</b><br>" + rs.getString("data") + "<br><br>");
                counter++;
            }
            
            if (counter ==  numPerPage )
            {
                int next = start + numPerPage;
                out.println("<br><br><a href=\"/blog/Show?start=" + next + "\">More</a><br><br>");
            }
            else // out of entries - time to go back to the beginning 
            {
                out.println("That's it.  Go back to <a href=\"/blog/Show\">The Beginning</a> <br>");
            }

            // disconnect from mysql 
            mydb.disconnect(stmt);
        }
        catch (Exception ex) 
        {
            out.println("Error retrieving data: " + ex.toString() + "<br>");
        }
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {

        response.setContentType("text/html");
        out = response.getWriter();

        // print the HTML header 
        out.println("<html><head<title>Java Web log</title></head>");
        out.println("<body bgcolor=\"white\">");
        out.println("<h1> My Web log </h1>");

        int start;
        try
        {
            start = Integer.parseInt( request.getParameter("start") );
        }
        catch (NumberFormatException ne)
        {
            // bad number. start at zero
            start = 0;
        }
        // call the printEntries method to display log entries 
        printEntries(start);

        out.println("</body>");
        out.println("</html>");
    }
    
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
        throws IOException, ServletException
    {
        // goPost just calls goGet 
        doGet(request,response);
    }

}