KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubbos > servlets > Search


1 /**
2  * RUBBoS: Rice University Bulletin Board System.
3  * Copyright (C) 2001-2004 Rice University and French National Institute For
4  * Research In Computer Science And Control (INRIA).
5  * Contact: jmob@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Niraj Tolia.
23  */

24
25 package edu.rice.rubbos.servlets;
26
27 import java.io.IOException JavaDoc;
28 import java.net.URLEncoder JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc stmt, Connection JavaDoc conn)
46   {
47     try
48     {
49       if (stmt != null)
50         stmt.close(); // close statement
51
}
52     catch (Exception JavaDoc ignore)
53     {
54     }
55
56     try
57     {
58       if (conn != null)
59           releaseConnection(conn);
60     }
61     catch (Exception JavaDoc ignore)
62     {
63     }
64
65   }
66
67   /** Build the html page for the response */
68   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
69       throws IOException JavaDoc, ServletException JavaDoc
70   {
71     ServletPrinter sp = null;
72     PreparedStatement JavaDoc stmt = null;
73     Connection JavaDoc conn = null;
74
75     String JavaDoc testtype, search, testpage, testnbOfStories, table, title = null;
76     String JavaDoc comment_table;
77     int page = 0, type, nbOfStories = 0;
78     ResultSet JavaDoc 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     // Display the search form
117
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     // Display the results
150
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                   + /* OR body LIKE '$search%%' */" ORDER BY date DESC LIMIT "
169                   + page * nbOfStories + "," + nbOfStories);
170           rs = stmt.executeQuery();
171         }
172         catch (Exception JavaDoc 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                     + /* OR body LIKE '$search%%' */" 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 JavaDoc e)
221         {
222           sp.printHTML("Exception searching type 0: " + e + "<br>");
223           closeConnection(stmt, conn);
224           return;
225         }
226       } // if (type == 0)
227

228       if (type == 1)
229       { // Look for comments
230
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                   + /* OR comment LIKE '$search%%' */" 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                     + /* OR comment LIKE '$search%%' */" 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             // Print the comment subject and author
282
do
283             {
284               String JavaDoc story_id = rs.getString("story_id");
285               String JavaDoc id = rs.getString("id");
286               String JavaDoc subject = rs.getString("subject");
287               String JavaDoc username = sp.getUserName(rs.getInt("writer"), conn);
288               String JavaDoc 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 JavaDoc e4)
306         {
307           sp.printHTML(e4 + "Exception in type==1");
308           closeConnection(stmt, conn);
309           return;
310         }
311       } // if (type == 1)
312

313       if (type == 2)
314       { // Look for stories of an author
315
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                   + /*
320                      * (users.firstname LIKE '$search%%' OR users.lastname LIKE
321                      * '$search%%' OR
322                      */
" 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                     + /*
333                        * (users.firstname LIKE '$search%%' OR users.lastname
334                        * LIKE '$search%%' OR
335                        */
" 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 JavaDoc e6)
371         {
372           sp.printHTML(e6 + "Exception in type==2");
373           closeConnection(stmt, conn);
374           return;
375         }
376       }// if (type == 2)
377

378       try
379       {
380         if (type != 1)
381         {
382
383           // Print the story titles and author
384
do
385           {
386             String JavaDoc id = rs.getString("id");
387             String JavaDoc date = rs.getString("date");
388             title = rs.getString("title");
389
390             String JavaDoc 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&nbsp&nbsp&nbsp"
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 JavaDoc e7)
439       {
440         sp.printHTML(e7 + "Exception in type!=1");
441       }
442       closeConnection(stmt, conn);
443     } // end else
444

445     // We do not need to do a closeConnection() here as a connection
446
// is only grabbed in a large if () clause above and we take care
447
// of closing it.
448
sp.printHTMLfooter();
449
450   }
451
452   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
453       throws IOException JavaDoc, ServletException JavaDoc
454   {
455     doGet(request, response);
456   }
457
458 }
459
Popular Tags