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 StoreModeratorLog 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 67 public void doGet(HttpServletRequest request, HttpServletResponse response) 68 throws IOException , ServletException 69 { 70 ServletPrinter sp = null; 71 PreparedStatement stmt = null; 72 Connection conn = null; 73 74 String nickname, password, comment_table, commentId, ratingstring; 75 int access = 0, userId = 0, rating; 76 ResultSet rs = null; 77 78 int updateResult; 79 80 sp = new ServletPrinter(response, "StoreModeratorLog"); 81 82 nickname = request.getParameter("nickname"); 83 password = request.getParameter("password"); 84 85 comment_table = request.getParameter("comment_table"); 86 commentId = request.getParameter("commentId"); 87 ratingstring = request.getParameter("rating"); 88 89 if (nickname == null) 90 { 91 sp.printHTML("Author, You must provide a nick name!<br>"); 92 return; 93 } 94 95 if (password == null) 96 { 97 sp.printHTML("Author, You must provide a password!<br>"); 98 return; 99 } 100 101 if (comment_table == null) 102 { 103 sp.printHTML("Moderating comment, You must provide a comment table!<br>"); 104 return; 105 } 106 107 if (commentId == null) 108 { 109 sp 110 .printHTML("Moderating comment, You must provide a comment identifier!<br>"); 111 return; 112 } 113 114 if (ratingstring == null) 115 { 116 sp.printHTML("Moderating comment, You must provide a rating!<br>"); 117 return; 118 } 119 else 120 rating = (Integer.valueOf(request.getParameter("rating"))).intValue(); 121 122 conn = getConnection(); 123 124 if ((nickname != null) && (password != null)) 125 { 126 try 127 { 128 stmt = conn 129 .prepareStatement("SELECT id,access FROM users WHERE nickname=\"" 130 + nickname + "\" AND password=\"" + password + "\""); 131 rs = stmt.executeQuery(); 132 } 133 catch (Exception e) 134 { 135 sp.printHTML("Failed to execute Query for BrowseStoriesByCategory: " 136 + e); 137 closeConnection(stmt, conn); 138 return; 139 } 140 141 try 142 { 143 if (rs.first()) 144 { 145 userId = rs.getInt("id"); 146 access = rs.getInt("access"); 147 } 148 stmt.close(); 149 } 150 catch (Exception e) 151 { 152 sp.printHTML("Exception StoreModeratorLog: " + e + "<br>"); 153 closeConnection(stmt, conn); 154 return; 155 } 156 } 157 158 if ((userId == 0) || (access == 0)) 159 { 160 sp.printHTMLheader("RUBBoS: Moderation"); 161 sp 162 .printHTML("<p><center><h2>Sorry, but this feature is only accessible by users with an author access.</h2></center><p>\n"); 163 } 164 else 165 { 166 sp.printHTMLheader("RUBBoS: Comment moderation result"); 167 sp.printHTML("<center><h2>Comment moderation result:</h2></center><p>\n"); 168 169 try 170 { 171 stmt = conn.prepareStatement("SELECT writer,rating FROM " 172 + comment_table + " WHERE id=" + commentId); 173 rs = stmt.executeQuery(); 174 175 if (!rs.first()) 176 { 177 sp 178 .printHTML("<h3>ERROR: Sorry, but this comment does not exist.</h3><br>\n"); 179 } 180 int rsrating = rs.getInt("rating"); 181 String writer = rs.getString("writer"); 182 183 stmt.close(); 184 185 if (((rsrating == -1) && (rating == -1)) 186 || ((rsrating == 5) && (rating == 1))) 187 sp 188 .printHTML("Comment rating is already to its maximum, updating only user's rating."); 189 else 190 { 191 if (rating != 0) 193 { 194 stmt = conn.prepareStatement("UPDATE users SET rating=rating+" 195 + rating + " WHERE id=" + writer); 196 updateResult = stmt.executeUpdate(); 197 stmt.close(); 198 199 stmt = conn.prepareStatement("UPDATE " + comment_table 200 + " SET rating=rating+" + rating + " WHERE id=" + commentId); 201 updateResult = stmt.executeUpdate(); 202 stmt.close(); 203 } 204 } 205 206 stmt = conn.prepareStatement("SELECT rating FROM " + comment_table 207 + " WHERE id=" + commentId); 208 rs = stmt.executeQuery(); 209 String user_row_rating = null, comment_row_rating = null; 210 211 if (rs.first()) 212 comment_row_rating = rs.getString("rating"); 213 stmt.close(); 214 215 stmt = conn.prepareStatement("SELECT rating FROM users WHERE id=" 216 + writer); 217 rs = stmt.executeQuery(); 218 219 if (rs.first()) 220 user_row_rating = rs.getString("rating"); 221 222 if (!rs.first()) 223 sp 224 .printHTML("<h3>ERROR: Sorry, but this user does not exist.</h3><br>\n"); 225 stmt.close(); 226 227 stmt = conn 229 .prepareStatement("INSERT INTO moderator_log VALUES (NULL, " 230 + userId + ", " + commentId + ", " + rating + ", NOW())"); 231 updateResult = stmt.executeUpdate(); 232 233 sp.printHTML("New comment rating is :" + comment_row_rating + "<br>\n"); 234 sp.printHTML("New user rating is :" + user_row_rating + "<br>\n"); 235 sp 236 .printHTML("<center><h2>Your moderation has been successfully stored.</h2></center>\n"); 237 238 } 239 catch (Exception e3) 240 { 241 sp.printHTML("Exception StoreModeratorLog stmts: " + e3 + "<br>"); 242 } 243 244 } 245 closeConnection(stmt, conn); 246 247 sp.printHTMLfooter(); 248 } 249 250 public void doPost(HttpServletRequest request, HttpServletResponse response) 251 throws IOException , ServletException 252 { 253 doGet(request, response); 254 } 255 256 } 257 | Popular Tags |