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 18 24 25 public class MDB_ViewBidHistory implements MessageDrivenBean , MessageListener 26 { 27 private DataSource dataSource; 28 private MessageDrivenContext messageDrivenContext; 29 private TopicConnectionFactory connectionFactory; 30 private TopicConnection connection; 31 private Topic topic; 32 private TopicSession session; 33 private TopicPublisher replier; 34 private Context initialContext = null; 35 36 37 public MDB_ViewBidHistory() 38 { 39 40 } 41 42 public void onMessage(Message message) 43 { 44 try 45 { 46 MapMessage request = (MapMessage)message; 47 String correlationID = request.getJMSCorrelationID(); 48 int itemId = request.getInt("itemId"); 49 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 51 String html = getBidHistory(itemId); 53 TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); 55 if (temporaryTopic != null) 56 { 57 connection = connectionFactory.createTopicConnection(); 59 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 61 TextMessage reply = session.createTextMessage(); 62 reply.setJMSCorrelationID(correlationID); 63 reply.setText(html); 64 replier = session.createPublisher(null); connection.start(); 66 replier.publish(temporaryTopic, reply); 67 replier.close(); 68 session.close(); 69 connection.stop(); 70 connection.close(); 71 } 72 } 73 catch (Exception e) 74 { 75 throw new EJBException ("Message traitment failed for MDB_ViewBidHistory: " +e); 76 } 77 } 78 79 80 86 public String getBidHistory(int itemId) throws RemoteException 87 { 88 StringBuffer html = null; 89 PreparedStatement stmt = null; 90 ResultSet rs = null; 91 Connection conn = null; 92 String date = null, bidderName = null, itemName = null; 93 float bid = 0; 94 int userId = -1; 95 96 try 98 { 99 conn = dataSource.getConnection(); 100 stmt = conn.prepareStatement("SELECT name FROM items WHERE id=?"); 101 stmt.setInt(1, itemId); 102 rs = stmt.executeQuery(); 103 } 104 catch (Exception e) 105 { 106 try 107 { 108 if (stmt != null) stmt.close(); 109 if (conn != null) conn.close(); 110 } 111 catch (Exception ignore) 112 { 113 } 114 throw new RemoteException ("Failed to execute Query for item in items table: " +e); 115 } 116 try 117 { 118 if (!rs.first()) 119 { 120 stmt.close(); 121 stmt = conn.prepareStatement("SELECT name FROM old_items WHERE id=?"); 122 stmt.setInt(1, itemId); 123 rs = stmt.executeQuery(); 124 125 } 126 } 127 catch (Exception e) 128 { 129 try 130 { 131 if (stmt != null) stmt.close(); 132 if (conn != null) conn.close(); 133 } 134 catch (Exception ignore) 135 { 136 } 137 throw new RemoteException ("Failed to execute Query for item in old_items table: " +e); 138 } 139 try 140 { 141 if ((rs == null) || (!rs.first())) { 143 stmt.close(); 144 conn.close(); 145 return ""; 146 } 147 else 148 { 149 itemName = rs.getString("name"); 150 html = new StringBuffer ("<center><h3>Bid History for "+itemName+"<br></h3></center>"); 151 } 152 stmt.close(); 153 } 154 catch (Exception e) 155 { 156 try 157 { 158 if (conn != null) conn.close(); 159 } 160 catch (Exception ignore) 161 { 162 } 163 throw new RemoteException ("This item does not exist (got exception: " +e+")<br>"); 164 } 165 try 167 { 168 stmt = conn.prepareStatement("SELECT * FROM bids WHERE item_id=? ORDER BY date DESC"); 169 stmt.setInt(1, itemId); 170 rs = stmt.executeQuery(); 171 if (!rs.first()) 172 { 173 stmt.close(); 174 conn.close(); 175 return html.append("<h3>There is no bid corresponding to this item.</h3><br>").toString(); 176 } 177 } 178 catch (SQLException e) 179 { 180 try 181 { 182 if (stmt != null) stmt.close(); 183 if (conn != null) conn.close(); 184 } 185 catch (Exception ignore) 186 { 187 } 188 throw new RemoteException ("Exception getting bids list: " +e+"<br>"); 189 } 190 PreparedStatement userStmt = null; 191 try 192 { 193 html.append(printBidHistoryHeader()); 194 userStmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 195 ResultSet urs = null; 196 do 197 { 198 date = rs.getString("date"); 200 bid = rs.getFloat("bid"); 201 userId = rs.getInt("user_id"); 202 203 userStmt.setInt(1, userId); 204 urs = userStmt.executeQuery(); 205 if (urs.first()) 206 bidderName = urs.getString("nickname"); 207 208 html.append(printBidHistory(userId, bidderName, bid, date)); 209 } 210 while(rs.next()); 211 html.append(printBidHistoryFooter()); 212 userStmt.close(); 213 stmt.close(); 214 conn.close(); 215 } 216 catch (SQLException e) 217 { 218 try 219 { 220 if (userStmt != null) userStmt.close(); 221 if (stmt != null) stmt.close(); 222 if (conn != null) conn.close(); 223 } 224 catch (Exception ignore) 225 { 226 } 227 throw new RemoteException ("Exception getting bid: " +e+"<br>"); 228 } 229 return html.toString(); 230 } 231 232 239 public String printBidHistory(int userId, String bidderName, float bid, String date) throws RemoteException 240 { 241 return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+userId+ 242 "\">"+bidderName+"<TD>"+bid+"<TD>"+date+"\n"; 243 } 244 245 251 public String printBidHistoryHeader() 252 { 253 return "<TABLE border=\"1\" summary=\"List of bids\">\n<THEAD>\n"+ 254 "<TR><TH>User ID<TH>Bid amount<TH>Date of bid\n<TBODY>\n"; 255 } 256 257 263 public String printBidHistoryFooter() 264 { 265 return "</TABLE>\n"; 266 } 267 268 270 281 public void setMessageDrivenContext(MessageDrivenContext ctx) 282 { 283 messageDrivenContext = ctx; 284 if (dataSource == null) 285 { 286 try 288 { 289 initialContext = new InitialContext (); 290 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 291 } 292 catch (Exception e) 293 { 294 throw new EJBException ("Cannot get JNDI InitialContext"); 295 } 296 } 297 } 298 299 303 public void ejbCreate() 304 { 305 306 } 307 308 317 public void ejbRemove() {} 318 319 } 320 | Popular Tags |