// Written by Brad Simonin.
// This Java Servlet writes information from an Access 97 database
// to an HTML table using the JDBC:ODBC Bridge.

import java.io.*;
import java.util.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class ViewGuestBookServlet extends HttpServlet
{
	private Connection con = null;
	
	public void init (ServletConfig config)
	throws ServletException
	{
		super.init(config);
		
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			String url = "jdbc:odbc:Brad2000";  // Brad is the name of my ODBC DSN
			con = DriverManager.getConnection(url);
		}
		catch(SQLException SQL_ex)
		{
			throw new UnavailableException(this, "Could not get Database connection");
		}
		catch(ClassNotFoundException Class_ex)
		{
			throw new UnavailableException(this, "Could not load database driver");
		}	
	} // end init method
	
	
	public void doGet (HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		res.setContentType("text/html");
		ServletOutputStream out = res.getOutputStream();
		StringBuffer sb = new StringBuffer();
		
		/*
		HttpSession session = req.getSession(true);
		Integer count = (Integer)session.getValue("tracker.count");
		if (count == null)
			count = new Integer(1);
		else
			count = new Integer(count.intValue() + 1);
		session.putValue("tracker.count", count);
		*/
		
		String SqlString;
		String HTMLString;
		int ThePos1;
		int ThePos2;
		
		SqlString = "SELECT GuestBook_Id, Name, Company_Occupation, City, " +
				"State, E_Mail, Web_Page FROM Brads_GuestBook ORDER BY Time_Stamp Desc";
				
		sb.append ("<html><head><title>Brad Simonin's GuestBook using a Java Servlet</title></head><body>");
		HTMLString = "<TABLE BORDER=1 BGCOLOR=#ffffff CELLSPACING=0 CELLPADDING=\"5\">" +
					 "<FONT FACE=\"Arial\" COLOR=#000000><CAPTION><B>Brad Simonin\'s GuestBook</B></CAPTION>";
		sb.append(HTMLString);
		HTMLString = "<THEAD><TR><TH>Name</TH><TH>Occupation</TH><TH>City</TH>" +
					 "<TH>State</TH><TH>E-Mail</TH><TH>Web Page</TH><TH>View Comments</TH>" +
					 "<TR></THEAD><TBODY>";
		sb.append(HTMLString);
					 
		try
		{
			Statement stmt;
			stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(SqlString);
			while(rs.next())
			{
				ThePos1 = -1;
				ThePos2 = -1;
				int Id = rs.getInt("GuestBook_Id");
				String Name = rs.getString("Name");
				String Occupation = rs.getString("Company_Occupation");
				String City = rs.getString("City");
				String State = rs.getString("State");
				String E_Mail = rs.getString("E_Mail");
				String WebPage = rs.getString("Web_Page");
				
				if (Name == null)
					Name = "";
				if (Occupation == null)
					Occupation = "";
				if (City == null)
					City = "";
				if (State == null)
					State = "";
				if (E_Mail == null)
					E_Mail = "";
				if (WebPage == null)
					WebPage = "";
				if (WebPage.indexOf((String)"#") != -1)
				{
					ThePos1 = WebPage.indexOf((String)"#",0);
					ThePos2 = WebPage.indexOf((String)"#",ThePos1 + 1);
					WebPage = WebPage.substring(ThePos1 + 1,ThePos2);	
				}
				WebPage = WebPage.trim();
								
				HTMLString = "<TR ALIGN=TOP><TD>" + Name + "<BR></TD>" +
						"<TD>" + Occupation + "<BR></TD>" +
						"<TD>" + City + "<BR></TD>" +
						"<TD>" + State + "<BR></TD>" +
						"<TD NOWRAP><A HREF=\"mailto:" + E_Mail + "\">" + E_Mail + "</A><BR></TD>" +
						"<TD NOWRAP><A HREF=\"" + WebPage + "\" TARGET=\"_BLANK\">" + WebPage + "</A><BR></TD>" +
						"<TD><form ACTION=\"http://128.123.20.93/ShowComments.jsp\" Method=\"POST\">" +
						"<input TYPE=\"HIDDEN\" NAME = \"ID\" VALUE=\"" + Id + "\">" +
						"<input TYPE=\"Submit\" VALUE=\"View Comments\" STYLE=\"font: 10pt Arial Black; background:Blue\">" + 
						"</form></TD>";
				sb.append(HTMLString);	
				
			}
			stmt.close();
		}
		catch(SQLException SQL_ex)
		{
			out.println("SQLException: " + SQL_ex.getMessage());
		}
		
		sb.append("</TBODY><TFOOT></TFOOT></TABLE>");
		sb.append("<P>To view the source code of this Java Servlet click on the " +
					"following hyperlink: <A HREF=\"http://www.cs.nmsu.edu/~bsimonin/ViewGuestBookServlet.java\">" +
					"ViewGuestBookServlet.java</a><P>");
		/*
		sb.append("SESSION TESTING:  You've visited this page " + count + 
					((count.intValue() == 1) ? " time." : " times."));
		sb.append("<p>");
		*/
		String BackButton;
		BackButton = "<FORM><p><input TYPE=\"button\" VALUE=\"   Back   \" " +
			"STYLE=\"font: 10pt Arial Black; background:Teal\" " + 
			"onClick=\"history.back(1)\"> </p>"; 
		sb.append (BackButton);
		sb.append ("</FORM></body></html>");
		
		try
		{
			res.setContentLength(sb.length());	
			out.println(sb.toString());
			sb = null;
			out.close();
		}
		catch(IOException IO_ex)
		{
			out.println("IOException: " + IO_ex.getMessage());
		}
			
	} // end doGet method
	
	
	public void destroy()
	{
		try
		{
			if (con != null) 
				con.close();
		}
		catch(SQLException ignored) {}
		
	} // end destroy method
	
	
} // end ViewGuestBookServlet
