Chris Pollett > Old Classes >
CS174

( Print View )

Grades: [Sec1]  [Sec2]

Submit: [Sec1]  [Sec2]

Course Info:
  [Texts & Links]
  [Topics]
  [Grading]
  [HW Info]
  [Exam Info]
  [Regrades]
  [Honesty]
  [Announcements]

HW Assignments:
  [Hw1]  [Hw2]  [Hw3]
  [Hw4]  [Hw5]

Practice Exams:
  [Mid1]  [Mid2]  [Final]

                            












HW4 Solutions Page

Return to homework page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<%
  /*
     about.jsp

     This page just displays information concerning 
     the making and workings of the Genealogy! site.
  */
%>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 

<head>  

  <title>Genealogy! - About this site.</title>

  <meta http-equiv="Content-Type" 
        content="text/html; charset=utf-8" />
  <meta name="Authors" content="Christopher Pollett" />
  <meta name="keywords" 
        content="Chris Pollett, Christopher Pollett, genealogy" />

  <link rel="stylesheet" 
   type="text/css" href="mystyles.css" title="mystyles"/>

</head>  
 
<body class="contentColor">
<jsp:include page="header.jsp" />
<h2>About this site...</h2>
<p>
This site was created by Chris Pollett as a solution to HW 4,
Spring semester 2002. The goal was to make a site to record
family trees/histories as easily as possible. 
</p>

<p>
Using the Create entry link, users can create or modify entries
in a database of family records. The form interface verifies
dates using Javascript before inserting new entries. Data
is currently stored in a MySQL database, however, if one changes
the driver and url in the web.xml file to the values given in class
this site works with Oracle.  
</p>

<p>
Using either the first letter
of the last name or using first name-last name combinations,
users can look up individual records. Under preferences one
control how many search results are displayed at a time.
This state information is maintained in a JavaBean called 
SearchBean.
</p>

<p>
Once an individual entry has been located, the user can trace
through the graphically represented family tree by clicking on links.
Users submit a form to comment on Person entries or provide 
additional information about these people. HttpSession
object are used here. They can also add an 
off-site link if they choose to places where more information 
can be had. In addition, people can vote on how relevant they find 
earlier such comments.
</p>

<p>
One last point about this site -- all pages on it have been 
validated using the W3C validator as XHTML1.1 compliant.
</p>

<p class="center"><a href="index.jsp">Return to main page</a></p>
<jsp:include page="footer.jsp" />
</body>
</html>
<%
  /*
     addRemarks.jsp
     
     This JSP is used to add a new comment on a Person entry
     in the Genealogy! site into the database.
  */
%>
<%@ page import="java.sql.*, java.net.*, javax.servlet.jsp.*" %>
<%@ page import="SearchBean" %> 

<jsp:useBean id="searchBean" class="SearchBean" scope="session" />
 

<%

     Class.forName(application.getInitParameter("jdbc.driver"));
     Connection conn = java.sql.DriverManager.getConnection(
       application.getInitParameter("jdbc.url"),
       application.getInitParameter("jdbc.user"),
       application.getInitParameter("jdbc.password")
     );

     Statement stat = conn.createStatement();
     String insert= "INSERT INTO Description VALUES ( '"+
      searchBean.getCode() + "', '" + 
      request.getParameter("author")+ "', '" + 
      request.getParameter("url") + "', '" +
      request.getParameter("comment") + "', '0', '0')";

      stat.executeUpdate(insert);
      conn.close(); 

      RequestDispatcher dispatcher =
         application.getRequestDispatcher("/entry.jsp");
      dispatcher.forward(request, response); 
%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<%
  /*
    create.jsp

    This file has a form whereby people can add new entries
    to the Genealogy! site database
  */
%>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
 
<head> 

  <title>Genealogy! - Create/Edit an entry</title>

  <meta http-equiv="Content-Type" 
        content="text/html; charset=utf-8" />
  <meta name="Authors" content="Christopher Pollett" />
  <meta name="keywords" 
        content="Chris Pollett, Christopher Pollett, genealogy" />

  <link rel="stylesheet" 
   type="text/css" href="mystyles.css" title="mystyles"/>
 
  <script type="text/javascript" src="verify.js" ></script>
</head>  
 
<body class="contentColor">

<%@ page import="SearchBean" %> 

<jsp:useBean id="searchBean" class="SearchBean" scope="session" />

<jsp:setProperty name="searchBean" property="*" />

<jsp:include page="header.jsp" />
<%
    String code = "" + searchBean.getCode();
    String firstName = "" + searchBean.getFirstName();
    String lastName = "" + searchBean.getLastName();
    String birthDate = "" + searchBean.getBirthDate();
    String deathDate = "" + searchBean.getDeathDate();
    String fatherCode = "" + searchBean.getFatherCode();
    String motherCode = "" + searchBean.getMotherCode();
    String spouseCodes;
    if(searchBean.getReset())
        spouseCodes="";
    else
       spouseCodes=""+session.getAttribute("spouseCodes");
 
%>

  <h2 class="center">Create/Edit an Entry Form:
  </h2>
  <form class="center" method="get" action="createDB.jsp"
       onsubmit="return verify(this);" >


  <table>
  <tr>
    <td class="bold" valign="top">First Name:
      <input type="text" name="firstName" size="10" 
          value="<%=firstName%>" maxlength="20"/>
    </td>
    <td><input type="hidden" name="code" value="<%=code%>" /></td>
    <td class="bold" valign="top">Last Name:
      <input type="text" name="lastName" size="10" 
          value="<%=lastName%>" maxlength="20" />
    <br />(<span style="color:red">Use maiden name for women</span>)
    </td>
  </tr>
  <tr><td>&nbsp;</td></tr>
  <tr>
    <td class="bold">Birth Date:
      <input type="text" name="birthDate" size="10" 
          value="<%=birthDate%>" maxlength="10" />
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

     (<span style="color:red">YYYY-MM-DD</span>)
    </td>
    <td></td>
    <td class="bold">Death Date:
      <input type="text" name="deathDate" size="10" 
          value="<%=deathDate%>" maxlength="10" />
      <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

      (<span style="color:red">YYYY-MM-DD</span>)

    </td>
  </tr>
  <tr><td>&nbsp;</td></tr>
  <tr>
    <td class="bold">Father Code:
      <input type="text" name="fatherCode" size="3" 
          value="<%=fatherCode%>" maxlength="9"/>
    </td>
    <td></td>
    <td class="bold">Mother Code:
      <input type="text" name="motherCode" size="3" 
          value="<%=motherCode%>" maxlength="9"/>
    </td>
  </tr>
  <tr>
    <td></td>
    <td class="bold">
      Spouse Codes:
      <input type="text" name="spouseCodes" size="6" 
          value="<%=spouseCodes%>" 
          maxlength="20"/>
      <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

      (<span style="color:red">Space separated</span>)       
    </td>
  </tr>
  <tr><td>&nbsp;</td></tr>
  <tr>
    <td></td>
    <td>
      <input type="submit" value="Submit"/><br />
    </td>
  </tr> 
  </table>    
  </form>

<jsp:include page="footer.jsp" />
</body>
</html>

<%
  /*
     createDB.jsp

     This page is called when a form is submitted by the create.jsp
     create.jsp sends data concerning a Person.
     It deletes any old rows (if they exist) associated with 
     this data (!!except from the Description table!!) and
     inserts new values. The response is then forwarded to
     entry.jsp to show the newly inserted data. 
  */
%>

<%@ page import="java.sql.*, java.net.*, javax.servlet.jsp.*" %>
<%@ page import="java.util.*" %>
<%@ page import="SearchBean" %> 

<jsp:useBean id="searchBean" class="SearchBean" scope="session" />
 
<%!
     /*
        The fields below correspond to fields in the Person
        table. scode corresponds to a space separated list of 
        spouses

     */
     String  code;
     String  last;
     String  first;
     String  bdate;
     String  ddate;
     String  mcode;
     String  fcode;
     String  scode;

     /*
       Deletes are old rows associated with the Person
       we are creating/updating

       stmt -- an initialized Statement object
     */
     void deleteOld(Statement stmt) throws Exception
     {
        String delete1= "DELETE FROM Married WHERE spouse1Code='"+
           code + "' OR  spouse2Code='" + 
           code + "'";

        stmt.executeUpdate(delete1);

        String delete2= "DELETE FROM Person WHERE code='"+
           code + "'";

        stmt.executeUpdate(delete2);

     }

     /*
       inserts the new rows associated with the Person
       we are creating/updating

       stmt -- an initialized Statement object
     */
     void insertNew(Statement stmt) throws Exception
     {
        String insert= "INSERT INTO Person VALUES ('"+
           code + "', '" + first + "', '" + last + "', '" +
           bdate + "', '" + ddate + "', '" + fcode + "', '" +
           mcode + "')"; 

        stmt.executeUpdate(insert);

        StringTokenizer t= new StringTokenizer(scode);
        while(t.hasMoreTokens())
        {
           insert = "INSERT INTO Married VALUES ('"+
              code + "', '" + t.nextToken() + "')";

           stmt.executeUpdate(insert);
        }
     }

     /*
       Calculates a new ID (if needed) for person we are inserting
       (note mysql and Oracle have slightly different syntax
        for auto-incrementing keys so what I did was to make 
        code more portable but slower)

       stmt -- an initialized Statement object
     */
     void calculateCode(Statement stmt) throws Exception
     {
        String count = "SELECT COUNT(*) FROM Person";
        ResultSet result=stmt.executeQuery(count);
        if(result.next())
            code=result.getString(1);
        result.close();
       
     }

     void getFormParameters(ServletRequest request)
     {
        code = ""+ request.getParameter("code");
        last = ""+ request.getParameter("lastName");
        first = ""+ request.getParameter("firstName");
        bdate = ""+ request.getParameter("birthDate");
        ddate = ""+ request.getParameter("deathDate");
        mcode = ""+ request.getParameter("motherCode");
        fcode = ""+ request.getParameter("fatherCode");
        scode = ""+ request.getParameter("spouseCodes");
     }
%>

<%
    getFormParameters(request);

     Class.forName(application.getInitParameter("jdbc.driver"));
     Connection conn = java.sql.DriverManager.getConnection(
       application.getInitParameter("jdbc.url"),
       application.getInitParameter("jdbc.user"),
       application.getInitParameter("jdbc.password")
     );
    
     Statement stmt = conn.createStatement();

     if(code.equals(""))
     {
       calculateCode(stmt);
     }

     deleteOld(stmt);

     insertNew(stmt);

     conn.close();
  
     RequestDispatcher dispatcher =
        application.getRequestDispatcher("/entry.jsp?code="+code);
     dispatcher.forward(request, response); 

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<%
  /*
  entry.jsp

  This file is used to output a single Person entry from
  the database. It also will about associated spouses
  and user comments.
  */
%>

<%@ page import="java.sql.*, java.net.*, javax.servlet.jsp.*" %>
<%@ page import="SearchBean" %> 

<jsp:useBean id="searchBean" class="SearchBean" scope="session" />
 
<jsp:setProperty name="searchBean" property="*" />

<%@ include file="outputLink.jsp" %>

<%!
   /*
      Outputs (as a HTML table) a Person based on database row
 
      r - ResultSet with row.
      out - JspWriter to client 
   */
   void outputPerson(ResultSet r, JspWriter out)
      throws Exception
   {
      String  code = r.getString("code");
      String  last = r.getString("lastName");
      String  first = r.getString("firstName");
      String  bdate = r.getString("birthDate");
      String  ddate = r.getString("deathDate");

      out.println("<table border=\"1\">");
      out.println("<tr><td>"+last+", "+first+
        "&nbsp;&nbsp;Code:="+code+
        "<br />("+bdate+" to "+
         ddate+")</td></tr>");      

      out.println("</table>");
   }

   /*
      Outputs (as a HTML table) a Parent of a Person based 
      on database row
 
      c - Database Connection
      r - ResultSet with row of Person.
      out - JspWriter to client
      type - mother or father 
   */

   void outputParent(Connection c, ResultSet r, 
      JspWriter out, String type) throws Exception
   {
      Statement stat = c.createStatement();

      out.println("<p>"+type+":</p>");
      String code = r.getString(type+"Code");

      if (code != null) 
      {
         String query= "SELECT * "+
         "FROM Person "+
         "WHERE code='"+code+"'";

         ResultSet result = stat.executeQuery(query);

         if(result.next())
         {
            out.println("<table><tr><td>");
            outputPerson(result, out);
            out.println("</td><td>");
            outputLink(result, out, "Examine Parent");
            out.println("</td></tr></table>");
         }
         result.close();
      }
   }

   /*
      Outputs the Spouses of a Person 
 
      c - Database Connection
      code - DB code of Person.
      out - JspWriter to client
      session - a string of spouses is passed as a HttpSession
                attribute 
   */
  void outputSpouses(Connection c, String code, 
      JspWriter out, HttpSession session) throws Exception
   {
     Statement stat = c.createStatement();

      out.println("<h2>Known Spouses:</h2>");
 
      if (code != null) 
      {
         String query= "SELECT * "+
         "FROM Married "+
         "WHERE (spouse1Code='"+code+"' OR "+
         "spouse2Code='"+code+"') ";

         ResultSet result = stat.executeQuery(query);

         String spouseCodes="";

         while(result.next())
         {
            String spouse1 = result.getString("spouse1Code");
            String spouse2 = result.getString("spouse2Code");
            if(code.equals(spouse2))
            {
               outputResult(c, spouse1 ,out);
               spouseCodes +=spouse1+" ";
            }     
            else
            {
               outputResult(c, spouse2, out);
               spouseCodes +=spouse2+" ";
            }
         }
         result.close();
         session.setAttribute("spouseCodes", spouseCodes);
      }
   }

   /*
      Outputs the Children of a Person 
 
      c - Database Connection
      code - DB code of Person.
      out - JspWriter to client
  
   */
  void outputChildren(Connection c, String code, 
      JspWriter out) throws Exception
   {
     Statement stat = c.createStatement();

      out.println("<h2>Known Children:</h2>");

      if (code != null) 
      {
         String query= "SELECT * "+
         "FROM Person "+
         "WHERE (fatherCode='"+code+"' OR "+
         "motherCode='"+code+"') ";

         ResultSet result = stat.executeQuery(query);

         while(result.next())
         {
            outputResult(c, result.getString("code") ,out);
         }
         result.close();
      }
   }

   /*
      Outputs a Person as a HTML link to that person 
 
      c - Database Connection
      code - DB code of Person.
      out - JspWriter to client
  
   */
   void outputResult(Connection c, String code, 
      JspWriter out) throws Exception
   {
     Statement stat = c.createStatement();

      if (code != null) 
      {
        String query= "SELECT * "+
         "FROM Person "+
         "WHERE code='"+code+"'";

         ResultSet result = stat.executeQuery(query);
         if(result.next())
            outputLink(result, out, null);
      }
   }

   /*
      Outputs descriptions (user comments) associated with
      a Person. The request, response, session are used
      because the remarks.jsp file is used to output a
      single such user comment and needs this info.

 
      c - Database Connection
      code - DB code of Person.
      out - JspWriter to client
      request - current ServletRequest
      response - current ServletResponse
      session - current HttpSession
  
   */
   void outputDescriptions(Connection c, String code, 
      JspWriter out, ServletRequest request, 
      ServletResponse response, HttpSession session) 
      throws Exception
   {
      Statement stat = c.createStatement();

      out.println("<hr /><h2>Remarks:</h2>");

      if (code != null) 
      {
         String query= "SELECT * "+
         "FROM Description "+
         "WHERE code='"+code+"'";

         ResultSet r = stat.executeQuery(query);

         ServletContext application = getServletContext();
         RequestDispatcher dispatcher =
            application.getRequestDispatcher("/remarks.jsp");
 
         while(r.next())
         {
            session.setAttribute("author", r.getString("author"));
            session.setAttribute("rating", r.getString("rating"));
            session.setAttribute("numberVotes", 
               r.getString("numberVotes"));
            session.setAttribute("comment", r.getString("comment"));
            session.setAttribute("url", r.getString("url"));

            out.flush();
            dispatcher.include(request, response); 

         }
         r.close();
      }

   }


%>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 

<head>
 
  <title>Genealogy! -  Display Entry</title>

  <meta http-equiv="Content-Type" 
        content="text/html; charset=utf-8" />
  <meta name="Authors" content="Christopher Pollett" />
  <meta name="keywords" 
        content="Chris Pollett, Christopher Pollett, Last name search,
        genealogy" />

  <link rel="stylesheet" 
   type="text/css" href="mystyles.css" title="mystyles"/>

</head>
 
<body class="contentColor">
<%@ include file="header.jsp" %>

<%
   Class.forName(application.getInitParameter("jdbc.driver"));
   Connection conn = java.sql.DriverManager.getConnection(
      application.getInitParameter("jdbc.url"),
      application.getInitParameter("jdbc.user"),
      application.getInitParameter("jdbc.password")
   );
 
   String code = searchBean.getCode();

   Statement stat = conn.createStatement();
   String query= "SELECT * "+
      "FROM Person "+
      "WHERE code='"+code+"'";

   ResultSet result = stat.executeQuery(query);

   if(result.next())
   {
   %>
   <table>
   <tr><td>&nbsp;</td><td>
   <% outputParent(conn, result,out,"father"); %></td>
   </tr>
   <tr><td> 
   <% 
   outputPerson(result, out);
   %> 
   &nbsp;&nbsp;<a href="create.jsp">Edit entry</a>
   </td></tr>
   <tr><td>&nbsp;</td>
   <td><% outputParent(conn, result,out,"mother"); %></td>
   </tr>
   </table>
   <%

   outputSpouses(conn, code, out, session);

   outputChildren(conn, code, out);

   outputDescriptions(conn, code,out, request, response, session);
   %>

   <h2>Add your own remark:</h2>

   <form action="addRemarks.jsp" method="post">

   <p> Your Name: <input type="text" name="author"
      size="20" maxlength="30"  /><br />

   Comment:<br />

   <textarea name= "comment" rows="8" cols="30">
   </textarea><br />

   Related URL:

   <input type="text" name="url"
      size="20" maxlength="50"  /><br />
   <input type="submit" name="remarks" value="Enter Remarks" />

   </p>
   </form>

   <%
   }
   else
   {%>
      <p> No entry found</p>
   <%}
   result.close();
%>

<%@ include file="footer.jsp" %>
</body>
</html>

/*
    Family.sql
    A simple database to keep track of family history. 
*/

/* 
   We begin with the database schema. Was lazy did not put in constraints.
   Not supported by mysql anyway.
*/
DROP TABLE IF EXISTS Person;
DROP TABLE IF EXISTS Married;
DROP TABLE IF EXISTS Description;

/*
  Stores records of all people  in family tree
*/
CREATE TABLE Person (
  code  INT NOT NULL, 
  firstName VARCHAR(20) NOT NULL,   
  lastName VARCHAR(20) NOT NULL,
  birthDate DATE,
  deathDate DATE,
  fatherCode INT, 
  motherCode INT,
  PRIMARY KEY(code)
); 

/*
  One person may marry in the their lifetime many others.
  So record marriages in a separate table.
*/
CREATE TABLE Married (
  spouse1Code INT NOT NULL,
  spouse2Code INT NOT NULL,
  PRIMARY KEY(spouse1Code, spouse2Code)   
);

/*
   Many users may comment on a given Person entered
   in database so we store such user comments in a
   separate table. Users can also provide a link which
   might have off-site info related to that person.
*/
CREATE TABLE Description (
  code  INT NOT NULL, 
  author VARCHAR(30) NOT NULL,
  url VARCHAR(50),
  comment VARCHAR(255),
  rating FLOAT,
  numberVotes INT,
  PRIMARY KEY(code, author) 
);


INSERT INTO Person VALUES
( '1', 'Bob', 'Smith', '1850-08-02', '1900-05-03', '2', '3');
INSERT INTO Person VALUES
( '2', 'Brad', 'Smith', '1790-05-10', '1860-11-4', null, null);
INSERT INTO Person VALUES
( '3', 'Sally', 'Parker', '1800-03-09', '1870-05-06', null, null);
INSERT INTO Person VALUES
( '4', 'Tom', 'Smith', '1870-02-10', '1940-11-5', null, null);
INSERT INTO Person VALUES
( '5', 'Tom5', 'Smith', '1870-02-10', '1940-11-5', null, null);
INSERT INTO Person VALUES
( '6', 'Tom6', 'Smith', '1870-02-10', '1940-11-5', null, null);
INSERT INTO Person VALUES
( '7', 'Tom7', 'Smith', '1870-02-10', '1940-11-5', null, null);
INSERT INTO Person VALUES
( '8', 'Tom8', 'Smith', '1870-02-10', '1940-11-5', null, null);
INSERT INTO Person VALUES
( '9', 'Tom9', 'Smith', '1870-02-10', '1940-11-5', null, null);
INSERT INTO Person VALUES
( '10', 'Tom0', 'Smith', '1870-02-10', '1940-11-5', null, null);
INSERT INTO Person VALUES
( '11', 'Tom1', 'Smith', '1870-02-10', '1940-11-5', null, null);



INSERT INTO Married VALUES
( '2', '3');

<!-- footer.jsp-->
<!-- Common footer to all files on Genealogy! site -->

  <hr />
  <table  width="100%" border="0" cellspacing="0" >
  <tr>
  <td align="left">
  <b>Last modified:</b> May 3, 2002</td>

  <td  align="right">
  <a href="about.jsp"><b>About this site...</b></a>
  </td>
  </tr>
  </table>

<!-- header.jsp --> 
<!-- Common header for all pages on Genealogy! site -->

 <table  width="100%" border="0" cellspacing="0" >
  <tr>   
  <td align="left" style="background: cyan">
  <a href="create.jsp?reset=true">
   <span class="bold">Create Entry</span></a>
  </td>
  <td align="right" style="background: cyan">
  <a href="index.jsp?reset=true">
   <span class="bold">Main Page</span>
  </a>
  </td> 
  <td align="right" style="background: cyan">
  <a href="preferences.jsp">
   <span class="bold">User Preferences</span>
  </a>
  </td>
  </tr>
  </table> 

  <h1 class="center">
  <span style="color: blue">G</span>
  <span style="color: orange">e</span>
  <span style="color: green">n</span>
  <span style="color: red">e</span>
  <span style="color: blue">a</span>
  <span style="color: red">l</span>
  <span style="color: orange">o</span>
  <span style="color: blue">g</span>
  <span style="color: red">y</span>
  <span style="color: green">!</span>
  </h1>

  <hr />

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<%
  /* 
     index.jsp
     Main page of the genealogy web site
  */
%>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 

<head> 

  <title>Genealogy! - Trace, enter and update family histories here</title>

  <meta http-equiv="Content-Type" 
        content="text/html; charset=utf-8" />
  <meta name="Authors" content="Christopher Pollett" />
  <meta name="keywords" 
        content="Chris Pollett, Christopher Pollett, genealogy" />

  <link rel="stylesheet" 
   type="text/css" href="mystyles.css" title="mystyles"/>

</head>  
 
<body class="contentColor">

<%@ page import="SearchBean" %> 

<jsp:useBean id="searchBean" class="SearchBean" scope="session" />

<jsp:setProperty name="searchBean" property="*" />

<jsp:include page="header.jsp" />

  <h3 class="center">Your place to trace, enter and update your family history.
  </h3>
  <form class="center" method="get" action="search.jsp">
    <p><span class="bold">Search someone's tree:</span><br />
      First Name:
      <input type="text" name="firstName" size="10" maxlength="20"/>
      &nbsp;&nbsp;&nbsp;Last Name:
      <input type="text" name="lastName" size="10" maxlength="20"/>

      <input type="submit" value="Submit"/><br />
       (Use maiden name for women)   
    </p>    
  </form>
 
  <table  width="100%" border="0" cellspacing="0" >
  <tr>
  <th colspan="26" align="left" >Last name index:</th></tr>
  <tr>
  <%
 

  for(char i='A';i<='Z';i++)
  {
    out.println("<td>");
    out.println("<a href=\"search.jsp?lastName="+i+"\">");
    out.println("<span class=\"bold\">"+i+"</span></a>");
    out.println("</td>");
  }
  %>
  </tr>
  <tr><td>&nbsp;</td></tr>
  </table>


<jsp:include page="footer.jsp" />
</body>
</html>

<!-- Some style used in Genealogy! program -->

.nobot {line-height: .05in; margin-bottom: .07in}
.contentColor {background: #FFFFFF; color:#000000}
a:link {color: #0000EE}
a:active {color: #FF0000}
a:visited {color: #551A8B}

.italic {font-style: italic}
.bold {font-weight: bold}
.underline {text-decoration: underline}
.code {font-family: Courier}
.center {text-align:center}


<%!
   /*
     outputLink.jsp

     is called by entry.jsp and search.jsp to output a
     summary of a given Person row together with a link
     to the full row as presented by entry.jsp
   */


   /*
     Outputs a link to display data for a Person

     r - ResultSet containing row of person
     out -- output to client
     linkText - what text should be used with link; if null
        a summary of that person is output as link text
   */
   public void outputLink(ResultSet r, JspWriter out, String linkText) 
         throws Exception
   {      
      String  code = ""+ r.getString("code");
      String  last = ""+ r.getString("lastName");
      String  first = ""+ r.getString("firstName");
      String  bdate = ""+ r.getString("birthDate");
      String  ddate = ""+ r.getString("deathDate");
      String  mcode = ""+ r.getString("motherCode");
      String  fcode = ""+ r.getString("fatherCode");
   
      out.println("<a href=\"entry.jsp?code="+
         URLEncoder.encode(code)+
         "&amp;lastName="+URLEncoder.encode(last)+ 
         "&amp;firstName="+URLEncoder.encode(first)+
         "&amp;birthDate="+URLEncoder.encode(bdate)+ 
         "&amp;deathDate="+URLEncoder.encode(ddate)+
         "&amp;motherCode="+URLEncoder.encode(mcode)+ 
         "&amp;fatherCode="+URLEncoder.encode(fcode)+
         "\">");
      if(linkText==null)
         out.println(last+", "+first+" ("+bdate.substring(0,4)+"-"+
            ddate.substring(0,4)+")"+"</a>");
      else
         out.println(linkText+"</a>");
   }
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<%
   /*
      preferences.jsp

      This file is used to allow the user to adjust
      the number of entries that are displayed
      at one time in response to a search. SearchBean
      is used to pass this information to other parts of
      the site.
   */
%>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 

<head>  

  <title>Genealogy! - Edit viewing preferences</title>

  <meta http-equiv="Content-Type" 
        content="text/html; charset=utf-8" />
  <meta name="Authors" content="Christopher Pollett" />
  <meta name="keywords" 
        content="Chris Pollett, Christopher Pollett, genealogy" />

  <link rel="stylesheet" 
   type="text/css" href="mystyles.css" title="mystyles"/>

</head>  
 
<body class="contentColor">
<%@ page import="SearchBean" %> 
<jsp:useBean id="searchBean" class="SearchBean" scope="session" />
 
<jsp:include page="header.jsp" />
<jsp:setProperty name="searchBean" property="*" />


<%!
   /*
      Global Decalarations
   */
   static int maxResultsPerPage = 50;
   static int offsetResultsPerPage = 5;
%>

<h2 class="center">Edit User Preferences</h2>
<h3 class="center">Set Mamixum Results per Page</h3>

<form class="center" action="preferences.jsp" method="get">
<p>
  <select name="resultsPerPage">
  <%
    int numResults = searchBean.getResultsPerPage(); 

    for(int i=offsetResultsPerPage; i<=maxResultsPerPage; 
       i+=offsetResultsPerPage)
    {
       out.print("<option value=\""+i+"\" ");
       if( i==numResults) out.print("selected=\"selected\" ");
       out.println(">"+i+"</option>");
    }
  %>
  </select>
  <input type="hidden" name="updated" value="true" />
  <input type="submit" value="Submit" />
</p>
</form>  

  <%
    // Show if change a preference

    if(request.getParameter("updated")!=null)
    {
       out.println("<p class=\"center\" style=\"color:red\">");
       out.println("Preference Set!</p>");
 
    }
  %>
<p class="center"><a href="index.jsp">Return to main page</a></p>
<jsp:include page="footer.jsp" />
</body>
</html>

<%@ page import="java.sql.*, java.net.*, javax.servlet.jsp.*" %>
<%@ page import="SearchBean" %> 
<%
/*
  rating.jsp

  This page is used to update a rating on a description (user comment)
  based on the value someone sent as a vote.

  The request for this page oriniates from entry.jsp, and after 
  the update, the response is forwarded to entry.jsp 
*/   
%>

<jsp:useBean id="searchBean" class="SearchBean" scope="session" />
 
<%

     String rate= request.getParameter("rating");
     if(rate !=null)
     {
     Class.forName(application.getInitParameter("jdbc.driver"));
     Connection conn = java.sql.DriverManager.getConnection(
       application.getInitParameter("jdbc.url"),
       application.getInitParameter("jdbc.user"),
       application.getInitParameter("jdbc.password")
     );
    
     float rating = Float.parseFloat(rate);
     float oldRating = Float.parseFloat(
        request.getParameter("oldRating"));
     float numberVotes = Float.parseFloat(
        request.getParameter("numberVotes"));

     Statement stat = conn.createStatement();
     String update= "UPDATE Description SET  rating='"+
      ((oldRating*numberVotes+rating)/(++numberVotes)) + "', " + 
      "numberVotes='" + numberVotes + "'"+
      " WHERE code='"+searchBean.getCode()+"' AND author='"+
      request.getParameter("author")+"'";

      stat.executeUpdate(update);
      conn.close();
 
      }
      RequestDispatcher dispatcher =
         application.getRequestDispatcher("/entry.jsp");
      dispatcher.forward(request, response); 
%>

<%
/*
  remarks.jsp
  
  This file is repeatedly included into entry.jsp. It is
  included once for each user comment on a given entry.
  the data it outputs is this comment together with how
  people rated the comment and the form to vote on the comment.
*/  

   String author = (String)session.getAttribute("author");
   String rating = (String)session.getAttribute("rating");
   String numberVotes = (String)session.getAttribute("numberVotes");
   String comment = (String)session.getAttribute("comment");
   String url = (String)session.getAttribute("url");
 
%>

<p>
<span class="bold">Remark Author: </span><%=author%><br />
(Average rating: <%=rating%>
&nbsp;&nbsp;
Number of votes: <%=numberVotes%>)
<p><span class="bold">Comment:</span><br /><%=comment%><br />
<a href="<%=url%>">URL related to comment</a></p>
<form class="nobot" method="get" action="rating.jsp">
<p class="bold">
Rate these remarks:</p>
<p>
<input type="hidden" name="author" value="<%=author%>" />
<input type="hidden" name="oldRating" value="<%=rating%>" />
<input type="hidden" name="numberVotes" value="<%=numberVotes%>" />
&nbsp;1<input type="radio" name="rating" value="1" />
&nbsp;2<input type="radio" name="rating" value="2" />
&nbsp;3<input type="radio" name="rating" value="3" />
&nbsp;4<input type="radio" name="rating" value="4" />
&nbsp;5<input type="radio" name="rating" value="5" />
<input type="submit" value="Vote!" />
</p>
</form>


<hr />

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<%
  /*
  search.jsp

  After a search request from the index.jsp page, this page
  outputs matches. It uses the user preferences to determine
  how many matches to output at one go.
  */
%>

<%@ page import="java.sql.*, java.net.*, javax.servlet.jsp.*" %>
<%@ page import="SearchBean" %> 

<jsp:useBean id="searchBean" class="SearchBean" scope="session" />
 
<jsp:setProperty name="searchBean" property="*" />

<%@ include file="outputLink.jsp" %>

<%!
   /*
     Outputs one page full of matches to the given search query.

     r- ResultSet cursor to the matches

     out - output ot the client

     searchBean - to determine how many matches we output 
   */
   public void outputAllResults(ResultSet r, JspWriter out,
      SearchBean searchBean) throws Exception
   {
      int count = 1;
      int startRow = searchBean.getStartRow();
      int perPage = searchBean.getResultsPerPage();
      int maxCount = startRow + perPage ;
      while(r.next() && count < maxCount  )
      {
         if(count >= startRow)
         {
           out.println("<p>"); 
           outputLink(r, out, null);
           out.println("</p>");
         }
         count++;
      }

      out.println("<p>");
      if(startRow > 0)
      {
         out.println("<a href=\"search.jsp?startRow="+
            (startRow-perPage)+"\" >Previous</a>&nbsp;&nbsp;");
      }

      out.println("<a href=\"index.jsp?reset=true\" >Main Page</a>&nbsp;&nbsp;");

      if(count >= maxCount)
      {
         out.println("<a href=\"search.jsp?startRow="+
            (startRow+perPage)+"\" >Next</a>");
      }
      out.println("</p>");
   }

%>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 

<head>
 
  <title>Genealogy! - Last name search</title>

  <meta http-equiv="Content-Type" 
        content="text/html; charset=utf-8" />
  <meta name="Authors" content="Christopher Pollett" />
  <meta name="keywords" 
        content="Chris Pollett, Christopher Pollett, Last name search,
        genealogy" />

  <link rel="stylesheet" 
   type="text/css" href="mystyles.css" title="mystyles"/>

</head>
 
<body class="contentColor">
<%@ include file="header.jsp" %>

   
<%
   if(searchBean.getCode() != "") 
     out.println("<h2>Search Results:</h2>");

   Class.forName(application.getInitParameter("jdbc.driver"));
   Connection conn = java.sql.DriverManager.getConnection(
      application.getInitParameter("jdbc.url"),
      application.getInitParameter("jdbc.user"),
      application.getInitParameter("jdbc.password")
   );
 

   Statement stat = conn.createStatement();
   String query= "SELECT * "+
      "FROM Person "+
      "WHERE lastName LIKE '"+searchBean.getLastName()+"%' " +
      "AND firstName LIKE '"+searchBean.getFirstName()+"%' "+
      "AND birthDate LIKE '"+searchBean.getBirthDate()+"%' "+
      "AND deathDate LIKE '"+searchBean.getDeathDate()+"%' ";

   ResultSet result = stat.executeQuery(query);

   outputAllResults(result, out, searchBean);

   result.close();
%>

<%@ include file="footer.jsp" %>
</body>
</html>

// verify.js 

/*
   verifies and create entry form of Genealogy DB
*/
function verify(form)
{

   if(form.firstName.value.length < 1 ||
      form.lastName.value.length < 1)
   {
     alert("You must supply both a first and last name");
     return false;
   }

   if(!checkDate(form.birthDate.value))
   {
     alert("Birth date must be in the format YYYY-MM-DD"+
        " where all characters are number");
     return false;
   }

   if(!checkDate(form.deathDate.value))
   {
     alert("Death date must be in the format YYYY-MM-DD"+
        " where all characters are number");
     return false;
   }
   return true;
}

/*
   Checks if date is in the format YYYY-MM-DD
*/
function checkDate(date)
{
   parts = date.split("-");

   if(parts.length !=3 || parts[0].length != 4 || 
     parts[1].length != 2 
     || parts[2].length != 2)
   {
     return false;
   }
   for(i = 0; i < parts.length; i++)
      for(j = 0; j < parts[i].length; j++)
         if(parts[i][j] < "0" || parts[i][j] > "9")
         {
            return false;
         }
  return true;
}


//SearchBean.java -- class to maintain state for Genealogy! site

/*
   This class is used to contain information about the
   current search entry as well as the user preferences for
   search results per page.
*/
public class SearchBean
{

   /**
      Default constructer

      Initally all entry information about a Person is blank
      and we output twenty rows in response to a search
   */
   public SearchBean()
   {

      setReset(null);
      resultsPerPage=20;;
   }

   /**
      Determines if entries in Bean have just been reset
 
      @return true - if have
   */
   public boolean getReset()
   {
     return reset;
   }

   /**
      Used to reset all the search parameters.
      Doesn't affect user preferences. 
 
      @param s - is ignored
   */
   public void setReset(String s)
   {
      code="";
      lastName="";
      firstName="";
      birthDate="";
      deathDate="";
      motherCode="";
      fatherCode="";
      reset=true;
      startRow = 0;
   } 

   /**
 
      @return - number of results can be displayed on a page
   */
   public int getResultsPerPage()
   {
      return resultsPerPage;
   }

   /**
      Set the number of results that can  

      @param r - the new results per page value
   */
   public void setResultsPerPage(int r)
   {
      resultsPerPage=r;
   }

   /**
 
      @return - the code of the current Person
   */
   public String getCode()
   {
      return code;
   }

   /**
     Sets what the code of the current entry will be
     If change then haven't just reset. 

      @param c - the code of the current Person
   */
   public void setCode(String c)
   {
      code=c;
      reset=false;
   }

   /**
 
      @return - the current last name
   */
   public String getLastName()
   {
      return lastName;
   }

   /**
     Sets what the last name of the current entry will be 
     If change then haven't just reset. 

      @param l - the last name of the current Person
   */
   public void setLastName(String l)
   {
      lastName=l;
      reset=false;
   }

   /**
 
      @return - the current first name
   */
   public String getFirstName()
   {
      return firstName;
   }

   /**
     Sets what the first name of the current entry will be
     If change then haven't just reset.  

      @param f - the first name of the current Person
   */
   public void setFirstName(String f)
   {
      firstName=f;
      reset=false;
   }


   /**
 
      @return - the current birth date
   */
   public String getBirthDate()
   {
      return birthDate;
   }

   /**
     Sets what the birth date of the current entry will be
     If change then haven't just reset.  

      @param b - the birth date of the current Person
   */
   public void setBirthDate(String b)
   {
      birthDate=b;
      reset=false;
   }

   /**
 
      @return - the current death date
   */
   public String getDeathDate()
   {
      return deathDate;
   }

   /**
     Sets what the death date of the current entry will be
     If change then haven't just reset.  

      @param d - the death date of the current Person
   */
   public void setDeathDate(String d)
   {
      deathDate=d;
      reset=false;
   }

   /**
 
      @return - the current father code
   */
   public String getFatherCode()
   {
      return fatherCode;
   }

   /**
     Sets what the father code of the current entry will be
     If change then haven't just reset.  

      @param f - the father code of the current Person
   */
   public void setFatherCode(String f)
   {
      fatherCode=f;
      reset=false;
   }

   /**
 
      @return - the current mother code
   */
   public String getMotherCode()
   {
      return motherCode;
   }

   /**
     Sets what the mother code of the current entry will be
     If change then haven't just reset.  

      @param m - the mother code of the current Person
   */
   public void setMotherCode(String m)
   {
      motherCode=m;
      reset=false;
   }

   /**
 
      @return - the current starting row of query results
   */
   public int getStartRow()
   {
      return startRow;
   }

   /**
     Sets the current starting row of query results
     If change then haven't just reset.  

      @param r - the new start row
   */
   public void setStartRow(int r)
   {
      startRow=r;
      reset=false;
   }


   //the next list of var's corresponds to the Person rable var's   
   String code;
 
   String lastName;
   String firstName;
   String birthDate;
   String deathDate;
   String motherCode;
   String fatherCode;

   int resultsPerPage; //number of results per page
   int startRow; //cuurent first row of query we will output on page
   boolean reset; //have we just started a new search?

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" 
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
  <context-param>
    <param-name>jdbc.driver</param-name>
    <param-value>org.gjt.mm.mysql.Driver</param-value>
  </context-param>
  <context-param>
    <param-name>jdbc.password</param-name>
    <param-value>cpollett</param-value>
  </context-param>
  <context-param>
    <param-name>jdbc.url</param-name>
    <param-value>jdbc:mysql://localhost/genealogy
    </param-value>
  </context-param>
  <context-param>
    <param-name>jdbc.username</param-name>
    <param-value>cpollett</param-value>
  </context-param>
</web-app>