KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 package edu.rice.rubis.servlets;
3
4 import java.io.IOException JavaDoc;
5 import java.sql.Connection JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9
10 import javax.servlet.ServletException JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13
14 /**
15  * This servlets displays general information about a user. It must be called
16  * this way :
17  *
18  * <pre>
19  *
20  * http://..../ViewUserInfo?userId=xx where xx is the id of the user
21  *
22  * </pre>
23  */

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

36   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
37   {
38     try
39     {
40       if (conn != null)
41         if (conn.getAutoCommit() == false)
42           conn.rollback();
43     }
44     catch (Exception JavaDoc ignore)
45     {
46     }
47     try
48     {
49       if (stmt != null)
50         stmt.close(); // close statement
51
}
52     catch (SQLException JavaDoc e)
53     {
54     }
55     if (conn != null)
56       releaseConnection(conn);
57   }
58
59   private boolean commentList(Integer JavaDoc userId, PreparedStatement JavaDoc stmt,
60       Connection JavaDoc conn, ServletPrinter sp)
61   {
62     ResultSet JavaDoc rs = null;
63     String JavaDoc date, comment;
64     int authorId;
65
66     try
67     {
68       conn.setAutoCommit(false); // faster if made inside a Tx
69

70       // Try to find the comment corresponding to the user
71
try
72       {
73         stmt = conn
74             .prepareStatement("SELECT * FROM comments WHERE to_user_id=?");
75         stmt.setInt(1, userId.intValue());
76         rs = stmt.executeQuery();
77       }
78       catch (Exception JavaDoc e)
79       {
80         sp.printHTML("Failed to execute Query for list of comments: " + e);
81         conn.rollback();
82         closeConnection(stmt, conn);
83         return false;
84       }
85       if (!rs.first())
86       {
87         sp.printHTML("<h3>There is no comment yet for this user.</h3><br>");
88         conn.commit();
89         closeConnection(stmt, conn);
90         return false;
91       }
92       sp.printHTML("<br><hr><br><h3>Comments for this user</h3><br>");
93
94       sp.printCommentHeader();
95       // Display each comment and the name of its author
96
do
97       {
98         comment = rs.getString("comment");
99         date = rs.getString("date");
100         authorId = rs.getInt("from_user_id");
101
102         String JavaDoc authorName = "none";
103         ResultSet JavaDoc authorRS = null;
104         PreparedStatement JavaDoc authorStmt = null;
105         try
106         {
107           authorStmt = conn
108               .prepareStatement("SELECT nickname FROM users WHERE id=?");
109           authorStmt.setInt(1, authorId);
110           authorRS = authorStmt.executeQuery();
111           if (authorRS.first())
112             authorName = authorRS.getString("nickname");
113           authorStmt.close();
114         }
115         catch (Exception JavaDoc e)
116         {
117           sp.printHTML("Failed to execute Query for the comment author: " + e);
118           conn.rollback();
119           authorStmt.close();
120           closeConnection(stmt, conn);
121           return false;
122         }
123         sp.printComment(authorName, authorId, date, comment);
124       }
125       while (rs.next());
126       sp.printCommentFooter();
127       conn.commit();
128     }
129     catch (Exception JavaDoc e)
130     {
131       sp.printHTML("Exception getting comment list: " + e + "<br>");
132       try
133       {
134         conn.rollback();
135         closeConnection(stmt, conn);
136         return false;
137       }
138       catch (Exception JavaDoc se)
139       {
140         sp.printHTML("Transaction rollback failed: " + e + "<br>");
141         closeConnection(stmt, conn);
142         return false;
143       }
144     }
145     return true;
146   }
147
148   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
149       throws IOException JavaDoc, ServletException JavaDoc
150   {
151     doPost(request, response);
152   }
153
154   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
155       throws IOException JavaDoc, ServletException JavaDoc
156   {
157     String JavaDoc value = request.getParameter("userId");
158     Integer JavaDoc userId;
159     ResultSet JavaDoc rs = null;
160     ServletPrinter sp = null;
161     PreparedStatement JavaDoc stmt = null;
162     Connection JavaDoc conn = null;
163
164     sp = new ServletPrinter(response, "ViewUserInfo");
165
166     if ((value == null) || (value.equals("")))
167     {
168       sp.printHTMLheader("RUBiS ERROR: View user information");
169       sp.printHTML("<h3>You must provide a user identifier !<br></h3>");
170       sp.printHTMLfooter();
171       return;
172     }
173     else
174       userId = new Integer JavaDoc(value);
175
176     sp.printHTMLheader("RUBiS: View user information");
177
178     // Try to find the user corresponding to the userId
179
try
180     {
181       conn = getConnection();
182       stmt = conn.prepareStatement("SELECT * FROM users WHERE id=?");
183       stmt.setInt(1, userId.intValue());
184       rs = stmt.executeQuery();
185     }
186     catch (Exception JavaDoc e)
187     {
188       sp.printHTML("Failed to execute Query for user: " + e);
189       closeConnection(stmt, conn);
190       sp.printHTMLfooter();
191       return;
192     }
193     try
194     {
195       if (!rs.first())
196       {
197         sp.printHTML("<h2>This user does not exist!</h2>");
198         closeConnection(stmt, conn);
199         sp.printHTMLfooter();
200         return;
201       }
202       String JavaDoc firstname = rs.getString("firstname");
203       String JavaDoc lastname = rs.getString("lastname");
204       String JavaDoc nickname = rs.getString("nickname");
205       String JavaDoc email = rs.getString("email");
206       String JavaDoc date = rs.getString("creation_date");
207       int rating = rs.getInt("rating");
208       stmt.close();
209
210       String JavaDoc result = new String JavaDoc();
211
212       result = result + "<h2>Information about " + nickname + "<br></h2>";
213       result = result + "Real life name : " + firstname + " " + lastname
214           + "<br>";
215       result = result + "Email address : " + email + "<br>";
216       result = result + "User since : " + date + "<br>";
217       result = result + "Current rating : <b>" + rating + "</b><br>";
218       sp.printHTML(result);
219
220     }
221     catch (SQLException JavaDoc s)
222     {
223       sp.printHTML("Failed to get general information about the user: " + s);
224       closeConnection(stmt, conn);
225       sp.printHTMLfooter();
226       return;
227     }
228     boolean connAlive = commentList(userId, stmt, conn, sp);
229     sp.printHTMLfooter();
230     // connAlive means we must close it. Otherwise we must NOT do a
231
// double free
232
if(connAlive) {
233         closeConnection(stmt, conn);
234     }
235   }
236
237   /**
238    * Clean up the connection pool.
239    */

240   public void destroy()
241   {
242     super.destroy();
243   }
244
245 }
246
Popular Tags