1 package edu.rice.rubis.beans; 2 3 import java.rmi.RemoteException ; 4 import javax.ejb.SessionBean ; 5 import javax.ejb.SessionContext ; 6 import javax.ejb.FinderException ; 7 import javax.ejb.ObjectNotFoundException ; 8 import javax.ejb.CreateException ; 9 import javax.ejb.RemoveException ; 10 import javax.ejb.EJBException ; 11 import javax.naming.Context ; 12 import javax.naming.InitialContext ; 13 import javax.rmi.PortableRemoteObject ; 14 import javax.sql.DataSource ; 15 import java.io.Serializable ; 16 import javax.transaction.UserTransaction ; 17 import java.util.Enumeration ; 18 import java.util.Iterator ; 19 import java.net.URLEncoder ; 20 21 27 28 public class SB_ViewItemBean implements SessionBean 29 { 30 protected SessionContext sessionContext; 31 protected Context initialContext = null; 32 protected DataSource dataSource = null; 33 private UserTransaction utx = null; 34 35 41 public String getItemDescription(Integer itemId, int userId) throws RemoteException 42 { 43 StringBuffer html = new StringBuffer (); 44 ItemHome iHome = null; 45 Item item = null; 46 47 try 48 { 49 iHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Item"), ItemHome.class); 50 item = iHome.findByPrimaryKey(new ItemPK(itemId)); 51 } 52 catch (Exception e) 53 { 54 throw new RemoteException ("Cannot lookup Item: " +e); 55 } 56 try 57 { 58 float buyNow = 0; 59 int nbOfBids = item.getNbOfBids(); 60 int qty = item.getQuantity(); 61 String firstBid; 62 float maxBid = item.getMaxBid(); 63 String itemName = item.getName(); 64 65 if (maxBid == 0) 66 { 67 firstBid = "none"; 68 maxBid = item.getInitialPrice(); 69 buyNow = item.getBuyNow(); 70 } 71 else 72 { 73 if (qty > 1) 74 { 75 BidHome bHome = null; 76 QueryHome qHome = null; 77 Query query = null; 78 try 79 { qHome = (QueryHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Query"), QueryHome.class); 81 query = qHome.create(); 82 } 83 catch (Exception e) 84 { 85 throw new RemoteException ("Cannot lookup Query: " +e); 86 } 87 try 88 { 89 bHome = (BidHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Bid"), BidHome.class); 91 } 92 catch (Exception e) 93 { 94 throw new RemoteException ("Cannot lookup Bid: " +e); 95 } 96 try 97 { 98 101 Enumeration list = query.getItemQtyMaxBid(qty, itemId).elements(); 102 Bid bid; 103 int numberOfItems = 0; 104 while (list.hasMoreElements()) 105 { 106 bid = bHome.findByPrimaryKey((BidPK)list.nextElement()); 107 numberOfItems += bid.getQuantity(); 108 if (numberOfItems >= qty) 109 { 110 maxBid = bid.getBid(); 111 break; 112 } 113 } 114 } 115 catch (Exception e) 116 { 117 throw new RemoteException ("Problem while computing current bid: "+e+"<br>"); 118 } 119 } 120 121 Float foo = new Float (maxBid); 122 firstBid = foo.toString(); 123 } 124 125 if (userId>0) 126 { 127 html.append(printHTMLHighlighted("You are ready to bid on: "+itemName)); 129 } 130 else 131 { 132 html.append(printHTMLHighlighted(itemName)); 134 } 135 html.append("<TABLE>\n"+ 136 "<TR><TD>Currently<TD><b><BIG>"+maxBid+"</BIG></b>\n"); 137 float reservePrice = item.getReservePrice(); 139 Integer sellerId = item.getSellerId(); 140 if (reservePrice > 0) 141 { if (maxBid >= reservePrice) 143 html.append("(The reserve price has been met)\n"); 144 else 145 html.append("(The reserve price has NOT been met)\n"); 146 } 147 html.append("<TR><TD>Quantity<TD><b><BIG>"+qty+"</BIG></b>\n"+ 148 "<TR><TD>First bid<TD><b><BIG>"+firstBid+"</BIG></b>\n"+ 149 "<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"+ 150 "<TR><TD>Seller<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+sellerId+"\">"+item.getSellerNickname()+"</a> (<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutCommentAuth?to="+sellerId+"&itemId="+itemId+"\">Leave a comment on this user</a>)\n"+ 151 "<TR><TD>Started<TD>"+item.getStartDate()+"\n"+ 152 "<TR><TD>Ends<TD>"+item.getEndDate()+"\n"+ 153 "</TABLE>"); 154 if (buyNow > 0) 156 html.append("<p><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.BuyNowAuth?itemId="+itemId+"\">"+ 157 "<IMG SRC=\""+BeanConfig.context+"/buy_it_now.jpg\" height=22 width=150></a>"+ 158 " <BIG><b>You can buy this item right now for only $"+buyNow+"</b></BIG><br><p>\n"); 159 160 if (userId<=0) 161 { 162 html.append("<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutBidAuth?itemId="+itemId+"\"><IMG SRC=\"/EJB_HTML/bid_now.jpg\" height=22 width=90> on this item</a>\n"); 163 } 164 165 html.append(printHTMLHighlighted("Item description")); 166 html.append(item.getDescription()); 167 html.append("<br><p>\n"); 168 169 if (userId>0) 170 { 171 html.append(printHTMLHighlighted("Bidding")); 172 float minBid = maxBid+1; 173 html.append("<form action=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.StoreBid\" method=POST>\n"+ 174 "<input type=hidden name=minBid value="+minBid+">\n"+ 175 "<input type=hidden name=userId value="+userId+">\n"+ 176 "<input type=hidden name=itemId value="+itemId+">\n"+ 177 "<input type=hidden name=maxQty value="+qty+">\n"+ 178 "<center><table>\n"+ 179 "<tr><td>Your bid (minimum bid is "+minBid+"):</td>\n"+ 180 "<td><input type=text size=10 name=bid></td></tr>\n"+ 181 "<tr><td>Your maximum bid:</td>\n"+ 182 "<td><input type=text size=10 name=maxBid></td></tr>\n"); 183 if (qty > 1) 184 html.append("<tr><td>Quantity:</td>\n"+ 185 "<td><input type=text size=5 name=qty></td></tr>\n"); 186 else 187 html.append("<input type=hidden name=qty value=1>\n"); 188 html.append("</table><p><input type=submit value=\"Bid now!\"></center><p>\n"); 189 } 190 } 191 catch (RemoteException re) 192 { 193 throw new RemoteException ("Unable to print Item description (exception: "+re+")<br>\n"); 194 } 195 return html.toString(); 196 } 197 198 204 public String printHTMLHighlighted(String msg) 205 { 206 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"; 207 } 208 209 210 212 215 public void ejbCreate() throws CreateException , RemoteException 216 { 217 } 218 219 220 public void ejbActivate() throws RemoteException {} 221 222 public void ejbPassivate() throws RemoteException {} 223 224 public void ejbRemove() throws RemoteException {} 225 226 227 236 public void setSessionContext(SessionContext sessionContext) throws RemoteException 237 { 238 this.sessionContext = sessionContext; 239 if (dataSource == null) 240 { 241 243 try 244 { 245 initialContext = new InitialContext (); 246 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 247 } 248 catch (Exception e) 249 { 250 throw new RemoteException ("Cannot get JNDI InitialContext"); 251 } 252 } 253 } 254 255 } 256 | Popular Tags |