KickJava   Java API By Example, From Geeks To Geeks.

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


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
8 import javax.servlet.ServletException JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12 /** This servlets display the page allowing a user to put a comment
13  * on an item.
14  * It must be called this way :
15  * <pre>
16  * http://..../PutComment?to=ww&itemId=xx&nickname=yy&password=zz
17  * where ww is the id of the user that will receive the comment
18  * xx is the item id
19  * yy is the nick name of the user
20  * zz is the user password
21  * /<pre>
22  */

23
24 public class PutComment extends RubisHttpServlet
25 {
26   
27
28   public int getPoolSize()
29   {
30     return Config.PutCommentPoolSize;
31   }
32
33 /**
34  * Close both statement and connection.
35  */

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

54   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
55   {
56     sp.printHTMLheader("RUBiS ERROR: PutComment");
57     sp.printHTML(
58       "<h2>Your request has not been processed due to the following error :</h2><br>");
59     sp.printHTML(errorMsg);
60     sp.printHTMLfooter();
61  
62   }
63
64   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
65     throws IOException JavaDoc, ServletException JavaDoc
66   {
67     ServletPrinter sp = null;
68     
69     String JavaDoc toStr = request.getParameter("to");
70     String JavaDoc itemStr = request.getParameter("itemId");
71     String JavaDoc name = request.getParameter("nickname");
72     String JavaDoc pass = request.getParameter("password");
73     sp = new ServletPrinter(response, "PubComment");
74
75     if ((toStr == null)
76       || (toStr.equals(""))
77       || (itemStr == null)
78       || (itemStr.equals(""))
79       || (name == null)
80       || (name.equals(""))
81       || (pass == null)
82       || (pass.equals("")))
83     {
84       printError("User id, name and password are required - Cannot process the request<br>", sp);
85       return;
86     }
87
88     PreparedStatement JavaDoc stmt = null;
89     Connection JavaDoc conn = null;
90     // Authenticate the user who want to comment
91
conn = getConnection();
92     Auth auth = new Auth(conn, sp);
93     int userId = auth.authenticate(name, pass);
94     if (userId == -1)
95     {
96       printError("You don't have an account on RUBiS!<br>You have to register first.<br>", sp);
97       closeConnection(stmt, conn);
98       return;
99     }
100
101     // Try to find the user corresponding to the 'to' ID
102

103     try
104     {
105       Integer JavaDoc toId = new Integer JavaDoc(toStr);
106       Integer JavaDoc itemId = new Integer JavaDoc(itemStr);
107       ResultSet JavaDoc urs, irs;
108       String JavaDoc toName = null, itemName = null;
109       try
110       {
111         stmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?");
112         stmt.setInt(1, toId.intValue());
113         urs = stmt.executeQuery();
114         if (urs.first())
115           toName = urs.getString("nickname");
116         stmt.close();
117       }
118       catch (Exception JavaDoc e)
119       {
120         printError("Failed to execute Query for user: " + e, sp);
121         closeConnection(stmt, conn);
122         return;
123       }
124       try
125       {
126         stmt = conn.prepareStatement("SELECT name FROM items WHERE id=?");
127         stmt.setInt(1, itemId.intValue());
128         irs = stmt.executeQuery();
129         if (irs.first())
130           itemName = irs.getString("name");
131         stmt.close();
132       }
133       catch (Exception JavaDoc e)
134       {
135         printError("Failed to execute Query for item: " + e, sp);
136         closeConnection(stmt, conn);
137         return;
138       }
139
140       // Display the form for comment
141
sp.printHTMLheader("RUBiS: Comment service");
142       sp.printHTML(
143         "<center><h2>Give feedback about your experience with "
144           + toName
145           + "</h2><br>");
146       sp.printHTML(
147         "<form action=\"/rubis_servlets/servlet/edu.rice.rubis.servlets.StoreComment\" method=POST>"
148           + "<input type=hidden name=to value="
149           + toStr
150           + ">"
151           + "<input type=hidden name=from value="
152           + userId
153           + ">"
154           + "<input type=hidden name=itemId value="
155           + itemId
156           + ">"
157           + "<center><table>"
158           + "<tr><td><b>From</b><td>"
159           + name
160           + "<tr><td><b>To</b><td>"
161           + toName
162           + "<tr><td><b>About item</b><td>"
163           + itemName
164           + "<tr><td><b>Rating</b>"
165           + "<td><SELECT name=rating>"
166           + "<OPTION value=\"5\">Excellent</OPTION>"
167           + "<OPTION value=\"3\">Average</OPTION>"
168           + "<OPTION selected value=\"0\">Neutral</OPTION>"
169           + "<OPTION value=\"-3\">Below average</OPTION>"
170           + "<OPTION value=\"-5\">Bad</OPTION>"
171           + "</SELECT></table><p><br>"
172           + "<TEXTAREA rows=\"20\" cols=\"80\" name=\"comment\">Write your comment here</TEXTAREA><br><p>"
173           + "<input type=submit value=\"Post this comment now!\"></center><p>");
174     }
175     catch (Exception JavaDoc e)
176     {
177       printError("This item does not exist (got exception: " + e + ")<br>", sp);
178       closeConnection(stmt, conn);
179       return;
180     }
181     closeConnection(stmt, conn);
182     sp.printHTMLfooter();
183   }
184
185   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
186     throws IOException JavaDoc, ServletException JavaDoc
187   {
188     doGet(request, response);
189   }
190
191   /**
192    * Clean up the connection pool.
193    */

194   public void destroy()
195   {
196     super.destroy();
197   }
198 }
199
Popular Tags