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 19 25 26 public class SB_ViewBidHistoryBean implements SessionBean 27 { 28 protected SessionContext sessionContext; 29 protected Context initialContext = null; 30 protected DataSource dataSource = null; 31 33 34 40 public String getBidHistory(Integer itemId) throws RemoteException 41 { 42 StringBuffer html; 43 ItemHome iHome; 44 Item item; 45 Query query; 46 QueryHome qHome; 47 BidHome bidHome; 48 Bid bid; 49 Enumeration bidList=null; 50 51 try 52 { 53 qHome = (QueryHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Query"), QueryHome.class); 54 query = qHome.create(); 55 } 56 catch (Exception e) 57 { 58 throw new RemoteException ("Cannot lookup Query: " +e); 59 } 60 try 61 { 62 iHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Item"), ItemHome.class); 63 item = iHome.findByPrimaryKey(new ItemPK(itemId)); 64 html = new StringBuffer ("<center><h3>Bid History for "+item.getName()+"<br></h3></center>"); 65 } 66 catch (Exception e) 67 { 68 throw new RemoteException ("Cannot lookup Item ("+itemId+"): " +e); 69 } 70 try 71 { 72 query = qHome.create(); 73 bidList = query.getItemBidHistory(itemId).elements(); 74 } 75 catch (Exception e) 76 { 77 throw new RemoteException ("Exception getting bids list: " +e+"<br>"); 78 } 79 if ((bidList == null) || (!bidList.hasMoreElements())) 80 { 81 return html.append("<h3>There is no bid corresponding to this item.</h3><br>").toString(); 82 } 83 try 85 { 86 bidHome = (BidHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Bid"), 87 BidHome.class); 88 } 89 catch (Exception e) 90 { 91 throw new RemoteException ("Cannot lookup Bid: " +e+"<br>"); 92 } 93 try 94 { 95 html.append(printBidHistoryHeader()); 96 while (bidList.hasMoreElements()) 97 { 98 bid = bidHome.findByPrimaryKey((BidPK)bidList.nextElement()); 100 html.append(bid.printBidHistory()); 101 } 102 html.append(printBidHistoryFooter()); 103 } 104 catch (Exception e) 105 { 106 throw new RemoteException ("Exception getting bid: " + e +"<br>"); 107 } 108 return html.toString(); 109 } 110 111 117 public String printBidHistoryHeader() 118 { 119 return "<TABLE border=\"1\" summary=\"List of bids\">\n<THEAD>\n"+ 120 "<TR><TH>User ID<TH>Bid amount<TH>Date of bid\n<TBODY>\n"; 121 } 122 123 129 public String printBidHistoryFooter() 130 { 131 return "</TABLE>\n"; 132 } 133 134 136 139 public void ejbCreate() throws CreateException , RemoteException 140 { 141 } 142 143 144 public void ejbActivate() throws RemoteException {} 145 146 public void ejbPassivate() throws RemoteException {} 147 148 public void ejbRemove() throws RemoteException {} 149 150 151 160 public void setSessionContext(SessionContext sessionContext) throws RemoteException 161 { 162 this.sessionContext = sessionContext; 163 if (dataSource == null) 164 { 165 167 try 168 { 169 initialContext = new InitialContext (); 170 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 171 } 172 catch (Exception e) 173 { 174 throw new RemoteException ("Cannot get JNDI InitialContext"); 175 } 176 } 177 } 178 179 } 180 | Popular Tags |