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 17 public class SearchItemsByRegion extends RubisHttpServlet 18 { 19 20 public int getPoolSize() 21 { 22 return Config.SearchItemsByRegionPoolSize; 23 } 24 25 28 private void closeConnection(PreparedStatement stmt, Connection conn) 29 { 30 try 31 { 32 if (stmt != null) 33 stmt.close(); if (conn != null) 35 releaseConnection(conn); 36 } 37 catch (Exception ignore) 38 { 39 } 40 } 41 42 46 private void printError(String errorMsg, ServletPrinter sp) 47 { 48 sp.printHTMLheader("RUBiS ERROR: SearchItemsByRegion"); 49 sp.printHTML( 50 "<h2>Your request has not been processed due to the following error :</h2><br>"); 51 sp.printHTML(errorMsg); 52 sp.printHTMLfooter(); 53 54 } 55 56 57 private void itemList( 58 Integer categoryId, 59 Integer regionId, 60 int page, 61 int nbOfItems, 62 ServletPrinter sp) 63 { 64 String itemName, endDate; 65 int itemId, nbOfBids = 0; 66 float maxBid; 67 ResultSet rs = null; 68 PreparedStatement stmt = null; 69 Connection conn = null; 70 71 try 73 { 74 conn = getConnection(); 75 stmt = 76 conn.prepareStatement( 77 "SELECT items.name, items.id, items.end_date, items.max_bid, items.nb_of_bids, items.initial_price FROM items,users WHERE items.category=? AND items.seller=users.id AND users.region=? AND end_date>=NOW() ORDER BY items.end_date ASC LIMIT ?,?"); 78 stmt.setInt(1, categoryId.intValue()); 79 stmt.setInt(2, regionId.intValue()); 80 stmt.setInt(3, page * nbOfItems); 81 stmt.setInt(4, nbOfItems); 82 rs = stmt.executeQuery(); 83 } 84 catch (Exception e) 85 { 86 sp.printHTML("Failed to execute Query for items in region: " + e); 87 closeConnection(stmt, conn); 88 return; 89 } 90 try 91 { 92 if (!rs.first()) 93 { 94 if (page == 0) 95 { 96 sp.printHTML( 97 "<h3>Sorry, but there is no items in this category for this region.</h3><br>"); 98 } 99 else 100 { 101 sp.printHTML( 102 "<h3>Sorry, but there is no more items in this category for this region.</h3><br>"); 103 sp.printItemHeader(); 104 sp.printItemFooter( 105 "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByRegion?category=" 106 + categoryId 107 + "®ion=" 108 + regionId 109 + "&page=" 110 + (page - 1) 111 + "&nbOfItems=" 112 + nbOfItems 113 + "\">Previous page</a>", 114 ""); 115 } 116 closeConnection(stmt, conn); 117 return; 118 } 119 120 sp.printItemHeader(); 121 do 122 { 123 itemName = rs.getString("name"); 124 itemId = rs.getInt("id"); 125 endDate = rs.getString("end_date"); 126 maxBid = rs.getFloat("max_bid"); 127 nbOfBids = rs.getInt("nb_of_bids"); 128 float initialPrice = rs.getFloat("initial_price"); 129 if (maxBid < initialPrice) 130 maxBid = initialPrice; 131 sp.printItem(itemName, itemId, maxBid, nbOfBids, endDate); 132 } 133 while (rs.next()); 134 if (page == 0) 135 { 136 sp.printItemFooter( 137 "", 138 "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByRegion?category=" 139 + categoryId 140 + "®ion=" 141 + regionId 142 + "&page=" 143 + (page + 1) 144 + "&nbOfItems=" 145 + nbOfItems 146 + "\">Next page</a>"); 147 } 148 else 149 { 150 sp.printItemFooter( 151 "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByRegion?category=" 152 + categoryId 153 + "®ion=" 154 + regionId 155 + "&page=" 156 + (page - 1) 157 + "&nbOfItems=" 158 + nbOfItems 159 + "\">Previous page</a>", 160 "<a HREF=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.SearchItemsByRegion?category=" 161 + categoryId 162 + "®ion=" 163 + regionId 164 + "&page=" 165 + (page + 1) 166 + "&nbOfItems=" 167 + nbOfItems 168 + "\">Next page</a>"); 169 } 170 closeConnection(stmt, conn); 171 } 172 catch (Exception e) 173 { 174 sp.printHTML("Exception getting item list: " + e + "<br>"); 175 closeConnection(stmt, conn); 176 } 177 } 178 179 181 public void doGet(HttpServletRequest request, HttpServletResponse response) 182 throws IOException , ServletException 183 { 184 Integer categoryId, regionId; 185 Integer page; 186 Integer nbOfItems; 187 188 ServletPrinter sp = null; 189 sp = new ServletPrinter(response, "SearchItemsByRegion"); 190 191 String value = request.getParameter("category"); 192 if ((value == null) || (value.equals(""))) 193 { 194 printError("You must provide a category!<br>", sp); 195 return; 196 } 197 else 198 categoryId = new Integer (value); 199 200 value = request.getParameter("region"); 201 if ((value == null) || (value.equals(""))) 202 { 203 printError("You must provide a region!<br>", sp); 204 return; 205 } 206 else 207 regionId = new Integer (value); 208 209 value = request.getParameter("page"); 210 if ((value == null) || (value.equals(""))) 211 page = new Integer (0); 212 else 213 page = new Integer (value); 214 215 value = request.getParameter("nbOfItems"); 216 if ((value == null) || (value.equals(""))) 217 nbOfItems = new Integer (25); 218 else 219 nbOfItems = new Integer (value); 220 221 sp.printHTMLheader("RUBiS: Search items by region"); 222 itemList(categoryId, regionId, page.intValue(), nbOfItems.intValue(), sp); 223 sp.printHTMLfooter(); 224 } 225 226 public void doPost(HttpServletRequest request, HttpServletResponse response) 227 throws IOException , ServletException 228 { 229 doGet(request, response); 230 } 231 232 235 public void destroy() 236 { 237 super.destroy(); 238 } 239 } 240 | Popular Tags |