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_ViewItem 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_ViewItem() 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 itemId = request.getInt("itemId"); 51 int userId = request.getInt("userId"); 52 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 54 55 String html = getItemDescription(itemId, 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_ViewItem: " +e); 81 } 82 } 83 84 90 public String getItemDescription(int itemId, int userId) throws RemoteException 91 { 92 StringBuffer html = new StringBuffer (); 93 Connection conn = null; 94 PreparedStatement stmt = null; 95 ResultSet rs = null; 96 97 String itemName=null, endDate=null, startDate=null, description=null, sellerName=null; 98 float maxBid=0, initialPrice=0, buyNow=0, reservePrice=0; 99 int qty=0, sellerId=-1, nbOfBids=0; 100 String firstBid=null; 101 102 try 103 { 104 conn = dataSource.getConnection(); 105 conn.setAutoCommit(false); 106 stmt = conn.prepareStatement("SELECT * FROM items WHERE id=?"); 107 stmt.setInt(1, itemId); 108 rs = stmt.executeQuery(); 109 } 110 catch (SQLException e) 111 { 112 try 113 { 114 if (stmt != null) stmt.close(); 115 if (conn != null) conn.close(); 116 } 117 catch (Exception ignore) 118 { 119 } 120 throw new RemoteException ("Failed to get the item: " +e); 121 } 122 try 123 { 124 if (!rs.first()) 125 { 126 stmt = conn.prepareStatement("SELECT * FROM old_items WHERE id=?"); 127 stmt.setInt(1, itemId); 128 rs = stmt.executeQuery(); 129 } 130 } 131 catch (SQLException e) 132 { 133 try 134 { 135 if (stmt != null) stmt.close(); 136 if (conn != null) conn.close(); 137 } 138 catch (Exception ignore) 139 { 140 } 141 throw new RemoteException ("Failed to get the item from old items: " +e); 142 } 143 try 144 { 145 if (rs.first()) 146 { 147 itemName = rs.getString("name"); 148 description = rs.getString("description"); 149 endDate = rs.getString("end_date"); 150 startDate = rs.getString("start_date"); 151 initialPrice = rs.getFloat("initial_price"); 152 reservePrice = rs.getFloat("reserve_price"); 153 qty = rs.getInt("quantity"); 154 sellerId = rs.getInt("seller"); 155 156 maxBid = rs.getFloat("max_bid"); 157 nbOfBids = rs.getInt("nb_of_bids"); 158 159 PreparedStatement sellerStmt = null; 160 ResultSet sellerResult = null; 161 try 162 { 163 sellerStmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?"); 164 sellerStmt.setInt(1, sellerId); 165 sellerResult = sellerStmt.executeQuery(); 166 if (sellerResult.first()) 168 sellerName = sellerResult.getString("nickname"); 169 sellerStmt.close(); } 171 catch (SQLException e) 172 { 173 try 174 { 175 if (sellerStmt != null) sellerStmt.close(); if (conn != null) conn.close(); 177 } 178 catch (Exception ignore) 179 { 180 } 181 throw new RemoteException ("Failed to execute Query for seller: " +e); 182 } 183 } 184 185 if (maxBid == 0) 186 { 187 firstBid = "none"; 188 maxBid = initialPrice; 189 buyNow = rs.getFloat("buy_now"); 190 } 191 else 192 { 193 if (qty > 1) 194 { 195 PreparedStatement bidStmt = null; 196 ResultSet bidResult = null; 197 try 198 { 199 202 bidStmt = conn.prepareStatement("SELECT bids.id, bids.qty, bids.bid FROM bids WHERE item_id=? ORDER BY bid DESC LIMIT ?"); 203 bidStmt.setInt(1, itemId); 204 bidStmt.setInt(2, qty); 205 bidResult = bidStmt.executeQuery(); 206 } 207 catch (SQLException e) 208 { 209 try 210 { 211 if (bidStmt != null) bidStmt.close(); if (conn != null) conn.close(); 213 } 214 catch (Exception ignore) 215 { 216 } 217 } 218 219 try 220 { 221 float bidValue; 222 int numberOfItems = 0; 223 while (bidResult.next()) 224 { 225 bidValue = bidResult.getFloat("bid"); 226 numberOfItems += bidResult.getInt("qty"); 227 if (numberOfItems >= qty) 228 { 229 maxBid = bidValue; 230 break; 231 } 232 } 233 bidStmt.close(); } 235 catch (Exception e) 236 { 237 try 238 { 239 if (stmt != null) stmt.close(); 240 if (conn != null) conn.close(); 241 } 242 catch (Exception ignore) 243 { 244 } 245 throw new RemoteException ("Problem while computing current bid: "+e+"<br>"); 246 } 247 Float foo = new Float (maxBid); 248 firstBid = foo.toString(); 249 } 250 } 251 if (stmt != null) stmt.close(); 252 if (conn != null) conn.close(); 253 254 if (userId>0) 255 { 256 html.append(printHTMLHighlighted("You are ready to bid on: "+itemName)); 257 } 258 else 259 { 260 html.append(printHTMLHighlighted(itemName)); 261 } 262 html.append("<TABLE>\n"+ 263 "<TR><TD>Currently<TD><b><BIG>"+maxBid+"</BIG></b>\n"); 264 if (reservePrice > 0) 266 { if (maxBid >= reservePrice) 268 html.append("(The reserve price has been met)\n"); 269 else 270 html.append("(The reserve price has NOT been met)\n"); 271 } 272 html.append("<TR><TD>Quantity<TD><b><BIG>"+qty+"</BIG></b>\n"+ 273 "<TR><TD>First bid<TD><b><BIG>"+firstBid+"</BIG></b>\n"+ 274 "<TR><TD># of bids<TD><b><BIG>"+nbOfBids+"</BIG></b> (<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewBidHistory?itemId="+itemId+"\">bid history</a>)\n"+ 275 "<TR><TD>Seller<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+sellerId+"\">"+sellerName+"</a> (<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutCommentAuth?to="+sellerId+"&itemId="+itemId+"\">Leave a comment on this user</a>)\n"+ 276 "<TR><TD>Started<TD>"+startDate+"\n"+ 277 "<TR><TD>Ends<TD>"+endDate+"\n"+ 278 "</TABLE>"); 279 if (buyNow > 0) 281 html.append("<p><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.BuyNowAuth?itemId="+itemId+"\">"+ 282 "<IMG SRC=\""+BeanConfig.context+"/buy_it_now.jpg\" height=22 width=150></a>"+ 283 " <BIG><b>You can buy this item right now for only $"+buyNow+"</b></BIG><br><p>\n"); 284 285 if (userId<=0) 286 { 287 html.append("<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutBidAuth?itemId="+itemId+"\"><IMG SRC=\""+BeanConfig.context+"/bid_now.jpg\" height=22 width=90> on this item</a>\n"); 288 } 289 290 html.append(printHTMLHighlighted("Item description")); 291 html.append(description); 292 html.append("<br><p>\n"); 293 294 if (userId>0) 295 { 296 html.append(printHTMLHighlighted("Bidding")); 297 float minBid = maxBid+1; 298 html.append("<form action=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.StoreBid\" method=POST>\n"+ 299 "<input type=hidden name=minBid value="+minBid+">\n"+ 300 "<input type=hidden name=userId value="+userId+">\n"+ 301 "<input type=hidden name=itemId value="+itemId+">\n"+ 302 "<input type=hidden name=maxQty value="+qty+">\n"+ 303 "<center><table>\n"+ 304 "<tr><td>Your bid (minimum bid is "+minBid+"):</td>\n"+ 305 "<td><input type=text size=10 name=bid></td></tr>\n"+ 306 "<tr><td>Your maximum bid:</td>\n"+ 307 "<td><input type=text size=10 name=maxBid></td></tr>\n"); 308 if (qty > 1) 309 html.append("<tr><td>Quantity:</td>\n"+ 310 "<td><input type=text size=5 name=qty></td></tr>\n"); 311 else 312 html.append("<input type=hidden name=qty value=1>\n"); 313 html.append("</table><p><input type=submit value=\"Bid now!\"></center><p>\n"); 314 } 315 } 316 catch (Exception e) 317 { 318 throw new RemoteException ("Unable to print Item description (exception: "+e+")<br>\n"); 319 } 320 return html.toString(); 321 } 322 323 329 public String printHTMLHighlighted(String msg) 330 { 331 return "<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>"+msg+"</B></FONT></TD></TR>\n</TABLE><p>\n"; 332 } 333 334 335 337 348 public void setMessageDrivenContext(MessageDrivenContext ctx) 349 { 350 messageDrivenContext = ctx; 351 if (dataSource == null) 352 { 353 try 355 { 356 initialContext = new InitialContext (); 357 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 358 } 359 catch (Exception e) 360 { 361 throw new EJBException ("Cannot get JNDI InitialContext"); 362 } 363 } 364 } 365 366 370 public void ejbCreate() 371 { 372 373 } 374 375 384 public void ejbRemove() {} 385 386 387 388 } 389 | Popular Tags |