import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.net.*; /** * Looks up an author in the database and produces information * about him/her from the database. */ public class DescribeAuthor extends LitsearchBase { public String drawPage(HttpServletRequest request, HttpServletResponse response) throws Exception { String author = request.getParameter("name"); //If no author is specified, show the form. if (author == null || author.equals("")) { Template t = new Template(TEMPLATE_DIR + "authorsearch.html"); return t.toString(); } //get a connection from the database ConnectionWrapper wrapper = null; try { wrapper = DatabaseHook.getConnection(); Connection c = wrapper.connection(); Statement stmt = c.createStatement(); String text = ""; //see how many authors the query matches String query = "SELECT COUNT(*) FROM Author WHERE name LIKE " + formatString(author + "%"); ResultSet rs = stmt.executeQuery(query); rs.next(); int count = rs.getInt(1); if (count == 0) { //if there are none, show failure. text += "Sorry. The author " + author + " was not found."; } else if (count > 1) { //if many, show a list to choose from. text = "Authors whose last names begin with '" + author + "':
"; query = "SELECT name " + "FROM Author " + "WHERE name LIKE " + formatString(author + "%") + " " + "ORDER BY name"; rs = stmt.executeQuery(query); while (rs.next()) { text += "
" + rs.getString(1) + ""; } } else { //if excatly one, show his record in tabular format. text += "" + "\n
\n"; query = "SELECT * FROM Author WHERE name LIKE " + formatString(author + "%"); rs = stmt.executeQuery(query); if (rs.next()) { author = rs.getString("name"); text += "" + author + " (" + rs.getInt("birth") + "-" + rs.getInt("death") + ")
\n"; String desc = rs.getString("description"); if (desc != null) text += desc + "

\n"; } text += ""; query = "SELECT c.title, c.critic, c.synopsis, c.url, c.publication " + "FROM Criticism c, AuthorCriticized a " + "WHERE a.url = c.url AND a.name=" + formatString(author); rs = stmt.executeQuery(query); //look up critical analysis about this author and display in tabular format. boolean firstPass = true; while (rs.next()) { if (firstPass) { text += "\n"; firstPass = false; } String title = rs.getString("title"); String critic = rs.getString("critic"); String synopsis = rs.getString("synopsis"); String url = rs.getString("url"); String publication = rs.getString("publication"); text += "\n"; if (!critic.equals("")) text += "\n"; text += "\n"; text += "\n"; text += "\n"; } text += "
 
Critical analysis of " + author + "
" + title + "
Author: " + critic + "
Source: " + publication + "
" + synopsis + "
 
"; text += ""; text += "
Works by this author:
"; //query works that this author has written and display their titles query = "SELECT w.id, w.title " + "FROM WrittenBy, Work w " + "WHERE WrittenBy.name=" + formatString(author) + " AND w.id = WrittenBy.work ORDER BY w.title"; rs = stmt.executeQuery(query); while (rs.next()) { text += "" + rs.getString("title") + "
\n"; } text += "
\n
"; } wrapper.checkIn(); return text; } catch (SQLException e) { wrapper.checkIn(); throw e; } } }