1 package edu.rice.rubis.beans; 2 3 import java.rmi.RemoteException ; 4 import javax.ejb.MessageDrivenBean ; 5 import javax.ejb.MessageDrivenContext ; 6 import javax.ejb.EJBException ; 7 import javax.jms.*; 8 import javax.naming.Context ; 9 import javax.naming.InitialContext ; 10 import javax.rmi.PortableRemoteObject ; 11 import javax.sql.DataSource ; 12 import java.sql.Connection ; 13 import java.sql.PreparedStatement ; 14 import java.sql.ResultSet ; 15 import java.sql.SQLException ; 16 import java.io.Serializable ; 17 import javax.transaction.UserTransaction ; 18 import java.net.URLEncoder ; 19 20 26 27 public class MDB_ViewUserInfo implements MessageDrivenBean , MessageListener 28 { 29 private DataSource dataSource; 30 private MessageDrivenContext messageDrivenContext; 31 private TopicConnectionFactory connectionFactory; 32 private TopicConnection connection; 33 private Topic topic; 34 private TopicSession session; 35 private TopicPublisher replier; 36 private Context initialContext = null; 37 38 39 public MDB_ViewUserInfo() 40 { 41 42 } 43 44 public void onMessage(Message message) 45 { 46 try 47 { 48 MapMessage request = (MapMessage)message; 49 String correlationID = request.getJMSCorrelationID(); 50 int userId = request.getInt("userId"); 51 52 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 54 55 String html = getUserInfo(userId); 57 58 TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); 60 if (temporaryTopic != null) 61 { 62 connection = connectionFactory.createTopicConnection(); 64 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 66 TextMessage reply = session.createTextMessage(); 67 reply.setJMSCorrelationID(correlationID); 68 reply.setText(html); 69 replier = session.createPublisher(null); connection.start(); 71 replier.publish(temporaryTopic, reply); 72 replier.close(); 73 session.close(); 74 connection.stop(); 75 connection.close(); 76 } 77 } 78 catch (Exception e) 79 { 80 throw new EJBException ("Message traitment failed for MDB_ViewUserInfo: " +e); 81 } 82 } 83 84 92 public String getComments(int userId, Connection conn) throws RemoteException 93 { 94 StringBuffer html; 95 PreparedStatement stmt = null; 96 ResultSet rs = null; 97 String comment=null, date=null; 98 int authorId; 99 100 try 102 { 103 stmt = conn.prepareStatement("SELECT * FROM comments WHERE to_user_id=?"); 104 stmt.setInt(1, userId); 105 rs = stmt.executeQuery(); 106 } 107 catch (SQLException e) 108 { 109 try 110 { 111 if (stmt != null) stmt.close(); 112 if (conn != null) conn.close(); 113 } 114 catch (Exception ignore) 115 { 116 } 117 throw new RemoteException ("Failed to get categories list " +e); 118 } 119 try 120 { 121 if (!rs.first()) 122 html = new StringBuffer ("<h3>There is no comment yet for this user.</h3><br>"); 123 else 124 { 125 html = new StringBuffer ("<br><hr><br><h3>Comments for this user</h3><br>"); 126 127 html.append(printCommentHeader()); 128 PreparedStatement authorStmt = null; 130 try 131 { 132 authorStmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 133 do 134 { 135 comment = rs.getString("comment"); 136 date = rs.getString("date"); 137 authorId = rs.getInt("from_user_id"); 138 139 String authorName = "none"; 140 ResultSet authorRS = null; 141 authorStmt.setInt(1, authorId); 142 authorRS = authorStmt.executeQuery(); 143 if (authorRS.first()) 144 authorName = authorRS.getString("nickname"); 145 html.append(printComment(authorName, date, comment, authorId)); 146 } 147 while (rs.next()); 148 authorStmt.close(); 149 stmt.close(); 150 } 151 catch (Exception e) 152 { 153 try 154 { 155 if (authorStmt != null) authorStmt.close(); 156 if (stmt != null) stmt.close(); 157 if (conn != null) conn.close(); 158 } 159 catch (Exception ignore) 160 { 161 } 162 throw new RemoteException ("This author does not exist (got exception: " +e+")<br>"); 163 } 164 165 html.append(printCommentFooter()); 166 } 167 } 168 catch (Exception e) 169 { 170 try 171 { 172 if (stmt != null) stmt.close(); 173 if (conn != null) conn.close(); 174 } 175 catch (Exception ignore) 176 { 177 } 178 throw new RemoteException ("Exception getting comment list: " + e +"<br>"); 179 } 180 return html.toString(); 181 } 182 183 184 191 public String getUserInfo(int userId) throws RemoteException 192 { 193 StringBuffer html = new StringBuffer (); 194 Connection conn = null; 195 PreparedStatement stmt = null; 196 ResultSet rs = null; 197 198 199 try 201 { 202 conn = dataSource.getConnection(); 203 stmt = conn.prepareStatement("SELECT * FROM users WHERE id=?"); 204 stmt.setInt(1, userId); 205 rs = stmt.executeQuery(); 206 } 207 catch (SQLException e) 208 { 209 try 210 { 211 if (stmt != null) stmt.close(); 212 if (conn != null) conn.close(); 213 } 214 catch (Exception ignore) 215 { 216 } 217 throw new RemoteException ("Failed to get user information from database: " +e); 218 } 219 220 try 221 { 222 if (rs.first()) 223 { 224 String firstname = rs.getString("firstname"); 225 String lastname = rs.getString("lastname"); 226 String nickname = rs.getString("nickname"); 227 String email = rs.getString("email"); 228 String date = rs.getString("creation_date"); 229 int rating = rs.getInt("rating"); 230 231 html.append(getHTMLGeneralUserInformation(firstname, lastname, nickname, email, date, rating)); 232 html.append(getComments(userId, conn)); 233 } 234 else 235 html.append("This user does not exist!<br>"); 236 stmt.close(); 237 conn.close(); 238 } 239 catch (Exception e) 240 { 241 try 242 { 243 if (stmt != null) stmt.close(); 244 if (conn != null) conn.close(); 245 } 246 catch (Exception ignore) 247 { 248 } 249 throw new RemoteException ("Cannot get user information (got exception: " +e+")<br>"); 250 } 251 return html.toString(); 252 } 253 254 261 public String getHTMLGeneralUserInformation(String firstname, String lastname, String nickname, String email, String creationDate, int rating) throws RemoteException 262 { 263 return "<h2>Information about "+nickname+"<br></h2>"+ 264 "Real life name : "+firstname+" "+lastname+"<br>" 265 +"Email address : "+email+"<br>" 266 +"User since : "+creationDate+"<br>" 267 +"Current rating : <b>"+rating+"</b><br>"; 268 } 269 270 276 public String printCommentHeader() 277 { 278 return "<DL>\n"; 279 } 280 281 288 public String printComment(String userName, String date, String comment, int fromUserId) throws RemoteException 289 { 290 return "<DT><b><BIG><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+fromUserId+"\">"+userName+"</a></BIG></b>"+ 291 " wrote the "+date+"<DD><i>"+comment+"</i><p>\n"; 292 } 293 294 300 public String printCommentFooter() 301 { 302 return "</DL>\n"; 303 } 304 305 306 307 309 320 public void setMessageDrivenContext(MessageDrivenContext ctx) 321 { 322 messageDrivenContext = ctx; 323 if (dataSource == null) 324 { 325 try 327 { 328 initialContext = new InitialContext (); 329 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 330 } 331 catch (Exception e) 332 { 333 throw new EJBException ("Cannot get JNDI InitialContext"); 334 } 335 } 336 } 337 338 342 public void ejbCreate() 343 { 344 345 } 346 347 356 public void ejbRemove() {} 357 358 359 360 } 361 | Popular Tags |