KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
37  * Builds the html page with the list of all categories and provides links to
38  * browse all items in a category or items in a category for a given region
39  */

40 public class ViewStory extends RubbosHttpServlet
41 {
42
43   public int getPoolSize()
44   {
45     return Config.BrowseCategoriesPoolSize;
46   }
47
48   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
49   {
50     try
51     {
52       if (stmt != null)
53         stmt.close(); // close statement
54
}
55     catch (Exception JavaDoc ignore)
56     {
57     }
58
59     try
60     {
61       if (conn != null)
62           releaseConnection(conn);
63     }
64     catch (Exception JavaDoc ignore)
65     {
66     }
67
68   }
69
70   public void display_follow_up(int cid, int level, int display, int filter,
71                                 Connection JavaDoc link, String JavaDoc comment_table,
72                                 ServletPrinter sp, Connection JavaDoc conn)
73       throws Exception JavaDoc
74   {
75
76     int i, childs, story_id, id, writer;
77     String JavaDoc subject, date;
78     ResultSet JavaDoc rs;
79     PreparedStatement JavaDoc stmt = null;
80     try
81     {
82
83       stmt = conn
84           .prepareStatement("SELECT id,subject,writer,date,story_id,childs FROM "
85               + comment_table + " WHERE parent=" + cid);
86       rs = stmt.executeQuery();
87
88       if (rs.first())
89       {
90         do
91         {
92           for (i = 0; i < level; i++)
93             sp.printHTML("&nbsp&nbsp&nbsp");
94
95           date = rs.getString("date");
96           story_id = rs.getInt("story_id");
97           id = rs.getInt("id");
98           subject = rs.getString("subject");
99           writer = rs.getInt("writer");
100           childs = rs.getInt("childs");
101
102           sp
103               .printHTML("<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewComment?comment_table="
104                   + comment_table
105                   + "&storyId="
106                   + story_id
107                   + "&commentId="
108                   + id
109                   + "&filter="
110                   + filter
111                   + "&display="
112                   + display
113                   + "\">"
114                   + subject + "</a> by " + writer + " on " + date + "<br>\n");
115           if (childs > 0)
116             display_follow_up(id, level + 1, display, filter, link,
117                               comment_table, sp, conn);
118         }
119         while (rs.next());
120       }
121     }
122     catch (Exception JavaDoc e3)
123     {
124       sp.printHTML(e3 + ": Exception in method display_follow_up");
125       try
126       {
127       stmt.close();
128       }
129       catch (Exception JavaDoc ignore)
130       {
131       }
132       throw e3;
133     }
134
135     stmt.close();
136
137   }
138
139   /** Build the html page for the response */
140   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
141       throws IOException JavaDoc, ServletException JavaDoc
142   {
143
144     ServletPrinter sp = null;
145     PreparedStatement JavaDoc stmt = null;
146     Connection JavaDoc conn = null;
147
148     String JavaDoc categoryName, nickname, title = null, body = null,
149         category, table;
150     String JavaDoc password = null, date = null, username = null;
151     int userId, access, storyId = 0;
152     ResultSet JavaDoc rs = null, count_result = null;
153     String JavaDoc comment_table = null;
154     String JavaDoc storyIdtest = request.getParameter("storyId");
155
156     sp = new ServletPrinter(response, "ViewStory");
157
158     if (storyIdtest == null)
159     {
160       sp.printHTML("You must provide a story identifier!<br>");
161       return;
162     }
163
164     if (storyIdtest != null)
165     {
166       storyId = (Integer.valueOf(request.getParameter("storyId"))).intValue();
167     }
168
169     conn = getConnection();
170
171     try
172     {
173       stmt = conn.prepareStatement("SELECT * FROM stories WHERE id=" + storyId);
174       rs = stmt.executeQuery();
175     }
176     catch (Exception JavaDoc e)
177     {
178       sp.printHTML("ERROR: ViewStory query failed" + e);
179       closeConnection(stmt, conn);
180       return;
181     }
182
183     try
184     {
185       if (!rs.first())
186       {
187         stmt = conn.prepareStatement("SELECT * FROM old_stories WHERE id="
188             + storyId);
189         rs = stmt.executeQuery();
190         comment_table = "old_comments";
191
192       }
193       else
194       {
195         comment_table = "comments";
196       }
197
198       if (!rs.first())
199       {
200         sp
201             .printHTML("<h3>ERROR: Sorry, but this story does not exist.</h3><br>\n");
202       }
203
204       username = sp.getUserName(rs.getInt("writer"), conn);
205       date = rs.getString("date");
206       title = rs.getString("title");
207       body = rs.getString("body");
208     }
209
210     catch (Exception JavaDoc e)
211     {
212       sp.printHTML("Exception viewing story " + e + "<br>");
213       closeConnection(stmt, conn);
214       return;
215     }
216
217     sp.printHTMLheader("RUBBoS: Viewing story " + title);
218     sp.printHTMLHighlighted(title);
219     sp.printHTML("Posted by " + username + " on " + date + "<br>\n");
220     sp.printHTML(body + "<br>\n");
221     sp
222         .printHTML("<p><center><a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.PostComment?comment_table="
223             + comment_table
224             + "&storyId="
225             + storyId
226             + "&parent=0\">Post a comment on this story</a></center><p>");
227
228     // Display filter chooser header
229
sp.printHTML("<br><hr><br>");
230     sp
231         .printHTML("<center><form action=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewComment\" method=POST>\n"
232             + "<input type=hidden name=commentId value=0>\n"
233             + "<input type=hidden name=storyId value="
234             + storyId
235             + ">\n"
236             + "<input type=hidden name=comment_table value="
237             + comment_table
238             + ">\n" + "<B>Filter :</B>&nbsp&nbsp<SELECT name=filter>\n");
239
240     int i = -1, rating;
241     String JavaDoc count;
242     int filter, display;
243
244     try
245     {
246       stmt = conn
247           .prepareStatement("SELECT rating, COUNT(rating) AS count FROM "
248               + comment_table + " WHERE story_id=" + storyId
249               + " GROUP BY rating ORDER BY rating");
250       count_result = stmt.executeQuery();
251
252       while (count_result.next())
253       {
254         rating = count_result.getInt("rating");
255         count = count_result.getString("count");
256         filter = 0;
257         while ((i < 6) && (rating != i))
258         {
259           if (i == filter)
260             sp.printHTML("<OPTION selected value=\"" + i + "\">" + i
261                 + ": 0 comment</OPTION>\n");
262           else
263             sp.printHTML("<OPTION value=\"" + i + "\">" + i
264                 + ": 0 comment</OPTION>\n");
265           i++;
266         }
267         if (rating == i)
268         {
269           if (i == filter)
270             sp.printHTML("<OPTION selected value=\"" + i + "\">" + i + ": "
271                 + count + " comments</OPTION>\n");
272           else
273             sp.printHTML("<OPTION value=\"" + i + "\">" + i + ": " + count
274                 + " comments</OPTION>\n");
275           i++;
276         }
277       }
278       stmt.close();
279     }
280     catch (Exception JavaDoc e2)
281     {
282       sp.printHTML("count_result failed " + e2 + "<br>");
283     }
284
285     while (i < 6)
286     {
287       sp.printHTML("<OPTION value=\"" + i + "\">" + i
288           + ": 0 comment</OPTION>\n");
289       i++;
290     }
291
292     sp
293         .printHTML("</SELECT>&nbsp&nbsp&nbsp&nbsp<SELECT name=display>\n"
294             + "<OPTION value=\"0\">Main threads</OPTION>\n"
295             + "<OPTION selected value=\"1\">Nested</OPTION>\n"
296             + "<OPTION value=\"2\">All comments</OPTION>\n"
297             + "</SELECT>&nbsp&nbsp&nbsp&nbsp<input type=submit value=\"Refresh display\"></center><p>\n");
298     display = 1;
299     filter = 0;
300
301     try
302     {
303       stmt = conn.prepareStatement("SELECT * FROM " + comment_table
304           + " WHERE story_id=" + storyId + " AND parent=0 AND rating>="
305           + filter);
306       rs = stmt.executeQuery();
307       String JavaDoc subject, comment, writer, link;
308       int childs, parent, id;
309
310       if (rs.first())
311       {
312         do
313         {
314           username = sp.getUserName(rs.getInt("writer"), conn);
315           subject = rs.getString("subject");
316           rating = rs.getInt("rating");
317           date = rs.getString("subject");
318           comment = rs.getString("comment");
319           id = rs.getInt("id");
320           parent = rs.getInt("parent");
321           childs = rs.getInt("childs");
322
323           sp.printHTML("<br><hr><br>");
324           sp
325               .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="
326                   + comment_table
327                   + "&storyId="
328                   + storyId
329                   + "&commentId="
330                   + id
331                   + "&filter="
332                   + filter
333                   + "&display="
334                   + display
335                   + "\">"
336                   + subject
337                   + "</a></B>&nbsp</FONT> (Score:"
338                   + rating
339                   + ")</TABLE>\n");
340           sp.printHTML("<TABLE><TR><TD><B>Posted by " + username + " on "
341               + date + "</B><p>\n");
342           sp.printHTML("<TR><TD>" + comment);
343           sp
344               .printHTML("<TR><TD><p>[ <a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.PostComment?comment_table="
345                   + comment_table
346                   + "&storyId="
347                   + storyId
348                   + "&parent="
349                   + id
350                   + "\">Reply to this</a>&nbsp|&nbsp"
351                   + "<a HREF=\" /rubbos/servlet/edu.rice.rubbos.servlets.ViewComment?comment_table="
352                   + comment_table
353                   + "&storyId="
354                   + storyId
355                   + "&commentId="
356                   + parent
357                   + "&filter="
358                   + filter
359                   + "&display="
360                   + display
361                   + "\">Parent</a>"
362                   + "&nbsp|&nbsp<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ModerateComment?comment_table="
363                   + comment_table
364                   + "&commentId="
365                   + id
366                   + "\">Moderate</a> ]</TABLE>\n");
367           if (childs > 0)
368               display_follow_up(id, 1, display, filter, conn, comment_table,
369                                 sp, conn);
370         }
371         while (rs.next());
372       }
373
374     }
375     catch (Exception JavaDoc e)
376     {
377       sp.printHTML("Failed to execute Query for ViewStory: " + e);
378       closeConnection(stmt, conn);
379       return;
380     }
381
382     closeConnection(stmt, conn);
383
384     sp.printHTMLfooter();
385
386   }
387
388   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
389       throws IOException JavaDoc, ServletException JavaDoc
390   {
391     doGet(request, response);
392   }
393
394 }
395
Popular Tags