KickJava   Java API By Example, From Geeks To Geeks.

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


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 StoreComment 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
71     ServletPrinter sp = null;
72     PreparedStatement JavaDoc stmt = null;
73     Connection JavaDoc conn = null;
74
75     String JavaDoc nickname, password, storyId, parent, userIdstring, subject, body;
76     String JavaDoc comment_table;
77     int page = 0, nbOfStories = 0, userId;
78     int updateResult;
79
80     sp = new ServletPrinter(response, "StoreComment");
81
82     nickname = request.getParameter("nickname");
83     password = request.getParameter("password");
84     storyId = request.getParameter("storyId");
85     parent = request.getParameter("parent");
86     subject = request.getParameter("subject");
87     body = request.getParameter("body");
88     comment_table = request.getParameter("comment_table");
89
90     if (nickname == null)
91     {
92       nickname = request.getParameter("nickname");
93     }
94
95     if (password == null)
96     {
97       password = request.getParameter("password");
98     }
99
100     if (storyId == null)
101     {
102       sp.printHTML("StoreComment, You must provide a story identifier!<br>");
103       return;
104     }
105
106     if (parent == null)
107     {
108       sp
109           .printHTML("StoreComment, You must provide a follow up identifier!<br>");
110       return;
111     }
112
113     if (subject == null)
114     {
115       sp.printHTML("StoreComment, You must provide a comment subject!<br>");
116       return;
117     }
118
119     if (body == null)
120     {
121       sp
122           .printHTML("StoreComment, <h3>You must provide a comment body!<br></h3>");
123       return;
124     }
125
126     if (comment_table == null)
127     {
128       sp.printHTML("Viewing comment, You must provide a comment table!<br>");
129       return;
130     }
131
132     sp.printHTMLheader("RUBBoS: Comment submission result");
133
134     sp.printHTML("<center><h2>Comment submission result:</h2></center><p>\n");
135
136     conn = getConnection();
137
138     // Authenticate the user
139
userIdstring = sp.authenticate(nickname, password, conn);
140     userId = (Integer.valueOf(userIdstring)).intValue();
141
142     if (userId == 0)
143       sp.printHTML("Comment posted by the 'Anonymous Coward'<br>\n");
144     else
145       sp.printHTML("Comment posted by user #" + userId + "<br>\n");
146
147     // Add comment to database
148

149     try
150     {
151       stmt = conn.prepareStatement("INSERT INTO " + comment_table
152           + " VALUES (NULL, " + userId + ", " + storyId + ", " + parent
153           + ", 0, 0, NOW(), \"" + subject + "\", \"" + body + "\")");
154       updateResult = stmt.executeUpdate();
155
156       stmt.close();
157
158       stmt = conn.prepareStatement("UPDATE " + comment_table
159           + " SET childs=childs+1 WHERE id=" + parent);
160       updateResult = stmt.executeUpdate();
161     }
162     catch (Exception JavaDoc e)
163     {
164       closeConnection(stmt, conn);
165       sp.printHTML("Exception getting categories: " + e + "<br>");
166       return;
167     }
168
169     closeConnection(stmt, conn);
170
171     sp.printHTML("Your comment has been successfully stored in the "
172         + comment_table + " database table<br>\n");
173     sp.printHTMLfooter();
174
175   }
176
177   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
178       throws IOException JavaDoc, ServletException JavaDoc
179   {
180     doGet(request, response);
181   }
182
183 }
184
Popular Tags