1 24 25 package edu.rice.rubbos.servlets; 26 27 import java.io.IOException ; 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 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 stmt, Connection conn) 45 { 46 try 47 { 48 if (stmt != null) 49 stmt.close(); } 51 catch (Exception ignore) 52 { 53 } 54 55 try 56 { 57 if (conn != null) 58 releaseConnection(conn); 59 } 60 catch (Exception ignore) 61 { 62 } 63 } 64 65 66 public void doGet(HttpServletRequest request, HttpServletResponse response) 67 throws IOException , ServletException 68 { 69 70 ServletPrinter sp = null; 71 PreparedStatement stmt = null; 72 Connection conn = null; 73 74 sp = new ServletPrinter(response, "OlderStories"); 75 76 String day, month, year, testpage, username, testnbOfStories; 77 int page = 0, nbOfStories = 0, id; 78 ResultSet 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 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> / <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> / <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 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 e) 193 { 194 sp.printHTML("Exception getting older stories: " + e + "<br>"); 195 closeConnection(stmt, conn); 196 return; 197 } 198 199 String title, date; 200 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 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   " 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 request, HttpServletResponse response) 274 throws IOException , ServletException 275 { 276 doGet(request, response); 277 } 278 279 } 280 | Popular Tags |