1 package edu.rice.rubis.servlets; 2 3 import java.io.IOException ; 4 import java.net.URLEncoder ; 5 import java.sql.Connection ; 6 import java.sql.PreparedStatement ; 7 import java.sql.ResultSet ; 8 9 import javax.servlet.ServletException ; 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 13 23 24 public class SearchItemsByCategory extends RubisHttpServlet 25 { 26 27 28 public int getPoolSize() 29 { 30 return Config.SearchItemsByCategoryPoolSize; 31 } 32 33 36 private void closeConnection(PreparedStatement stmt, Connection conn) 37 { 38 try 39 { 40 if (stmt != null) 41 stmt.close(); if (conn != null) 43 releaseConnection(conn); 44 } 45 catch (Exception ignore) 46 { 47 } 48 } 49 50 54 private void printError(String errorMsg, ServletPrinter sp) 55 { 56 sp.printHTMLheader("RUBiS ERROR: Search Items By Category"); 57 sp.printHTML( 58 "<h2>We cannot process your request due to the following error :</h2><br>"); 59 sp.printHTML(errorMsg); 60 sp.printHTMLfooter(); 61 62 } 63 64 private void itemList( 65 Integer categoryId, 66 String categoryName, 67 int page, 68 int nbOfItems, 69 ServletPrinter sp) 70 { 71 72 PreparedStatement stmt = null; 73 Connection conn = null; 74 75 String itemName, endDate; 76 int itemId; 77 float maxBid; 78 int nbOfBids = 0; 79 ResultSet rs = null; 80 81 try 83 { 84 conn = getConnection(); 85 87 stmt = 88 conn.prepareStatement( 89 "SELECT items.name, items.id, items.end_date, items.max_bid, items.nb_of_bids, items.initial_price FROM items WHERE items.category=? AND end_date>=NOW() ORDER BY items.end_date ASC LIMIT ?,?"); 90 stmt.setInt(1, categoryId.intValue()); 91 stmt.setInt(2, page * nbOfItems); 92 stmt.setInt(3, nbOfItems); 93 rs = stmt.executeQuery(); 94 } 95 catch (Exception e) 96 { 97 sp.printHTML("Failed to executeQuery for item: " + e); 98 closeConnection(stmt, conn); 99 return; 100 } 101 try 102 { 103 if (!rs.first()) 104 { 105 if (page == 0) 106 { 107 sp.printHTML( 108 "<h2>Sorry, but there are no items available in this category !</h2>"); 109 } 110 else 111 { 112 sp.printHTML( 113 "<h2>Sorry, but there are no more items available in this category !</h2>"); 114 sp.printItemHeader(); 115 sp.printItemFooter( 116 "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByCategory?category=" 117 + categoryId 118 + "&categoryName=" 119 + URLEncoder.encode(categoryName) 120 + "&page=" 121 + (page - 1) 122 + "&nbOfItems=" 123 + nbOfItems 124 + "\">Previous page</a>", 125 ""); 126 } 127 closeConnection(stmt, conn); 128 return; 129 } 130 131 sp.printItemHeader(); 132 do 133 { 134 itemName = rs.getString("name"); 135 itemId = rs.getInt("id"); 136 endDate = rs.getString("end_date"); 137 maxBid = rs.getFloat("max_bid"); 138 nbOfBids = rs.getInt("nb_of_bids"); 139 float initialPrice = rs.getFloat("initial_price"); 140 if (maxBid < initialPrice) 141 maxBid = initialPrice; 142 sp.printItem(itemName, itemId, maxBid, nbOfBids, endDate); 143 } 144 while (rs.next()); 145 if (page == 0) 146 { 147 sp.printItemFooter( 148 "", 149 "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByCategory?category=" 150 + categoryId 151 + "&categoryName=" 152 + URLEncoder.encode(categoryName) 153 + "&page=" 154 + (page + 1) 155 + "&nbOfItems=" 156 + nbOfItems 157 + "\">Next page</a>"); 158 } 159 else 160 { 161 sp.printItemFooter( 162 "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByCategory?category=" 163 + categoryId 164 + "&categoryName=" 165 + URLEncoder.encode(categoryName) 166 + "&page=" 167 + (page - 1) 168 + "&nbOfItems=" 169 + nbOfItems 170 + "\">Previous page</a>", 171 "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByCategory?category=" 172 + categoryId 173 + "&categoryName=" 174 + URLEncoder.encode(categoryName) 175 + "&page=" 176 + (page + 1) 177 + "&nbOfItems=" 178 + nbOfItems 179 + "\">Next page</a>"); 180 } 181 closeConnection(stmt, conn); 183 } 184 catch (Exception e) 185 { 186 printError("Exception getting item list: " + e + "<br>", sp); 187 closeConnection(stmt, conn); 196 } 197 } 198 199 public void doGet(HttpServletRequest request, HttpServletResponse response) 200 throws IOException , ServletException 201 { 202 Integer page; 203 Integer nbOfItems; 204 String value = request.getParameter("category"); 205 ; 206 Integer categoryId; 207 String categoryName = request.getParameter("categoryName"); 208 209 ServletPrinter sp = null; 210 sp = new ServletPrinter(response, "SearchItemsByCategory"); 211 212 if ((value == null) || (value.equals(""))) 213 { 214 printError("You must provide a category identifier!<br>", sp); 215 return; 216 } 217 else 218 categoryId = new Integer (value); 219 220 value = request.getParameter("page"); 221 if ((value == null) || (value.equals(""))) 222 page = new Integer (0); 223 else 224 page = new Integer (value); 225 226 value = request.getParameter("nbOfItems"); 227 if ((value == null) || (value.equals(""))) 228 nbOfItems = new Integer (25); 229 else 230 nbOfItems = new Integer (value); 231 232 if (categoryName == null) 233 { 234 sp.printHTMLheader("RUBiS: Missing category name"); 235 sp.printHTML("<h2>Items in this category</h2><br><br>"); 236 } 237 else 238 { 239 sp.printHTMLheader("RUBiS: Items in category " + categoryName); 240 sp.printHTML("<h2>Items in category " + categoryName + "</h2><br><br>"); 241 } 242 243 itemList(categoryId, categoryName, page.intValue(), nbOfItems.intValue(), sp); 244 sp.printHTMLfooter(); 245 } 246 247 public void doPost(HttpServletRequest request, HttpServletResponse response) 248 throws IOException , ServletException 249 { 250 doGet(request, response); 251 } 252 253 256 public void destroy() 257 { 258 super.destroy(); 259 } 260 } 261 | Popular Tags |