KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > SB_ViewBidHistoryBean


1 package edu.rice.rubis.beans;
2
3 import java.rmi.RemoteException JavaDoc;
4 import javax.ejb.SessionBean JavaDoc;
5 import javax.ejb.SessionContext JavaDoc;
6 import javax.ejb.FinderException JavaDoc;
7 import javax.ejb.ObjectNotFoundException JavaDoc;
8 import javax.ejb.CreateException JavaDoc;
9 import javax.ejb.RemoveException JavaDoc;
10 import javax.ejb.EJBException JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import javax.naming.InitialContext JavaDoc;
13 import javax.rmi.PortableRemoteObject JavaDoc;
14 import javax.sql.DataSource JavaDoc;
15 import java.io.Serializable JavaDoc;
16 import javax.transaction.UserTransaction JavaDoc;
17 import java.util.Enumeration JavaDoc;
18
19 /**
20  * This is a stateless session bean used get the bid history of an item.
21  *
22  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
23  * @version 1.1
24  */

25
26 public class SB_ViewBidHistoryBean implements SessionBean JavaDoc
27 {
28   protected SessionContext JavaDoc sessionContext;
29   protected Context JavaDoc initialContext = null;
30   protected DataSource JavaDoc dataSource = null;
31   //private UserTransaction utx = null;
32

33
34   /**
35    * Get the list of bids related to a specific item.
36    *
37    * @return a string in html format
38    * @since 1.1
39    */

40   public String JavaDoc getBidHistory(Integer JavaDoc itemId) throws RemoteException JavaDoc
41   {
42     StringBuffer JavaDoc html;
43     ItemHome iHome;
44     Item item;
45     Query query;
46     QueryHome qHome;
47     BidHome bidHome;
48     Bid bid;
49     Enumeration JavaDoc 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 JavaDoc e)
57     {
58       throw new RemoteException JavaDoc("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 JavaDoc("<center><h3>Bid History for "+item.getName()+"<br></h3></center>");
65     }
66     catch (Exception JavaDoc e)
67     {
68       throw new RemoteException JavaDoc("Cannot lookup Item ("+itemId+"): " +e);
69     }
70     try
71     {
72       query = qHome.create();
73       bidList = query.getItemBidHistory(itemId).elements();
74     }
75     catch (Exception JavaDoc e)
76     {
77     throw new RemoteException JavaDoc("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     // Lookup bid home interface
84
try
85     {
86       bidHome = (BidHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Bid"),
87                                                      BidHome.class);
88     }
89     catch (Exception JavaDoc e)
90     {
91       throw new RemoteException JavaDoc("Cannot lookup Bid: " +e+"<br>");
92     }
93     try
94     {
95       html.append(printBidHistoryHeader());
96       while (bidList.hasMoreElements())
97       {
98         // Get the bids
99
bid = bidHome.findByPrimaryKey((BidPK)bidList.nextElement());
100         html.append(bid.printBidHistory());
101       }
102       html.append(printBidHistoryFooter());
103     }
104     catch (Exception JavaDoc e)
105     {
106       throw new RemoteException JavaDoc("Exception getting bid: " + e +"<br>");
107     }
108     return html.toString();
109   }
110
111   /**
112    * Bids list header printed function
113    *
114    * @return a string in html format
115    * @since 1.1
116    */

117   public String JavaDoc 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   /**
124    * Bids list footer printed function
125    *
126    * @return a string in html format
127    * @since 1.1
128    */

129   public String JavaDoc printBidHistoryFooter()
130   {
131     return "</TABLE>\n";
132   }
133
134   // ======================== EJB related methods ============================
135

136   /**
137    * This method is empty for a stateless session bean
138    */

139   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
140   {
141   }
142
143   /** This method is empty for a stateless session bean */
144   public void ejbActivate() throws RemoteException JavaDoc {}
145   /** This method is empty for a stateless session bean */
146   public void ejbPassivate() throws RemoteException JavaDoc {}
147   /** This method is empty for a stateless session bean */
148   public void ejbRemove() throws RemoteException JavaDoc {}
149
150
151   /**
152    * Sets the associated session context. The container calls this method
153    * after the instance creation. This method is called with no transaction context.
154    * We also retrieve the Home interfaces of all RUBiS's beans.
155    *
156    * @param sessionContext - A SessionContext interface for the instance.
157    * @exception RemoteException - Thrown if the instance could not perform the function
158    * requested by the container because of a system-level error.
159    */

160   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
161   {
162     this.sessionContext = sessionContext;
163     if (dataSource == null)
164     {
165       // Finds DataSource from JNDI
166

167       try
168       {
169         initialContext = new InitialContext JavaDoc();
170         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
171       }
172       catch (Exception JavaDoc e)
173       {
174         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
175       }
176     }
177   }
178
179 }
180
Popular Tags