1 24 25 package edu.rice.rubbos.servlets; 26 27 import java.io.IOException ; 28 import java.net.URLEncoder ; 29 import java.sql.Connection ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 33 import javax.servlet.ServletException ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 public class Search extends RubbosHttpServlet 38 { 39 40 public int getPoolSize() 41 { 42 return Config.BrowseCategoriesPoolSize; 43 } 44 45 private void closeConnection(PreparedStatement stmt, Connection conn) 46 { 47 try 48 { 49 if (stmt != null) 50 stmt.close(); } 52 catch (Exception ignore) 53 { 54 } 55 56 try 57 { 58 if (conn != null) 59 releaseConnection(conn); 60 } 61 catch (Exception ignore) 62 { 63 } 64 65 } 66 67 68 public void doGet(HttpServletRequest request, HttpServletResponse response) 69 throws IOException , ServletException 70 { 71 ServletPrinter sp = null; 72 PreparedStatement stmt = null; 73 Connection conn = null; 74 75 String testtype, search, testpage, testnbOfStories, table, title = null; 76 String comment_table; 77 int page = 0, type, nbOfStories = 0; 78 ResultSet rs = null; 79 80 sp = new ServletPrinter(response, "Search"); 81 82 testtype = request.getParameter("type"); 83 testnbOfStories = request.getParameter("nbOfStories"); 84 search = request.getParameter("search"); 85 testpage = request.getParameter("page"); 86 87 if (testtype == null) 88 { 89 type = 0; 90 } 91 else 92 type = (Integer.valueOf(request.getParameter("type"))).intValue(); 93 94 if (testpage == null) 95 { 96 page = 0; 97 } 98 else 99 page = (Integer.valueOf(request.getParameter("page"))).intValue(); 100 101 if (testnbOfStories != null) 102 { 103 nbOfStories = 25; 104 } 105 106 if (search == null) 107 { 108 search = request.getParameter("search"); 109 } 110 111 if (testnbOfStories == null) 112 nbOfStories = 25; 113 114 sp.printHTMLheader("RUBBoS search"); 115 116 sp 118 .printHTML("<form action=\"/rubbos/servlet/edu.rice.rubbos.servlets.Search\" method=POST>\n" 119 + "<center><table>\n" 120 + "<tr><td><b>Search</b><td><input type=text size=50 name=search value=" 121 + search + ">\n" + "<tr><td><b>in</b><td><SELECT name=type>\n"); 122 if (type == 0) 123 { 124 sp.printHTML("<OPTION selected value=\"0\">Stories</OPTION>\n"); 125 table = "stories"; 126 title = "Stories"; 127 } 128 else 129 sp.printHTML("<OPTION value=\"0\">Stories</OPTION>\n"); 130 if (type == 1) 131 { 132 sp.printHTML("<OPTION selected value=\"1\">Comments</OPTION>\n"); 133 table = "comments"; 134 title = "Comments"; 135 } 136 else 137 sp.printHTML("<OPTION value=\"1\">Comments</OPTION>\n"); 138 if (type == 2) 139 { 140 sp.printHTML("<OPTION selected value=\"2\">Authors</OPTION>\n"); 141 table = "users"; 142 title = "Stories with author"; 143 } 144 else 145 sp.printHTML("<OPTION value=\"2\">Authors</OPTION>\n"); 146 sp.printHTML("</SELECT></table><p><br>\n" 147 + "<input type=submit value=\"Search now!\"></center><p>\n"); 148 149 if (search == null) 151 sp 152 .printHTML("<br><center><h2>Please select a text to search for</h2></center><br>"); 153 else 154 { 155 sp.printHTML("<br><h2>" + title + " matching <i>" + search 156 + "</i></h2></center><br>"); 157 158 conn = getConnection(); 159 160 if (type == 0) 161 { 162 try 163 { 164 stmt = conn 165 .prepareStatement("SELECT id, title, date, writer FROM stories WHERE title LIKE '" 166 + search 167 + "%' " 168 + " ORDER BY date DESC LIMIT " 169 + page * nbOfStories + "," + nbOfStories); 170 rs = stmt.executeQuery(); 171 } 172 catch (Exception e) 173 { 174 sp.printHTML("Failed to execute Query for BrowseStoriesByCategory: " 175 + e); 176 closeConnection(stmt, conn); 177 return; 178 } 179 try 180 { 181 if (!rs.first()) 182 { 183 stmt = conn 184 .prepareStatement("SELECT id, title, date, writer FROM old_stories WHERE title LIKE '" 185 + search 186 + "%' " 187 + " ORDER BY date DESC LIMIT " 188 + page * nbOfStories + "," + nbOfStories); 189 rs = stmt.executeQuery(); 190 } 191 if (!rs.first()) 192 193 { 194 if (page == 0) 195 sp.printHTML("<h2>Sorry, but there is no story matching <i>" 196 + search + "</i> !</h2>"); 197 else 198 { 199 sp 200 .printHTML("<h2>Sorry, but there are no more stories available matching <i>" 201 + search + "</i>.</h2><br>\n"); 202 sp 203 .printHTML("<p><CENTER>\n<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.Search?search=" 204 + URLEncoder.encode(search) 205 + "&type=" 206 + type 207 + "&page=" 208 + (page - 1) 209 + "&nbOfStories=" 210 + nbOfStories 211 + "\">Previous page</a>\n</CENTER>\n"); 212 } 213 214 sp.printHTMLfooter(); 215 closeConnection(stmt, conn); 216 return; 217 } 218 } 219 220 catch (Exception e) 221 { 222 sp.printHTML("Exception searching type 0: " + e + "<br>"); 223 closeConnection(stmt, conn); 224 return; 225 } 226 } 228 if (type == 1) 229 { comment_table = "comments"; 231 try 232 { 233 stmt = conn 234 .prepareStatement("SELECT id,story_id,subject,writer,date FROM comments WHERE subject LIKE '" 235 + search 236 + "%' " 237 + " GROUP BY story_id ORDER BY date DESC LIMIT " 238 + page * nbOfStories + "," + nbOfStories); 239 rs = stmt.executeQuery(); 240 if (!rs.first()) 241 { 242 stmt = conn 243 .prepareStatement("SELECT id,story_id,subject,writer,date FROM old_comments WHERE subject LIKE '" 244 + search 245 + "%' " 246 + " ORDER BY date DESC LIMIT " 247 + page * nbOfStories + "," + nbOfStories); 248 rs = stmt.executeQuery(); 249 250 comment_table = "old_comments"; 251 } 252 if (!rs.first()) 253 { 254 if (page == 0) 255 sp.printHTML("<h2>Sorry, but there is no comment matching <i>" 256 + search + "</i> !</h2>"); 257 else 258 { 259 sp 260 .printHTML("<h2>Sorry, but there are no more comments available matching <i>" 261 + search + "</i>.</h2><br>\n"); 262 sp 263 .printHTML("<p><CENTER>\n<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.Search?search=" 264 + URLEncoder.encode(search) 265 + "&type=" 266 + type 267 + "&page=" 268 + (page - 1) 269 + "&nbOfStories=" 270 + nbOfStories 271 + "\">Previous page</a>\n</CENTER>\n"); 272 } 273 274 sp.printHTMLfooter(); 275 closeConnection(stmt, conn); 276 return; 277 } 278 else 279 { 280 281 do 283 { 284 String story_id = rs.getString("story_id"); 285 String id = rs.getString("id"); 286 String subject = rs.getString("subject"); 287 String username = sp.getUserName(rs.getInt("writer"), conn); 288 String date = rs.getString("date"); 289 290 sp 291 .printHTML("<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewComment?comment_table=" 292 + comment_table 293 + "&storyId=" 294 + story_id 295 + "&commentId=" 296 + id 297 + "&filter=0&display=0\">" 298 + subject 299 + "</a> by " 300 + username + " on " + date + "<br>\n"); 301 } 302 while (rs.next()); 303 } 304 } 305 catch (Exception e4) 306 { 307 sp.printHTML(e4 + "Exception in type==1"); 308 closeConnection(stmt, conn); 309 return; 310 } 311 } 313 if (type == 2) 314 { try 316 { 317 stmt = conn 318 .prepareStatement("SELECT stories.id, stories.title, stories.date, stories.writer FROM stories,users WHERE writer=users.id AND " 319 + " users.nickname LIKE '" 323 + search 324 + "%'" 325 + " ORDER BY date DESC LIMIT " 326 + page 327 * nbOfStories + "," + nbOfStories); 328 rs = stmt.executeQuery(); 329 if (!rs.first()) 330 stmt = conn 331 .prepareStatement("SELECT old_stories.id, old_stories.title, old_stories.date, old_stories.writer FROM old_stories,users WHERE writer=users.id AND " 332 + " users.nickname LIKE '" 336 + search 337 + "%'" 338 + " ORDER BY date DESC LIMIT " 339 + page 340 * nbOfStories + "," + nbOfStories); 341 rs = stmt.executeQuery(); 342 343 if (!rs.first()) 344 { 345 if (page == 0) 346 sp 347 .printHTML("<h2>Sorry, but there is no story with author matching <i>" 348 + search + "</i> !</h2>"); 349 else 350 { 351 sp 352 .printHTML("<h2>Sorry, but there are no more stories available with author matching <i>$search</i>.</h2><br>\n"); 353 sp 354 .printHTML("<p><CENTER>\n<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.Search?search=" 355 + URLEncoder.encode(search) 356 + "&type=" 357 + type 358 + "&page=" 359 + (page - 1) 360 + "&nbOfStories=" 361 + nbOfStories 362 + "\">Previous page</a>\n</CENTER>\n"); 363 } 364 365 sp.printHTMLfooter(); 366 closeConnection(stmt, conn); 367 return; 368 } 369 } 370 catch (Exception e6) 371 { 372 sp.printHTML(e6 + "Exception in type==2"); 373 closeConnection(stmt, conn); 374 return; 375 } 376 } 378 try 379 { 380 if (type != 1) 381 { 382 383 do 385 { 386 String id = rs.getString("id"); 387 String date = rs.getString("date"); 388 title = rs.getString("title"); 389 390 String username = sp.getUserName(rs.getInt("writer"), conn); 391 sp 392 .printHTML("<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewStory?storyId=" 393 + id 394 + "\">" 395 + title 396 + "</a> by " 397 + username 398 + " on " 399 + date + "<br>\n"); 400 } 401 while (rs.next()); 402 } 403 404 if (page == 0) 405 sp 406 .printHTML("<p><CENTER>\n<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.Search?search=" 407 + URLEncoder.encode(search) 408 + "&type=" 409 + type 410 + "&page=" 411 + (page + 1) 412 + "&nbOfStories=" 413 + nbOfStories 414 + "\">Next page</a>\n</CENTER>\n"); 415 else 416 sp 417 .printHTML("<p><CENTER>\n<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.Search?search=" 418 + URLEncoder.encode(search) 419 + "&type=" 420 + type 421 + "&page=" 422 + (page - 1) 423 + "&nbOfStories=" 424 + nbOfStories 425 + "\">Previous page</a>\n   " 426 + "<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.Search?category=" 427 + search 428 + "=" 429 + URLEncoder.encode(search) 430 + "&type=" 431 + type 432 + "&page=" 433 + (page + 1) 434 + "&nbOfStories=" 435 + nbOfStories 436 + "\">Next page</a>\n\n</CENTER>\n"); 437 } 438 catch (Exception e7) 439 { 440 sp.printHTML(e7 + "Exception in type!=1"); 441 } 442 closeConnection(stmt, conn); 443 } 445 sp.printHTMLfooter(); 449 450 } 451 452 public void doPost(HttpServletRequest request, HttpServletResponse response) 453 throws IOException , ServletException 454 { 455 doGet(request, response); 456 } 457 458 } 459 | Popular Tags |