1 2 package edu.rice.rubis.servlets; 3 4 import java.io.IOException ; 5 import java.sql.Connection ; 6 import java.sql.PreparedStatement ; 7 import java.sql.ResultSet ; 8 import java.sql.SQLException ; 9 10 import javax.servlet.ServletException ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 14 24 25 public class ViewUserInfo extends RubisHttpServlet 26 { 27 28 public int getPoolSize() 29 { 30 return Config.ViewUserInfoPoolSize; 31 } 32 33 36 private void closeConnection(PreparedStatement stmt, Connection conn) 37 { 38 try 39 { 40 if (conn != null) 41 if (conn.getAutoCommit() == false) 42 conn.rollback(); 43 } 44 catch (Exception ignore) 45 { 46 } 47 try 48 { 49 if (stmt != null) 50 stmt.close(); } 52 catch (SQLException e) 53 { 54 } 55 if (conn != null) 56 releaseConnection(conn); 57 } 58 59 private boolean commentList(Integer userId, PreparedStatement stmt, 60 Connection conn, ServletPrinter sp) 61 { 62 ResultSet rs = null; 63 String date, comment; 64 int authorId; 65 66 try 67 { 68 conn.setAutoCommit(false); 70 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 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 do 97 { 98 comment = rs.getString("comment"); 99 date = rs.getString("date"); 100 authorId = rs.getInt("from_user_id"); 101 102 String authorName = "none"; 103 ResultSet authorRS = null; 104 PreparedStatement 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 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 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 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 request, HttpServletResponse response) 149 throws IOException , ServletException 150 { 151 doPost(request, response); 152 } 153 154 public void doPost(HttpServletRequest request, HttpServletResponse response) 155 throws IOException , ServletException 156 { 157 String value = request.getParameter("userId"); 158 Integer userId; 159 ResultSet rs = null; 160 ServletPrinter sp = null; 161 PreparedStatement stmt = null; 162 Connection 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 (value); 175 176 sp.printHTMLheader("RUBiS: View user information"); 177 178 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 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 firstname = rs.getString("firstname"); 203 String lastname = rs.getString("lastname"); 204 String nickname = rs.getString("nickname"); 205 String email = rs.getString("email"); 206 String date = rs.getString("creation_date"); 207 int rating = rs.getInt("rating"); 208 stmt.close(); 209 210 String result = new String (); 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 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 if(connAlive) { 233 closeConnection(stmt, conn); 234 } 235 } 236 237 240 public void destroy() 241 { 242 super.destroy(); 243 } 244 245 } 246 | Popular Tags |