1 24 25 package edu.rice.rubbos.servlets; 26 27 import java.io.IOException ; 28 import java.net.URLEncoder ; 29 import java.sql.Connection ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 33 import javax.servlet.ServletException ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 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 stmt, Connection conn) 46 { 47 try 48 { 49 if (stmt != null) 50 stmt.close(); } 52 catch (Exception ignore) 53 { 54 } 55 56 try 57 { 58 if (conn != null) 59 releaseConnection(conn); 60 } 61 catch (Exception ignore) 62 { 63 } 64 65 } 66 67 68 public void doGet(HttpServletRequest request, HttpServletResponse response) 69 throws IOException , ServletException 70 { 71 72 ServletPrinter sp = null; 73 PreparedStatement stmt = null; 74 Connection conn = null; 75 76 sp = new ServletPrinter(response, "BrowseStoriesByCategory"); 77 78 String categoryName, username, categoryId, testpage, testnbOfStories; 79 int page = 0, nbOfStories = 0; 80 ResultSet 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 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 title = rs.getString("title"); 171 String 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 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   " 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 request, HttpServletResponse response) 227 throws IOException , ServletException 228 { 229 doGet(request, response); 230 } 231 232 } 233 | Popular Tags |