KickJava   Java API By Example, From Geeks To Geeks.

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


1 package edu.rice.rubis.servlets;
2
3 import java.io.IOException JavaDoc;
4 import java.sql.Connection JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8
9 import javax.servlet.ServletException JavaDoc;
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
12
13 /** This servlets records a comment in the database and display
14  * the result of the transaction.
15  * It must be called this way :
16  * <pre>
17  * http://..../StoreComment?itemId=aa&userId=bb&minComment=cc&maxQty=dd&comment=ee&maxComment=ff&qty=gg
18  * where: aa is the item id
19  * bb is the user id
20  * cc is the minimum acceptable comment for this item
21  * dd is the maximum quantity available for this item
22  * ee is the user comment
23  * ff is the maximum comment the user wants
24  * gg is the quantity asked by the user
25  * /<pre>
26  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
27  * @version 1.0
28  */

29
30 public class StoreComment extends RubisHttpServlet
31 {
32
33
34   public int getPoolSize()
35   {
36     return Config.StoreCommentPoolSize;
37   }
38
39 /**
40  * Close both statement and connection to the database.
41  */

42   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
43   {
44     try
45     {
46       if (stmt != null)
47         stmt.close(); // close statement
48
if (conn != null)
49         releaseConnection(conn);
50     }
51     catch (Exception JavaDoc ignore)
52     {
53     }
54   }
55
56 /**
57  * Display an error message.
58  * @param errorMsg the error message value
59  */

60   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
61   {
62     sp.printHTMLheader("RUBiS ERROR: StoreComment");
63     sp.printHTML(
64       "<h2>Your request has not been processed due to the following error :</h2><br>");
65     sp.printHTML(errorMsg);
66     sp.printHTMLfooter();
67    
68   }
69
70   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
71     throws IOException JavaDoc, ServletException JavaDoc
72   {
73     doPost(request, response);
74   }
75
76   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
77     throws IOException JavaDoc, ServletException JavaDoc
78   {
79     Integer JavaDoc toId; // to user id
80
Integer JavaDoc fromId; // from user id
81
Integer JavaDoc itemId; // item id
82
String JavaDoc comment; // user comment
83
Integer JavaDoc rating; // user rating
84
ServletPrinter sp = null;
85     PreparedStatement JavaDoc stmt = null;
86     Connection JavaDoc conn = null;
87
88     sp = new ServletPrinter(response, "StoreComment");
89
90     /* Get and check all parameters */
91
92     String JavaDoc value = request.getParameter("to");
93     if ((value == null) || (value.equals("")))
94     {
95       printError("<h3>You must provide a 'to user' identifier !<br></h3>", sp);
96       return;
97     }
98     else
99       toId = new Integer JavaDoc(value);
100
101     value = request.getParameter("from");
102     if ((value == null) || (value.equals("")))
103     {
104       printError("<h3>You must provide a 'from user' identifier !<br></h3>", sp);
105       return;
106     }
107     else
108       fromId = new Integer JavaDoc(value);
109
110     value = request.getParameter("itemId");
111     if ((value == null) || (value.equals("")))
112     {
113       printError("<h3>You must provide an item identifier !<br></h3>", sp);
114       return;
115     }
116     else
117       itemId = new Integer JavaDoc(value);
118
119     value = request.getParameter("rating");
120     if ((value == null) || (value.equals("")))
121     {
122       printError("<h3>You must provide a rating !<br></h3>", sp);
123       return;
124     }
125     else
126       rating = new Integer JavaDoc(value);
127
128     comment = request.getParameter("comment");
129     if ((comment == null) || (comment.equals("")))
130     {
131       printError("<h3>You must provide a comment !<br></h3>", sp);
132       return;
133     }
134
135     try
136     {
137       conn = getConnection();
138       conn.setAutoCommit(false); // faster if made inside a Tx
139
// Try to create a new comment
140
try
141       {
142         String JavaDoc now = TimeManagement.currentDateToString();
143         stmt =
144           conn.prepareStatement(
145             "INSERT INTO comments VALUES (NULL, \""
146               + fromId
147               + "\", \""
148               + toId
149               + "\", \""
150               + itemId
151               + "\", \""
152               + rating
153               + "\", \""
154               + now
155               + "\",\""
156               + comment
157               + "\")");
158
159         stmt.executeUpdate();
160         stmt.close();
161       }
162       catch (SQLException JavaDoc e)
163       {
164         conn.rollback();
165         printError(
166           "Error while storing the comment (got exception: " + e + ")<br>", sp);
167          closeConnection(stmt, conn);
168         return;
169       }
170       // Try to find the user corresponding to the 'to' ID
171
try
172       {
173         ResultSet JavaDoc urs;
174         stmt = conn.prepareStatement("SELECT rating FROM users WHERE id=?");
175         stmt.setInt(1, toId.intValue());
176         urs = stmt.executeQuery();
177         if (urs.first())
178         {
179           int userRating = urs.getInt("rating");
180           userRating = userRating + rating.intValue();
181
182           stmt = conn.prepareStatement("UPDATE users SET rating=? WHERE id=?");
183           stmt.setInt(1, userRating);
184           stmt.setInt(2, toId.intValue());
185           stmt.executeUpdate();
186         }
187       }
188       catch (SQLException JavaDoc e)
189       {
190         conn.rollback();
191         printError(
192           "Error while updating user's rating (got exception: " + e + ")<br>", sp);
193          closeConnection(stmt, conn);
194         return;
195       }
196       sp.printHTMLheader("RUBiS: Comment posting");
197       sp.printHTML(
198         "<center><h2>Your comment has been successfully posted.</h2></center>");
199
200       sp.printHTMLfooter();
201       conn.commit();
202       closeConnection(stmt, conn);
203     }
204     catch (Exception JavaDoc e)
205     {
206       sp.printHTML("Exception getting comment list: " + e + "<br>");
207       try
208       {
209         conn.rollback();
210         closeConnection(stmt, conn);
211       }
212       catch (Exception JavaDoc se)
213       {
214         sp.printHTML("Transaction rollback failed: " + e + "<br>");
215         closeConnection(stmt, conn);
216       }
217     }
218   }
219
220   /**
221    * Clean up the connection pool.
222    */

223   public void destroy()
224   {
225     super.destroy();
226   }
227 }
228
Popular Tags