KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 /**
19  * This is a stateless session bean used to build the html form to buy an item.
20  *
21  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
22  * @version 1.1
23  */

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

32
33   /**
34    * Authenticate the user and get the information to build the html form.
35    *
36    * @return a string in html format
37    * @since 1.1
38    */

39   public String JavaDoc getBuyNowForm(Integer JavaDoc itemId, String JavaDoc username, String JavaDoc password) throws RemoteException JavaDoc
40   {
41     int userId = -1;
42     String JavaDoc html = "";
43
44     // Authenticate the user who want to comment
45
if ((username != null && !username.equals("")) || (password != null && !password.equals("")))
46       {
47         SB_AuthHome authHome = null;
48         SB_Auth auth = null;
49         try
50         {
51           authHome = (SB_AuthHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/SB_Auth"), SB_AuthHome.class);
52           auth = authHome.create();
53         }
54         catch (Exception JavaDoc e)
55         {
56           throw new RemoteException JavaDoc("Cannot lookup SB_Auth: " +e);
57         }
58         try
59         {
60           userId = auth.authenticate(username, password);
61         }
62         catch (Exception JavaDoc e)
63         {
64           throw new RemoteException JavaDoc("Authentication failed: " +e);
65         }
66         if (userId == -1)
67         {
68            html = (" You don't have an account on RUBiS!<br>You have to register first.<br>");
69            return html;
70         }
71       }
72       // Try to find the Item corresponding to the Item ID
73
ItemHome itemHome;
74       try
75       {
76         itemHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Item"),
77                                                          ItemHome.class);
78       }
79       catch (Exception JavaDoc e)
80       {
81         throw new RemoteException JavaDoc("Cannot lookup Item: " +e+"<br>");
82       }
83       try
84       {
85         Item item = itemHome.findByPrimaryKey(new ItemPK(itemId));
86
87         // Display the form for buying the item
88
html = printItemDescriptionToBuyNow(item, userId);
89       }
90       catch (Exception JavaDoc e)
91       {
92         throw new RemoteException JavaDoc("Exception getting the item information: "+ e +"<br>");
93       }
94  
95     return html;
96   }
97                    
98   /**
99    * Print the full description of an item and the buy now option
100    *
101    * @param item an <code>Item</code> value
102    * @param userId an authenticated user id
103    */

104   public String JavaDoc printItemDescriptionToBuyNow(Item item, int userId) throws RemoteException JavaDoc
105   {
106     String JavaDoc html = "";
107     try
108     {
109       String JavaDoc itemName = item.getName();
110       html = html + "<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>You are ready to buy this item: "+itemName+"</B></FONT></TD></TR>\n</TABLE><p>\n" + item.printItemDescriptionToBuyNow(userId);
111       ;
112     }
113     catch (RemoteException JavaDoc re)
114     {
115       throw new RemoteException JavaDoc("Unable to print Item description (exception: "+re+")<br>\n");
116     }
117     return html;
118   }
119
120
121
122
123   // ======================== EJB related methods ============================
124

125   /**
126    * This method is empty for a stateless session bean
127    */

128   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
129   {
130   }
131
132   /** This method is empty for a stateless session bean */
133   public void ejbActivate() throws RemoteException JavaDoc {}
134   /** This method is empty for a stateless session bean */
135   public void ejbPassivate() throws RemoteException JavaDoc {}
136   /** This method is empty for a stateless session bean */
137   public void ejbRemove() throws RemoteException JavaDoc {}
138
139
140   /**
141    * Sets the associated session context. The container calls this method
142    * after the instance creation. This method is called with no transaction context.
143    * We also retrieve the Home interfaces of all RUBiS's beans.
144    *
145    * @param sessionContext - A SessionContext interface for the instance.
146    * @exception RemoteException - Thrown if the instance could not perform the function
147    * requested by the container because of a system-level error.
148    */

149   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
150   {
151     this.sessionContext = sessionContext;
152     if (dataSource == null)
153     {
154       // Finds DataSource from JNDI
155

156       try
157       {
158         initialContext = new InitialContext JavaDoc();
159         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
160       }
161       catch (Exception JavaDoc e)
162       {
163         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
164       }
165     }
166   }
167
168 }
169
Popular Tags