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 24 25 public class BuyNow extends RubisHttpServlet 26 { 27 28 29 public int getPoolSize() 30 { 31 return Config.BuyNowPoolSize; 32 } 33 34 37 private void closeConnection(PreparedStatement stmt, Connection conn) 38 { 39 try 40 { 41 if (stmt != null) 42 stmt.close(); if (conn != null) 44 releaseConnection(conn); 45 } 46 catch (Exception ignore) 47 { 48 } 49 } 50 51 55 private void printError(String errorMsg, ServletPrinter sp) 56 { 57 sp.printHTMLheader("RUBiS ERROR: Buy now"); 58 sp.printHTML( 59 "<h2>Your request has not been processed due to the following error :</h2><br>"); 60 sp.printHTML(errorMsg); 61 sp.printHTMLfooter(); 62 } 63 64 72 public void doGet(HttpServletRequest request, HttpServletResponse response) 73 throws IOException , ServletException  74 { 75 ServletPrinter sp = null; 76 77 String itemStr = request.getParameter("itemId"); 78 String name = request.getParameter("nickname"); 79 String pass = request.getParameter("password"); 80 sp = new ServletPrinter(response, "BuyNow"); 81 82 if ((itemStr == null) 83 || (itemStr.equals("")) 84 || (name == null) 85 || (name.equals("")) 86 || (pass == null) 87 || (pass.equals(""))) 88 { 89 printError("Item id, name and password are required - Cannot process the request<br>", sp); 90 return; 91 } 92 PreparedStatement stmt = null; 93 Connection conn = null; 94 conn = getConnection(); 96 Auth auth = new Auth(conn, sp); 97 int userId = auth.authenticate(name, pass); 98 if (userId == -1) 99 { 100 sp.printHTML("name: " + name + "<br>"); 101 sp.printHTML("pwd: " + pass + "<br>"); 102 printError(" You don't have an account on RUBiS!<br>You have to register first.<br>", sp); 103 closeConnection(stmt, conn); 104 return; 105 } 106 Integer itemId = new Integer (itemStr); 107 try 109 { 110 stmt = conn.prepareStatement("SELECT * FROM items WHERE id=?"); 111 stmt.setInt(1, itemId.intValue()); 112 ResultSet irs = stmt.executeQuery(); 113 if (!irs.first()) 114 { 115 printError("This item does not exist in the database.", sp); 116 closeConnection(stmt, conn); 117 return; 118 } 119 120 String itemName = irs.getString("name"); 121 String description = irs.getString("description"); 122 String startDate = irs.getString("start_date"); 123 String endDate = irs.getString("end_date"); 124 float buyNow = irs.getFloat("buy_now"); 125 int quantity = irs.getInt("quantity"); 126 int sellerId = irs.getInt("seller"); 127 stmt.close(); 128 String sellerName = null; 129 try 130 { 131 stmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 132 stmt.setInt(1, sellerId); 133 ResultSet srs = stmt.executeQuery(); 134 if (!srs.first()) 135 { 136 printError("This user does not exist in the database.", sp); 137 closeConnection(stmt, conn); 138 return; 139 } 140 sellerName = srs.getString("nickname"); 141 } 142 catch (SQLException s) 143 { 144 printError("Failed to execute Query for seller: " + s, sp); 145 closeConnection(stmt, conn); 146 return; 147 } 148 sp.printItemDescriptionToBuyNow( 150 itemId.intValue(), 151 itemName, 152 description, 153 buyNow, 154 quantity, 155 sellerId, 156 sellerName, 157 startDate, 158 endDate, 159 userId); 160 161 } 162 catch (SQLException e) 163 { 164 printError("Failed to execute Query for item: " + e, sp); 165 closeConnection(stmt, conn); 166 return; 167 } 168 sp.printHTMLfooter(); 169 closeConnection(stmt, conn); 170 171 187 } 188 189 197 public void doPost(HttpServletRequest request, HttpServletResponse response) 198 throws IOException , ServletException  199 { 200 doGet(request, response); 201 } 202 203 206 public void destroy() 207 { 208 super.destroy(); 209 } 210 } 211 | Popular Tags |