KickJava   Java API By Example, From Geeks To Geeks.

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


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 BrowseStoriesByCategory 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
72     ServletPrinter sp = null;
73     PreparedStatement JavaDoc stmt = null;
74     Connection JavaDoc conn = null;
75
76     sp = new ServletPrinter(response, "BrowseStoriesByCategory");
77
78     String JavaDoc categoryName, username, categoryId, testpage, testnbOfStories;
79     int page = 0, nbOfStories = 0;
80     ResultSet JavaDoc rs = null;
81
82     testpage = request.getParameter("page");
83     testnbOfStories = request.getParameter("nbOfStories");
84
85     if (testpage != null)
86     {
87       page = (Integer.valueOf(request.getParameter("page"))).intValue();
88     }
89
90     if (testnbOfStories != null)
91     {
92       nbOfStories = (Integer.valueOf(request.getParameter("nbOfStories")))
93           .intValue();
94     }
95
96     categoryId = request.getParameter("category");
97     categoryName = request.getParameter("categoryName");
98
99     if (categoryName == null)
100     {
101       sp.printHTML("Browse Stories By Category"
102           + "You must provide a category name!<br>");
103       return;
104     }
105
106     if (categoryId == null)
107     {
108       sp.printHTML("Browse Stories By Category"
109           + "You must provide a category identifier!<br>");
110       return;
111     }
112
113     if (page == 0)
114     {
115       page = 0;
116     }
117
118     if (nbOfStories == 0)
119     {
120       nbOfStories = 25;
121
122     }
123     sp.printHTMLheader("RUBBoS Browse Stories By Category");
124     sp.printHTML("<br><h2>Stories in category " + categoryName + "</h2><br>");
125
126     conn = getConnection();
127
128     try
129     {
130       stmt = conn.prepareStatement("SELECT * FROM stories WHERE category= "
131           + categoryId + " ORDER BY date DESC LIMIT " + page * nbOfStories
132           + "," + nbOfStories);
133       rs = stmt.executeQuery();
134     }
135     catch (Exception JavaDoc e)
136     {
137       sp.printHTML("Failed to execute Query for BrowseStoriesByCategory: " + e);
138       closeConnection(stmt, conn);
139       return;
140     }
141     try
142     {
143       if (!rs.first())
144       {
145         if (page == 0)
146         {
147           sp
148               .printHTML("<h2>Sorry, but there is no story available in this category !</h2>");
149         }
150         else
151         {
152           sp
153               .printHTML("<h2>Sorry, but there are no more stories available at this time.</h2><br>\n");
154           sp
155               .printHTML("<p><CENTER>\n<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.BrowseStoriesByCategory?category="
156                   + categoryId
157                   + "&categoryName="
158                   + URLEncoder.encode(categoryName)
159                   + "&page="
160                   + (page - 1)
161                   + "&nbOfStories=nbOfStories\">Previous page</a>\n</CENTER>\n");
162         }
163         sp.printHTMLfooter();
164         closeConnection(stmt, conn);
165         return;
166       }
167       
168       do
169       {
170         String JavaDoc title = rs.getString("title");
171         String JavaDoc date = rs.getString("date");
172         username = rs.getString("writer");
173         int id = rs.getInt("id");
174
175         sp
176             .printHTML("<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewStory?storyId="
177                 + id
178                 + "\">"
179                 + title
180                 + "</a> by "
181                 + username
182                 + " on "
183                 + date
184                 + "<br>\n");
185       }
186       while (rs.next());
187     }
188     catch (Exception JavaDoc e)
189     {
190       sp.printHTML("Exception getting categories: " + e + "<br>");
191     }
192         
193     closeConnection(stmt, conn);
194
195
196     if (page == 0)
197       sp
198           .printHTML("<p><CENTER>\n<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.BrowseStoriesByCategory?category="
199               + categoryId
200               + "&categoryName="
201               + URLEncoder.encode(categoryName)
202               + "&page="
203               + (page + 1)
204               + "&nbOfStories="+nbOfStories+"\">Next page</a>\n</CENTER>\n");
205     else
206       sp
207           .printHTML("<p><CENTER>\n<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.BrowseStoriesByCategory?category="
208               + categoryId
209               + "&categoryName="
210               + URLEncoder.encode(categoryName)
211               + "&page="
212               + (page - 1)
213               + "&nbOfStories="+nbOfStories+"\">Previous page</a>\n&nbsp&nbsp&nbsp"
214               + "<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.BrowseStoriesByCategory?category="
215               + categoryId
216               + "&categoryName="
217               + URLEncoder.encode(categoryName)
218               + "&page="
219               + (page + 1)
220               + "&nbOfStories="+nbOfStories+"\">Next page</a>\n\n</CENTER>\n");
221
222     sp.printHTMLfooter();
223
224   }
225
226   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
227       throws IOException JavaDoc, ServletException JavaDoc
228   {
229     doGet(request, response);
230   }
231
232 }
233
Popular Tags