KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 public class ViewComment extends RubbosHttpServlet
37 {
38
39   public int getPoolSize()
40   {
41     return Config.BrowseCategoriesPoolSize;
42   }
43
44   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
45   {
46     try
47     {
48       if (stmt != null)
49         stmt.close(); // close statement
50
}
51     catch (Exception JavaDoc ignore)
52     {
53     }
54
55     try
56     {
57       if (conn != null)
58           releaseConnection(conn);
59     }
60     catch (Exception JavaDoc ignore)
61     {
62     }
63
64   }
65   
66   /**
67    * This function must throw an exception as there should be only
68    * ONE close fo the connection and that close should be handled by
69    * the caller
70    */

71   public void display_follow_up(int cid, int level, int display, int filter,
72                                 Connection JavaDoc link, String JavaDoc comment_table,
73                                 boolean separator, ServletPrinter sp,
74                                 Connection JavaDoc conn) throws Exception JavaDoc
75   {
76     ResultSet JavaDoc follow;
77     int childs, story_id, rating, parent, id, i;
78     String JavaDoc subject, username, date, comment;
79     PreparedStatement JavaDoc stmtfollow = null;
80
81     try
82     {
83       stmtfollow = conn.prepareStatement("SELECT * FROM " + comment_table
84           + " WHERE parent=" + cid);
85       //+" AND rating>="+filter);
86
follow = stmtfollow.executeQuery();
87
88       while (follow.next())
89       {
90         story_id = follow.getInt("story_id");
91         id = follow.getInt("id");
92         subject = follow.getString("subject");
93         username = sp.getUserName(follow.getInt("writer"), conn);
94         date = follow.getString("date");
95         rating = follow.getInt("rating");
96         parent = follow.getInt("parent");
97         comment = follow.getString("comment");
98         childs = follow.getInt("childs");
99
100         if (rating >= filter)
101         {
102           if (!separator)
103           {
104             sp.printHTML("<br><hr><br>");
105             separator = true;
106           }
107           if (display == 1) // Preview nested comments
108
{
109             for (i = 0; i < level; i++)
110               sp.printHTML(" &nbsp &nbsp &nbsp ");
111             sp
112                 .printHTML("<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewComment?comment_table="
113                     + comment_table
114                     + "&storyId="
115                     + story_id
116                     + "&commentId="
117                     + id
118                     + "&filter="
119                     + filter
120                     + "&display="
121                     + display
122                     + "\">"
123                     + subject
124                     + "</a> by "
125                     + username
126                     + " on "
127                     + date
128                     + "<br>\n");
129           }
130           else
131           {
132             sp.printHTML("<TABLE bgcolor=\"#CCCCFF\"><TR>");
133             for (i = 0; i < level; i++)
134               sp.printHTML("<TD>&nbsp&nbsp&nbsp");
135             sp
136                 .printHTML("<TD><FONT size=\"4\" color=\"#000000\"><B><a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewComment?comment_table="
137                     + comment_table
138                     + "&storyId="
139                     + story_id
140                     + "&commentId="
141                     + id
142                     + "&filter="
143                     + filter
144                     + "&display="
145                     + display
146                     + "\">"
147                     + subject
148                     + "</a></B>&nbsp</FONT> (Score:"
149                     + rating
150                     + ")</TABLE>\n");
151             sp.printHTML("<TABLE>");
152             for (i = 0; i < level; i++)
153               sp.printHTML("<TD>&nbsp&nbsp&nbsp");
154             sp.printHTML("<TD><B>Posted by " + username + " on " + date
155                 + "</B><p><TR>\n");
156             for (i = 0; i < level; i++)
157               sp.printHTML("<TD>&nbsp&nbsp&nbsp");
158             sp.printHTML("<TD>" + comment + "<TR>");
159             for (i = 0; i < level; i++)
160               sp.printHTML("<TD>&nbsp&nbsp&nbsp");
161             sp
162                 .printHTML("<TD><p>[ <a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.PostComment?comment_table="
163                     + comment_table
164                     + "&storyId="
165                     + story_id
166                     + "&parent="
167                     + id
168                     + "\">Reply to this</a>"
169                     + "&nbsp|&nbsp<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewComment?comment_table="
170                     + comment_table
171                     + "&storyId="
172                     + story_id
173                     + "&commentId="
174                     + parent
175                     + "&filter="
176                     + filter
177                     + "&display="
178                     + display
179                     + "\">Parent</a>&nbsp|&nbsp<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ModerateComment?comment_table="
180                     + comment_table
181                     + "&commentId="
182                     + id
183                     + "\">Moderate</a> ]</TABLE><br>");
184           }
185         }
186         if (childs > 0)
187           display_follow_up(id, level + 1, display, filter, link,
188                             comment_table, separator, sp, conn);
189       }
190     }
191     catch (Exception JavaDoc e)
192     {
193       sp.printHTML("Failure at display_follow_up: " + e);
194       try
195       {
196       stmtfollow.close();
197       }
198       catch (Exception JavaDoc ignore)
199       {
200       }
201       throw e;
202     }
203     stmtfollow.close();
204   }
205
206   /** Build the html page for the response */
207   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
208       throws IOException JavaDoc, ServletException JavaDoc
209   {
210
211     ServletPrinter sp = null;
212     PreparedStatement JavaDoc stmt = null;
213     Connection JavaDoc conn = null;
214
215     String JavaDoc categoryName, filterstring, username, categoryId,
216         comment = null, displaystring, storyId, commentIdstring, comment_table;
217     int parent = 0, childs, page = 0, filter = 0, display = 0,
218         commentId;
219     int i = 0, count, rating;
220     ResultSet JavaDoc rs = null;
221
222     sp = new ServletPrinter(response, "ViewComment");
223
224     filterstring = request.getParameter("filter");
225     storyId = request.getParameter("storyId");
226     displaystring = request.getParameter("display");
227     commentIdstring = request.getParameter("commentId");
228     comment_table = request.getParameter("comment_table");
229
230     if (filterstring != null)
231     {
232       filter = (Integer.valueOf(request.getParameter("filter"))).intValue();
233     }
234     else
235       filter = 0;
236
237     if (displaystring != null)
238     {
239       display = (Integer.valueOf(request.getParameter("display"))).intValue();
240     }
241     else
242       display = 0;
243
244     if (storyId == null)
245     {
246       sp.printHTML("Viewing comment: You must provide a story identifier!<br>");
247       return;
248     }
249
250     if (commentIdstring == null)
251     {
252       sp
253           .printHTML("Viewing comment: You must provide a comment identifier!<br>");
254       return;
255     }
256     else
257       commentId = (Integer.valueOf(request.getParameter("commentId")))
258           .intValue();
259
260     if (comment_table == null)
261     {
262       sp.printHTML("Viewing comment: You must provide a comment table!<br>");
263     }
264
265     conn = getConnection();
266
267     if (commentId == 0)
268       parent = 0;
269     else
270     {
271       try
272       {
273         stmt = conn.prepareStatement("SELECT parent FROM " + comment_table
274             + " WHERE id=" + commentId);
275         rs = stmt.executeQuery();
276         if (!rs.first())
277         {
278           sp
279               .printHTML("<h3>ERROR: Sorry, but this comment does not exist.</h3><br>\n");
280           closeConnection(stmt, conn);
281           return;
282         }
283         parent = rs.getInt("parent");
284     stmt.close();
285       }
286       catch (Exception JavaDoc e)
287       {
288         sp.printHTML("Failure at 'SELECT parent' stmt: " + e);
289         closeConnection(stmt, conn);
290         return;
291       }
292     }
293
294     sp.printHTMLheader("RUBBoS: Viewing comments");
295     sp
296         .printHTML("<center><form action=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewComment\" method=POST>\n"
297             + "<input type=hidden name=commentId value="
298             + commentId
299             + ">\n"
300             + "<input type=hidden name=storyId value="
301             + storyId
302             + ">\n"
303             + "<input type=hidden name=comment_table value="
304             + comment_table
305             + ">\n" + "<B>Filter :</B>&nbsp&nbsp<SELECT name=filter>\n");
306
307     try
308     {
309       stmt = conn
310           .prepareStatement("SELECT rating, COUNT(rating) AS count FROM "
311               + comment_table + " WHERE story_id=" + storyId
312               + " GROUP BY rating ORDER BY rating");
313       rs = stmt.executeQuery();
314
315       i = -1;
316       if (rs.first())
317       {
318         do
319         {
320           rating = rs.getInt("rating");
321           count = rs.getInt("count");
322           while ((i < 6) && (rating != i))
323           {
324             if (i == filter)
325               sp.printHTML("<OPTION selected value=\"" + i + "\">" + i
326                   + ": 0 comment</OPTION>\n");
327             else
328               sp.printHTML("<OPTION value=\"" + i + "\">" + i
329                   + ": 0 comment</OPTION>\n");
330             i++;
331           }
332           if (rating == i)
333           {
334             if (i == filter)
335               sp.printHTML("<OPTION selected value=\"" + i + "\">" + i + ": "
336                   + count + " comments</OPTION>\n");
337             else
338               sp.printHTML("<OPTION value=\"" + i + "\">" + i + ": " + count
339                   + " comments</OPTION>\n");
340             i++;
341           }
342         }
343         while (rs.next());
344       }
345       stmt.close();
346     }
347     catch (Exception JavaDoc e)
348     {
349       sp.printHTML("Failed to execute Query for View Comment: " + e);
350       closeConnection(stmt, conn);
351       return;
352     }
353
354     while (i < 6)
355     {
356       sp.printHTML("<OPTION value=\"" + i + "\">" + i
357           + ": 0 comment</OPTION>\n");
358       i++;
359     }
360
361     sp.printHTML("</SELECT>&nbsp&nbsp&nbsp&nbsp<SELECT name=display>\n"
362         + "<OPTION value=\"0\">Main threads</OPTION>\n");
363     if (display == 1)
364       sp.printHTML("<OPTION selected value=\"1\">Nested</OPTION>\n");
365     else
366       sp.printHTML("<OPTION value=\"1\">Nested</OPTION>\n");
367     if (display == 2)
368       sp.printHTML("<OPTION selected value=\"2\">All comments</OPTION>\n");
369     else
370       sp.printHTML("<OPTION value=\"2\">All comments</OPTION>\n");
371     sp
372         .printHTML("</SELECT>&nbsp&nbsp&nbsp&nbsp<input type=submit value=\"Refresh display\"></center><p>\n");
373
374     String JavaDoc subject, date;
375     int id;
376     boolean separator;
377     try
378     {
379       stmt = conn.prepareStatement("SELECT * FROM " + comment_table
380           + " WHERE story_id=" + storyId + " AND parent=0"); //+ parent+
381
//" AND rating>="+filter);
382
rs = stmt.executeQuery();
383
384       while (rs.next())
385       {
386         username = sp.getUserName(rs.getInt("writer"), conn);
387         rating = rs.getInt("rating");
388         parent = rs.getInt("parent");
389         id = rs.getInt("id");
390         subject = rs.getString("subject");
391         date = rs.getString("date");
392         childs = rs.getInt("childs");
393         comment = rs.getString("comment");
394         separator = false;
395
396         if (rating >= filter)
397         {
398           sp.printHTML("<br><hr><br>");
399           separator = true;
400           sp
401               .printHTML("<TABLE width=\"100%\" bgcolor=\"#CCCCFF\"><TR><TD><FONT size=\"4\" color=\"#000000\"><B><a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewComment?comment_table="
402                   + comment_table
403                   + "&storyId="
404                   + storyId
405                   + "&commentId="
406                   + id
407                   + "&filter="
408                   + filter
409                   + "&display="
410                   + display
411                   + "\">"
412                   + subject
413                   + "</a></B>&nbsp</FONT> (Score:"
414                   + rating
415                   + ")</TABLE>\n");
416           sp.printHTML("<TABLE><TR><TD><B>Posted by " + username + " on "
417               + date + "</B><p>\n");
418           sp.printHTML("<TR><TD>" + comment);
419           sp
420               .printHTML("<TR><TD><p>[ <a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.PostComment?comment_table="
421                   + comment_table
422                   + "&storyId="
423                   + storyId
424                   + "&parent="
425                   + id
426                   + "\">Reply to this</a>&nbsp|&nbsp"
427                   + "<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewComment?comment_table="
428                   + comment_table
429                   + "&storyId="
430                   + storyId
431                   + "&commentId="
432                   + parent
433                   + "&filter="
434                   + filter
435                   + "&display="
436                   + display
437                   + "\">Parent</a>"
438                   + "&nbsp|&nbsp<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ModerateComment?comment_table="
439                   + comment_table
440                   + "&commentId="
441                   + id
442                   + "\">Moderate</a> ]</TABLE>\n");
443         }
444         if ((display > 0) && (childs > 0))
445           display_follow_up(id, 1, display, filter, conn, comment_table,
446                             separator, sp, conn);
447       }
448
449     }
450     catch (Exception JavaDoc e)
451     {
452       closeConnection(stmt, conn);
453       sp.printHTML("Exception getting categories: " + e + "<br>");
454       return;
455     }
456
457     closeConnection(stmt, conn);
458
459     sp.printHTMLfooter();
460
461   }
462
463   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
464       throws IOException JavaDoc, ServletException JavaDoc
465   {
466     doGet(request, response);
467   }
468
469 }
470
Popular Tags