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 29 30 public class StoreBuyNow extends RubisHttpServlet 31 { 32 33 public int getPoolSize() 34 { 35 return Config.StoreBuyNowPoolSize; 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: StoreBuyNow"); 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 69 77 public void doGet(HttpServletRequest request, HttpServletResponse response) 78 throws IOException , ServletException 79 { 80 doPost(request, response); 81 } 82 83 91 public void doPost(HttpServletRequest request, HttpServletResponse response) 92 throws IOException , ServletException 93 { 94 Integer userId; Integer itemId; int maxQty; int qty; ServletPrinter sp = null; 102 PreparedStatement stmt = null; 103 Connection conn = null; 104 105 sp = new ServletPrinter(response, "StoreBuyNow"); 106 107 108 109 String value = request.getParameter("userId"); 110 if ((value == null) || (value.equals(""))) 111 { 112 printError("<h3>You must provide a user identifier !<br></h3>", sp); 113 return; 114 } 115 else 116 userId = new Integer (value); 117 118 value = request.getParameter("itemId"); 119 if ((value == null) || (value.equals(""))) 120 { 121 printError("<h3>You must provide an item identifier !<br></h3>", sp); 122 return; 123 } 124 else 125 itemId = new Integer (value); 126 127 value = request.getParameter("maxQty"); 128 if ((value == null) || (value.equals(""))) 129 { 130 printError("<h3>You must provide a maximum quantity !<br></h3>", sp); 131 return; 132 } 133 else 134 { 135 Integer foo = new Integer (value); 136 maxQty = foo.intValue(); 137 } 138 139 value = request.getParameter("qty"); 140 if ((value == null) || (value.equals(""))) 141 { 142 printError("<h3>You must provide a quantity !<br></h3>", sp); 143 return; 144 } 145 else 146 { 147 Integer foo = new Integer (value); 148 qty = foo.intValue(); 149 } 150 151 152 if (qty > maxQty) 153 { 154 printError( 155 "<h3>You cannot request " 156 + qty 157 + " items because only " 158 + maxQty 159 + " are proposed !<br></h3>", sp); 160 return; 161 } 162 String now = TimeManagement.currentDateToString(); 163 try 165 { 166 int quantity; 167 conn = getConnection(); 168 conn.setAutoCommit(false); 169 stmt = 170 conn.prepareStatement( 171 "SELECT quantity, end_date FROM items WHERE id=?"); 172 stmt.setInt(1, itemId.intValue()); 173 ResultSet irs = stmt.executeQuery(); 174 if (!irs.first()) 175 { 176 conn.rollback(); 177 printError("This item does not exist in the database.", sp); 178 closeConnection(stmt, conn); 179 return; 180 } 181 quantity = irs.getInt("quantity"); 182 quantity = quantity - qty; 183 stmt.close(); 184 if (quantity == 0) 185 { 186 stmt = 187 conn.prepareStatement( 188 "UPDATE items SET end_date=?, quantity=? WHERE id=?"); 189 stmt.setString(1, now); 190 stmt.setInt(2, quantity); 191 stmt.setInt(3, itemId.intValue()); 192 stmt.executeUpdate(); 193 stmt.close(); 194 } 195 else 196 { 197 stmt = conn.prepareStatement("UPDATE items SET quantity=? WHERE id=?"); 198 stmt.setInt(1, quantity); 199 stmt.setInt(2, itemId.intValue()); 200 stmt.executeUpdate(); 201 stmt.close(); 202 } 203 } 204 catch (SQLException e) 205 { 206 sp.printHTML("Failed to execute Query for the item: " + e + "<br>"); 207 try 208 { 209 conn.rollback(); 210 closeConnection(stmt, conn); 211 } 212 catch (Exception se) 213 { 214 printError("Transaction rollback failed: " + e, sp); 215 closeConnection(stmt, conn); 216 } 217 return; 218 } 219 try 220 { 221 stmt = 222 conn.prepareStatement( 223 "INSERT INTO buy_now VALUES (NULL, \"" 224 + userId 225 + "\", \"" 226 + itemId 227 + "\", \"" 228 + qty 229 + "\", \"" 230 + now 231 + "\")"); 232 stmt.executeUpdate(); 233 234 conn.commit(); 235 sp.printHTMLheader("RUBiS: BuyNow result"); 236 if (qty == 1) 237 sp.printHTML( 238 "<center><h2>Your have successfully bought this item.</h2></center>\n"); 239 else 240 sp.printHTML( 241 "<center><h2>Your have successfully bought these items.</h2></center>\n"); 242 } 243 catch (Exception e) 244 { 245 sp.printHTML( 246 "Error while storing the BuyNow (got exception: " + e + ")<br>"); 247 try 248 { 249 conn.rollback(); 250 closeConnection(stmt, conn); 251 } 252 catch (Exception se) 253 { 254 printError("Transaction rollback failed: " + e + "<br>", sp); 255 } 256 return; 257 } 258 closeConnection(stmt, conn); 259 sp.printHTMLfooter(); 260 } 261 262 265 public void destroy() 266 { 267 super.destroy(); 268 } 269 270 } 271 | Popular Tags |