KickJava   Java API By Example, From Geeks To Geeks.

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


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 StoreModeratorLog 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
66   /** Build the html page for the response */
67   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
68       throws IOException JavaDoc, ServletException JavaDoc
69   {
70     ServletPrinter sp = null;
71     PreparedStatement JavaDoc stmt = null;
72     Connection JavaDoc conn = null;
73
74     String JavaDoc nickname, password, comment_table, commentId, ratingstring;
75     int access = 0, userId = 0, rating;
76     ResultSet JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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           // Update ratings
192
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 JavaDoc 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         // Update moderator log
228
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 JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response)
251       throws IOException JavaDoc, ServletException JavaDoc
252   {
253     doGet(request, response);
254   }
255
256 }
257
Popular Tags