KickJava   Java API By Example, From Geeks To Geeks.

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


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 OlderStories 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   /** Build the html page for the response */
66   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
67       throws IOException JavaDoc, ServletException JavaDoc
68   {
69
70     ServletPrinter sp = null;
71     PreparedStatement JavaDoc stmt = null;
72     Connection JavaDoc conn = null;
73
74     sp = new ServletPrinter(response, "OlderStories");
75
76     String JavaDoc day, month, year, testpage, username, testnbOfStories;
77     int page = 0, nbOfStories = 0, id;
78     ResultSet JavaDoc rs = null;
79
80     testpage = request.getParameter("page");
81     testnbOfStories = request.getParameter("nbOfStories");
82     day = request.getParameter("day");
83     month = request.getParameter("month");
84     year = request.getParameter("year");
85
86     if (testpage != null)
87     {
88       page = (Integer.valueOf(request.getParameter("page"))).intValue();
89     }
90
91     if (testpage == null)
92     {
93       page = 0;
94     }
95
96     if (month == null)
97     {
98       month = request.getParameter("month");
99     }
100
101     if (day == null)
102     {
103       day = request.getParameter("day");
104     }
105
106     if (year == null)
107     {
108       year = request.getParameter("year");
109     }
110
111     if (testnbOfStories != null)
112     {
113       nbOfStories = (Integer.valueOf(request.getParameter("nbOfStories")))
114           .intValue();
115     }
116     else
117       nbOfStories = 25;
118
119     sp.printHTMLheader("RUBBoS Older Stories");
120
121     // Display the date chooser
122
sp
123         .printHTML("<form action=\"/rubbos/servlet/edu.rice.rubbos.servlets.OlderStories\" method=POST>\n");
124     sp.printHTML("<center><B>Date (day/month/year):</B><SELECT name=day>\n");
125     for (int i = 1; i < 32; i++)
126       sp.printHTML("<OPTION value=\"" + i + "\">" + i + "</OPTION>\n");
127     sp.printHTML("</SELECT>&nbsp/&nbsp<SELECT name=month>\n");
128     for (int i = 1; i < 13; i++)
129       sp.printHTML("<OPTION value=\"" + i + "\">" + i + "</OPTION>\n");
130     sp.printHTML("</SELECT>&nbsp/&nbsp<SELECT name=year>\n");
131     for (int i = 2000; i < 2013; i++)
132       sp.printHTML("<OPTION value=\"" + i + "\">" + i + "</OPTION>\n");
133     sp
134         .printHTML("</SELECT><p><input type=submit value=\"Retrieve stories from this date!\"><p>\n");
135
136     if ((day == null) || (month == null) || (year == null))
137       sp.printHTML("<br><h2>Please select a date</h2><br>");
138     else
139     {
140       sp.printHTML("<br><h2>Stories of the " + day + "/" + month + "/" + year
141           + "</h2></center><br>");
142
143       String JavaDoc before, after;
144       before = year + "-" + month + "-" + day + " 0:0:0";
145       after = year + "-" + month + "-" + day + " 23:59:59";
146
147       conn = getConnection();
148
149       try
150       {
151         stmt = conn.prepareStatement("SELECT * FROM stories WHERE date>='"
152             + before + "' AND date<='" + after + "' ORDER BY date DESC LIMIT "
153             + page * nbOfStories + "," + nbOfStories);
154         rs = stmt.executeQuery();
155         if (!rs.first())
156         {
157       stmt.close();
158       stmt = conn
159               .prepareStatement("SELECT * FROM old_stories WHERE date>='"
160                   + before + "' AND date<='" + after
161                   + "' ORDER BY date DESC LIMIT " + page * nbOfStories + ","
162                   + nbOfStories);
163           rs = stmt.executeQuery();
164         }
165         if (!rs.first())
166         {
167           if (page == 0)
168             sp
169                 .printHTML("<h2>Sorry, but there are no story available for this date !</h2>");
170           else
171           {
172             sp
173                 .printHTML("<h2>Sorry, but there is no more stories available for this date.</h2><br>\n");
174             sp
175                 .printHTML("<p><CENTER>\n<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.OlderStories?day="
176                     + day
177                     + "&month="
178                     + month
179                     + "&year="
180                     + year
181                     + "&page="
182                     + (page - 1)
183                     + "&nbOfStories="
184                     + nbOfStories
185                     + "\">Previous page</a>\n</CENTER>\n");
186           }
187           sp.printHTMLfooter();
188           closeConnection(stmt, conn);
189           return;
190         }
191       }
192       catch (Exception JavaDoc e)
193       {
194         sp.printHTML("Exception getting older stories: " + e + "<br>");
195         closeConnection(stmt, conn);
196         return;
197       }
198
199       String JavaDoc title, date;
200       // Print the story titles and author
201

202       try
203       {
204         while (rs.next())
205         {
206           id = rs.getInt("id");
207           title = rs.getString("title");
208           username = sp.getUserName(rs.getInt("writer"), conn);
209           date = rs.getString("date");
210           sp
211               .printHTML("<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewStory?storyId="
212                   + id
213                   + "\">"
214                   + title
215                   + "</a> by "
216                   + username
217                   + " on "
218                   + date
219                   + "<br>\n");
220         }
221       }
222       catch (Exception JavaDoc e2)
223       {
224         sp.printHTML("Exception getting strings: " + e2 + "<br>");
225       }
226
227       closeConnection(stmt, conn);
228
229       if (page == 0)
230         sp
231             .printHTML("<p><CENTER>\n<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.OlderStories?day="
232                 + day
233                 + "&month="
234                 + month
235                 + "&year="
236                 + year
237                 + "&page="
238                 + (page + 1)
239                 + "&nbOfStories="
240                 + nbOfStories
241                 + "\">Next page</a>\n</CENTER>\n");
242       else
243         sp
244             .printHTML("<p><CENTER>\n<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.OlderStories?day="
245                 + day
246                 + "&month="
247                 + month
248                 + "&year="
249                 + year
250                 + "&page="
251                 + (page - 1)
252                 + "&nbOfStories="
253                 + nbOfStories
254                 + "\">Previous page</a>\n&nbsp&nbsp&nbsp"
255                 + "<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.OlderStories?category="
256                 + day
257                 + "="
258                 + day
259                 + "&month="
260                 + month
261                 + "&year="
262                 + year
263                 + "&page="
264                 + (page + 1)
265                 + "&nbOfStories="
266                 + nbOfStories
267                 + "\">Next page</a>\n\n</CENTER>\n");
268     }
269     sp.printHTMLfooter();
270
271   }
272
273   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
274       throws IOException JavaDoc, ServletException JavaDoc
275   {
276     doGet(request, response);
277   }
278
279 }
280
Popular Tags