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 22 23 public class ViewItem extends RubisHttpServlet 24 { 25 26 27 public int getPoolSize() 28 { 29 return Config.ViewItemPoolSize; 30 } 31 32 35 private void closeConnection(PreparedStatement stmt, Connection conn) 36 { 37 try 38 { 39 if (stmt != null) 40 stmt.close(); if (conn != null) 42 releaseConnection(conn); 43 } 44 catch (Exception ignore) 45 { 46 } 47 } 48 49 53 private void printError(String errorMsg, ServletPrinter sp) 54 { 55 sp.printHTMLheader("RUBiS ERROR: View item"); 56 sp.printHTML( 57 "<h2>We cannot process your request due to the following error :</h2><br>"); 58 sp.printHTML(errorMsg); 59 sp.printHTMLfooter(); 60 61 } 62 63 public void doGet(HttpServletRequest request, HttpServletResponse response) 64 throws IOException , ServletException 65 { 66 ServletPrinter sp = null; 67 PreparedStatement stmt = null; 68 Connection conn = null; 69 70 sp = new ServletPrinter(response, "ViewItem"); 71 ResultSet rs = null; 72 73 String value = request.getParameter("itemId"); 74 if ((value == null) || (value.equals(""))) 75 { 76 printError("No item identifier received - Cannot process the request<br>", sp); 77 return; 78 } 79 Integer itemId = new Integer (value); 80 try 82 { 83 conn = getConnection(); 84 stmt = conn.prepareStatement("SELECT * FROM items WHERE id=?"); 85 stmt.setInt(1, itemId.intValue()); 86 rs = stmt.executeQuery(); 87 } 88 catch (Exception e) 89 { 90 sp.printHTML("Failed to execute Query for item: " + e); 91 closeConnection(stmt, conn); 92 return; 93 } 94 112 try 113 { 114 if (!rs.first()) 115 { 116 sp.printHTML("<h2>This item does not exist!</h2>"); 117 closeConnection(stmt, conn); 118 return; 119 } 120 String itemName, endDate, startDate, description, sellerName; 121 float maxBid, initialPrice, buyNow, reservePrice; 122 int quantity, sellerId, nbOfBids = 0; 123 itemName = rs.getString("name"); 124 description = rs.getString("description"); 125 endDate = rs.getString("end_date"); 126 startDate = rs.getString("start_date"); 127 initialPrice = rs.getFloat("initial_price"); 128 reservePrice = rs.getFloat("reserve_price"); 129 buyNow = rs.getFloat("buy_now"); 130 quantity = rs.getInt("quantity"); 131 sellerId = rs.getInt("seller"); 132 133 maxBid = rs.getFloat("max_bid"); 134 nbOfBids = rs.getInt("nb_of_bids"); 135 if (maxBid < initialPrice) 136 maxBid = initialPrice; 137 138 PreparedStatement sellerStmt = null; 139 try 140 { 141 sellerStmt = 142 conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 143 sellerStmt.setInt(1, sellerId); 144 ResultSet sellerResult = sellerStmt.executeQuery(); 145 if (sellerResult.first()) 147 sellerName = sellerResult.getString("nickname"); 148 else 149 { 150 sp.printHTML("Unknown seller"); 151 sellerStmt.close(); 152 closeConnection(stmt, conn); 153 return; 154 } 155 sellerStmt.close(); 156 157 } 158 catch (SQLException e) 159 { 160 sp.printHTML("Failed to executeQuery for seller: " + e); 161 sellerStmt.close(); 162 closeConnection(stmt, conn); 163 return; 164 } 165 sp.printItemDescription( 166 itemId.intValue(), 167 itemName, 168 description, 169 initialPrice, 170 reservePrice, 171 buyNow, 172 quantity, 173 maxBid, 174 nbOfBids, 175 sellerName, 176 sellerId, 177 startDate, 178 endDate, 179 -1, 180 conn); 181 } 182 catch (Exception e) 183 { 184 printError("Exception getting item list: " + e + "<br>", sp); 185 } 186 closeConnection(stmt, conn); 187 sp.printHTMLfooter(); 188 } 189 190 public void doPost(HttpServletRequest request, HttpServletResponse response) 191 throws IOException , ServletException 192 { 193 doGet(request, response); 194 } 195 196 199 public void destroy() 200 { 201 super.destroy(); 202 } 203 } 204 | Popular Tags |