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 8 import javax.servlet.ServletException ; 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 11 12 28 29 public class StoreBid extends RubisHttpServlet 30 { 31 32 33 public int getPoolSize() 34 { 35 return Config.StoreBidPoolSize; 36 } 37 38 41 private void closeConnection(PreparedStatement stmt, Connection conn) 42 { 43 try 44 { 45 if (stmt != null) 46 stmt.close(); if (conn != null) 48 releaseConnection(conn); 49 } 50 catch (Exception ignore) 51 { 52 } 53 } 54 55 59 private void printError(String errorMsg, ServletPrinter sp) 60 { 61 sp.printHTMLheader("RUBiS ERROR: StoreBid"); 62 sp.printHTML( 63 "<h2>Your request has not been processed due to the following error :</h2><br>"); 64 sp.printHTML(errorMsg); 65 sp.printHTMLfooter(); 66 } 67 68 76 public void doGet(HttpServletRequest request, HttpServletResponse response) 77 throws IOException , ServletException 78 { 79 doPost(request, response); 80 } 81 82 90 public void doPost(HttpServletRequest request, HttpServletResponse response) 91 throws IOException , ServletException 92 { 93 Integer userId; Integer itemId; float minBid; float bid; float maxBid; int maxQty; int qty; ServletPrinter sp = null; 101 PreparedStatement stmt = null; 102 Connection conn = null; 103 104 sp = new ServletPrinter(response, "StoreBid"); 105 106 107 108 String value = request.getParameter("userId"); 109 if ((value == null) || (value.equals(""))) 110 { 111 printError("<h3>You must provide a user identifier !<br></h3>", sp); 112 return; 113 } 114 else 115 userId = new Integer (value); 116 117 value = request.getParameter("itemId"); 118 if ((value == null) || (value.equals(""))) 119 { 120 printError("<h3>You must provide an item identifier !<br></h3>", sp); 121 return; 122 } 123 else 124 itemId = new Integer (value); 125 126 value = request.getParameter("minBid"); 127 if ((value == null) || (value.equals(""))) 128 { 129 printError("<h3>You must provide a minimum bid !<br></h3>", sp); 130 return; 131 } 132 else 133 { 134 Float foo = new Float (value); 135 minBid = foo.floatValue(); 136 } 137 138 value = request.getParameter("bid"); 139 if ((value == null) || (value.equals(""))) 140 { 141 printError("<h3>You must provide a bid !<br></h3>", sp); 142 return; 143 } 144 else 145 { 146 Float foo = new Float (value); 147 bid = foo.floatValue(); 148 } 149 150 value = request.getParameter("maxBid"); 151 if ((value == null) || (value.equals(""))) 152 { 153 printError("<h3>You must provide a maximum bid !<br></h3>", sp); 154 return; 155 } 156 else 157 { 158 Float foo = new Float (value); 159 maxBid = foo.floatValue(); 160 } 161 162 value = request.getParameter("maxQty"); 163 if ((value == null) || (value.equals(""))) 164 { 165 printError("<h3>You must provide a maximum quantity !<br></h3>", sp); 166 return; 167 } 168 else 169 { 170 Integer foo = new Integer (value); 171 maxQty = foo.intValue(); 172 } 173 174 value = request.getParameter("qty"); 175 if ((value == null) || (value.equals(""))) 176 { 177 printError("<h3>You must provide a quantity !<br></h3>", sp); 178 return; 179 } 180 else 181 { 182 Integer foo = new Integer (value); 183 qty = foo.intValue(); 184 } 185 186 187 188 if (qty > maxQty) 189 { 190 printError( 191 "<h3>You cannot request " 192 + qty 193 + " items because only " 194 + maxQty 195 + " are proposed !<br></h3>", sp); 196 return; 197 } 198 if (bid < minBid) 199 { 200 printError( 201 "<h3>Your bid of $" 202 + bid 203 + " is not acceptable because it is below the $" 204 + minBid 205 + " minimum bid !<br></h3>", sp); 206 return; 207 } 208 if (maxBid < minBid) 209 { 210 printError( 211 "<h3>Your maximum bid of $" 212 + maxBid 213 + " is not acceptable because it is below the $" 214 + minBid 215 + " minimum bid !<br></h3>", sp); 216 return; 217 } 218 if (maxBid < bid) 219 { 220 printError( 221 "<h3>Your maximum bid of $" 222 + maxBid 223 + " is not acceptable because it is below your current bid of $" 224 + bid 225 + " !<br></h3>", sp); 226 return; 227 } 228 try 229 { 230 conn = getConnection(); 231 conn.setAutoCommit(false); 232 String now = TimeManagement.currentDateToString(); 233 stmt = 234 conn.prepareStatement( 235 "INSERT INTO bids VALUES (NULL, \"" 236 + userId 237 + "\", \"" 238 + itemId 239 + "\", \"" 240 + qty 241 + "\", \"" 242 + bid 243 + "\", \"" 244 + maxBid 245 + "\", \"" 246 + now 247 + "\")"); 248 stmt.executeUpdate(); 249 stmt.close(); 250 PreparedStatement update = null; 252 try 253 { 254 stmt = 255 conn.prepareStatement( 256 "SELECT nb_of_bids, max_bid FROM items WHERE id=?"); 257 stmt.setInt(1, itemId.intValue()); 258 ResultSet rs = stmt.executeQuery(); 259 if (rs.first()) 260 { 261 262 int nbOfBids = rs.getInt("nb_of_bids"); 263 nbOfBids++; 264 float oldMaxBid = rs.getFloat("max_bid"); 265 if (bid > oldMaxBid) 266 { 267 oldMaxBid = bid; 268 update = 269 conn.prepareStatement( 270 "UPDATE items SET max_bid=?, nb_of_bids=? WHERE id=?"); 271 update.setFloat(1, maxBid); 272 update.setInt(2, nbOfBids); 273 update.setInt(3, itemId.intValue()); 274 update.executeUpdate(); 275 update.close(); 276 } 277 else 278 { 279 update = 280 conn.prepareStatement("UPDATE items SET nb_of_bids=? WHERE id=?"); 281 update.setInt(1, nbOfBids); 282 update.setInt(2, itemId.intValue()); 283 update.executeUpdate(); 284 update.close(); 285 } 286 287 } 288 else 289 { 290 conn.rollback(); 291 printError("Couldn't find the item.", sp); 292 closeConnection(stmt, conn); 293 return; 294 } 295 } 296 catch (Exception ex) 297 { 298 conn.rollback(); 299 printError("Failed to update nb of bids and max bid: " + ex, sp); 300 if (update != null) 301 update.close(); 302 closeConnection(stmt, conn); 303 return; 304 } 305 sp.printHTMLheader("RUBiS: Bidding result"); 306 sp.printHTML( 307 "<center><h2>Your bid has been successfully processed.</h2></center>\n"); 308 conn.commit(); 309 closeConnection(stmt, conn); 310 } 311 catch (Exception e) 312 { 313 sp.printHTML( 314 "Error while storing the bid (got exception: " + e + ")<br>"); 315 try 316 { 317 conn.rollback(); 318 closeConnection(stmt, conn); 319 } 320 catch (Exception se) 321 { 322 printError("Transaction rollback failed: " + e, sp); 323 } 324 return; 325 } 326 sp.printHTMLfooter(); 327 } 328 329 332 public void destroy() 333 { 334 super.destroy(); 335 } 336 337 } 338 | Popular Tags |