1 package edu.rice.rubis.servlets; 2 3 import java.io.IOException ; 4 import java.sql.Connection ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 9 import javax.servlet.ServletException ; 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 13 19 20 public class ViewBidHistory extends RubisHttpServlet 21 { 22 23 24 public int getPoolSize() 25 { 26 return Config.ViewBidHistoryPoolSize; 27 } 28 29 32 private void closeConnection(PreparedStatement stmt, Connection conn) 33 { 34 try 35 { 36 if (stmt != null) 37 stmt.close(); if (conn != null) 39 releaseConnection(conn); 40 } 41 catch (Exception ignore) 42 { 43 } 44 } 45 46 47 private boolean listBids(Integer itemId, PreparedStatement stmt, Connection conn, ServletPrinter sp) 48 { 49 float bid; 50 int userId; 51 String bidderName, date; 52 ResultSet rs = null; 53 54 try 56 { 57 stmt = 58 conn.prepareStatement( 59 "SELECT * FROM bids WHERE item_id=? ORDER BY date DESC"); 60 stmt.setInt(1, itemId.intValue()); 61 rs = stmt.executeQuery(); 62 if (!rs.first()) 63 { 64 sp.printHTML( 65 "<h3>There is no bid corresponding to this item.</h3><br>"); 66 closeConnection(stmt, conn); 67 return false; 68 } 69 } 70 catch (SQLException e) 71 { 72 sp.printHTML("Exception getting bids list: " + e + "<br>"); 73 closeConnection(stmt, conn); 74 return false; 75 } 76 77 sp.printBidHistoryHeader(); 78 try 79 { 80 do 81 { 82 date = rs.getString("date"); 84 bid = rs.getFloat("bid"); 85 userId = rs.getInt("user_id"); 86 87 ResultSet urs = null; 88 try 89 { 90 stmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 91 stmt.setInt(1, userId); 92 urs = stmt.executeQuery(); 93 if (!urs.first()) 94 { 95 sp.printHTML("This user does not exist in the database.<br>"); 96 closeConnection(stmt, conn); 97 return false; 98 } 99 bidderName = urs.getString("nickname"); 100 } 101 catch (SQLException e) 102 { 103 sp.printHTML("Couldn't get bidder name: " + e + "<br>"); 104 closeConnection(stmt, conn); 105 return false; 106 } 107 sp.printBidHistory(userId, bidderName, bid, date); 108 } 109 while (rs.next()); 110 } 111 catch (SQLException e) 112 { 113 sp.printHTML("Exception getting bid: " + e + "<br>"); 114 closeConnection(stmt, conn); 115 return false; 116 } 117 sp.printBidHistoryFooter(); 118 return true; 119 } 120 121 public void doGet(HttpServletRequest request, HttpServletResponse response) 122 throws IOException , ServletException 123 { 124 doPost(request, response); 125 } 126 127 public void doPost(HttpServletRequest request, HttpServletResponse response) 128 throws IOException , ServletException 129 { 130 String value = request.getParameter("itemId"); 131 Integer itemId; 132 String itemName; 133 ResultSet rs = null; 134 ServletPrinter sp = null; 135 PreparedStatement stmt = null; 136 Connection conn = null; 137 138 sp = new ServletPrinter(response, "ViewBidHistory"); 139 140 if ((value == null) || (value.equals(""))) 141 { 142 sp.printHTMLheader("RUBiS ERROR: View bids history"); 143 sp.printHTML("<h3>You must provide an item identifier !<br></h3>"); 144 sp.printHTMLfooter(); 145 return; 146 } 147 else 148 itemId = new Integer (value); 149 if (itemId.intValue() == -1) 150 sp.printHTML("ItemId is -1: this item does not exist.<br>"); 151 152 sp.printHTMLheader("RUBiS: Bid history"); 153 154 try 156 { 157 conn = getConnection(); 158 stmt = conn.prepareStatement("SELECT name FROM items WHERE id=?"); 159 stmt.setInt(1, itemId.intValue()); 160 rs = stmt.executeQuery(); 161 } 162 catch (Exception e) 163 { 164 sp.printHTML("Failed to execute Query for item in table items: " + e); 165 closeConnection(stmt, conn); 166 return; 167 } 168 186 try 187 { 188 if (!rs.first()) 189 { 190 sp.printHTML("<h2>This item does not exist!</h2>"); 191 closeConnection(stmt, conn); 192 return; 193 } 194 itemName = rs.getString("name"); 195 sp.printHTML( 196 "<center><h3>Bid History for " + itemName + "<br></h3></center>"); 197 } 198 catch (Exception e) 199 { 200 sp.printHTML("This item does not exist (got exception: " + e + ")<br>"); 201 sp.printHTMLfooter(); 202 closeConnection(stmt, conn); 203 return; 204 } 205 206 boolean connAlive = listBids(itemId, stmt, conn, sp); 207 if (connAlive) { 210 closeConnection(stmt, conn); 211 } 212 sp.printHTMLfooter(); 213 } 214 215 218 public void destroy() 219 { 220 super.destroy(); 221 } 222 223 } 224 | Popular Tags |