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 25 26 public class PutBid extends RubisHttpServlet 27 { 28 29 30 public int getPoolSize() 31 { 32 return Config.PutBidPoolSize; 33 } 34 35 38 private void closeConnection(PreparedStatement stmt, Connection conn) 39 { 40 try 41 { 42 if (stmt != null) 43 stmt.close(); if (conn != null) 45 releaseConnection(conn); 46 47 } 48 catch (Exception ignore) 49 { 50 } 51 } 52 53 57 private void printError(String errorMsg, ServletPrinter sp) 58 { 59 sp.printHTMLheader("RUBiS ERROR: PutBid"); 60 sp.printHTML( 61 "<h2>Your request has not been processed due to the following error :</h2><br>"); 62 sp.printHTML(errorMsg); 63 sp.printHTMLfooter(); 64 65 } 66 67 public void doGet(HttpServletRequest request, HttpServletResponse response) 68 throws IOException , ServletException 69 { 70 ServletPrinter sp = null; 71 72 String itemStr = request.getParameter("itemId"); 73 String name = request.getParameter("nickname"); 74 String pass = request.getParameter("password"); 75 sp = new ServletPrinter(response, "PubBid"); 76 77 if ((itemStr == null) 78 || (itemStr.equals("")) 79 || (name == null) 80 || (name.equals("")) 81 || (pass == null) 82 || (pass.equals(""))) 83 { 84 printError("Item id, name and password are required - Cannot process the request<br>", sp); 85 return; 86 } 87 Integer itemId = new Integer (itemStr); 88 89 PreparedStatement stmt = null; 90 Connection conn = null; 91 conn = getConnection(); 92 Auth auth = new Auth(conn, sp); 94 int userId = auth.authenticate(name, pass); 95 if (userId == -1) 96 { 97 printError(" You don't have an account on RUBiS!<br>You have to register first.<br>", sp); 98 closeConnection(stmt, conn); 99 return; 100 } 101 102 String itemName, endDate, startDate, description, sellerName; 104 float maxBid, initialPrice, buyNow, reservePrice; 105 int quantity, sellerId, nbOfBids = 0; 106 ResultSet rs = null; 107 try 108 { 109 stmt = conn.prepareStatement("SELECT * FROM items WHERE id=?"); 110 stmt.setInt(1, itemId.intValue()); 111 rs = stmt.executeQuery(); 112 } 113 catch (Exception e) 114 { 115 printError("Failed to execute Query for item: " + e, sp); 116 closeConnection(stmt, conn); 117 return; 118 } 119 try 120 { 121 if (!rs.first()) 122 { 123 printError("<h2>This item does not exist!</h2>", sp); 124 closeConnection(stmt, conn); 125 return; 126 } 127 itemName = rs.getString("name"); 128 description = rs.getString("description"); 129 endDate = rs.getString("end_date"); 130 startDate = rs.getString("start_date"); 131 initialPrice = rs.getFloat("initial_price"); 132 reservePrice = rs.getFloat("reserve_price"); 133 buyNow = rs.getFloat("buy_now"); 134 quantity = rs.getInt("quantity"); 135 sellerId = rs.getInt("seller"); 136 137 PreparedStatement sellerStmt = null; 138 try 139 { 140 sellerStmt = 141 conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 142 sellerStmt.setInt(1, sellerId); 143 ResultSet sellerResult = sellerStmt.executeQuery(); 144 if (sellerResult.first()) 146 sellerName = sellerResult.getString("nickname"); 147 else 148 { 149 printError("Unknown seller", sp); 150 sellerStmt.close(); 151 closeConnection(stmt, conn); 152 return; 153 } 154 sellerStmt.close(); 155 156 } 157 catch (SQLException e) 158 { 159 printError("Failed to executeQuery for seller: " + e, sp); 160 sellerStmt.close(); 161 closeConnection(stmt, conn); 162 return; 163 } 164 PreparedStatement maxBidStmt = null; 165 try 166 { 167 maxBidStmt = 168 conn.prepareStatement( 169 "SELECT MAX(bid) AS bid FROM bids WHERE item_id=?"); 170 maxBidStmt.setInt(1, itemId.intValue()); 171 ResultSet maxBidResult = maxBidStmt.executeQuery(); 172 if (maxBidResult.first()) 174 maxBid = maxBidResult.getFloat("bid"); 175 else 176 maxBid = initialPrice; 177 maxBidStmt.close(); 178 } 179 catch (SQLException e) 180 { 181 printError("Failed to executeQuery for max bid: " + e, sp); 182 maxBidStmt.close(); 183 closeConnection(stmt, conn); 184 return; 185 } 186 PreparedStatement nbStmt = null; 187 try 188 { 189 nbStmt = 190 conn.prepareStatement( 191 "SELECT COUNT(*) AS bid FROM bids WHERE item_id=?"); 192 nbStmt.setInt(1, itemId.intValue()); 193 ResultSet nbResult = nbStmt.executeQuery(); 194 if (nbResult.first()) 196 nbOfBids = nbResult.getInt("bid"); 197 nbStmt.close(); 198 } 199 catch (SQLException e) 200 { 201 printError("Failed to executeQuery for number of bids: " + e, sp); 202 nbStmt.close(); 203 closeConnection(stmt, conn); 204 return; 205 } 206 sp.printItemDescription( 207 itemId.intValue(), 208 itemName, 209 description, 210 initialPrice, 211 reservePrice, 212 buyNow, 213 quantity, 214 maxBid, 215 nbOfBids, 216 sellerName, 217 sellerId, 218 startDate, 219 endDate, 220 userId, 221 conn); 222 } 223 catch (Exception e) 224 { 225 printError("Exception getting item list: " + e + "<br>", sp); 226 closeConnection(stmt, conn); 227 } 228 closeConnection(stmt, conn); 229 sp.printHTMLfooter(); 230 } 231 232 public void doPost(HttpServletRequest request, HttpServletResponse response) 233 throws IOException , ServletException 234 { 235 doGet(request, response); 236 } 237 238 241 public void destroy() 242 { 243 super.destroy(); 244 } 245 } 246 | Popular Tags |