KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.net.URLEncoder JavaDoc;
20
21 /**
22  * This is a stateless session bean used to get the list of
23  * categories from database and return the information to the BrowseRegions servlet.
24  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
25  * @version 1.1
26  */

27
28 public class SB_BrowseCategoriesBean implements SessionBean JavaDoc
29 {
30   protected SessionContext JavaDoc sessionContext;
31   protected Context JavaDoc initialContext = null;
32   protected DataSource JavaDoc dataSource = null;
33   private UserTransaction JavaDoc utx = null;
34
35
36   /**
37    * Get all the categories from the database.
38    *
39    * @return a string that is the list of categories in html format
40    * @since 1.1
41    */

42   /** List all the categories in the database */
43   public String JavaDoc getCategories(String JavaDoc regionName, String JavaDoc username, String JavaDoc password) throws RemoteException JavaDoc
44   {
45     Collection JavaDoc list;
46     Category cat;
47     CategoryHome home = null;
48     String JavaDoc html = "";
49     int regionId = -1;
50     int userId = -1;
51
52     if (regionName != null && regionName !="")
53     {
54       // Connecting to region Home interface thru JNDI
55
RegionHome regionHome = null;
56       try
57       {
58         regionHome = (RegionHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Region"),
59                                                        RegionHome.class);
60       }
61       catch (Exception JavaDoc e)
62       {
63         throw new RemoteException JavaDoc("Cannot lookup Region: " +e);
64       }
65       // get the region ID
66
try
67       {
68         Region region = regionHome.findByName(regionName);
69         regionId = region.getId().intValue();
70       }
71       catch (Exception JavaDoc e)
72       {
73         throw new RemoteException JavaDoc(" Region "+regionName+" does not exist in the database!<br>(got exception: " +e+")");
74       }
75     }
76     else
77     {
78       // Authenticate the user who wants to sell items
79
if ((username != null && !username.equals("")) || (password != null && !password.equals("")))
80       {
81         SB_AuthHome authHome = null;
82         SB_Auth auth = null;
83         try
84         {
85           authHome = (SB_AuthHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/SB_Auth"), SB_AuthHome.class);
86           auth = authHome.create();
87         }
88         catch (Exception JavaDoc e)
89         {
90           throw new RemoteException JavaDoc("Cannot lookup SB_Auth: " +e);
91         }
92         try
93         {
94           userId = auth.authenticate(username, password);
95         }
96         catch (Exception JavaDoc e)
97         {
98           throw new RemoteException JavaDoc("Authentication failed: " +e);
99         }
100         if (userId == -1)
101         {
102            html = (" You don't have an account on RUBiS!<br>You have to register first.<br>");
103            return html;
104         }
105       }
106     }
107
108     // Connecting to Category Home
109
try
110     {
111       home = (CategoryHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Category"), CategoryHome.class);
112     }
113     catch (Exception JavaDoc e)
114     {
115       throw new RemoteException JavaDoc("Cannot lookup Category: " +e);
116     }
117
118     utx = sessionContext.getUserTransaction();
119
120     try
121     {
122       utx.begin();
123       list = home.findAllCategories();
124       if (list.isEmpty())
125         html = ("<h2>Sorry, but there is no category available at this time. Database table is empty</h2><br>");
126       else
127       {
128         Iterator JavaDoc it = list.iterator();
129         while (it.hasNext())
130         {
131           cat = (Category)it.next();
132           if (regionId != -1)
133           {
134             html = html + printCategoryByRegion(cat, regionId);
135           }
136           else
137           {
138             if (userId != -1)
139               html = html + printCategoryToSellItem(cat, userId);
140             else
141               html = html + printCategory(cat);
142           }
143         }
144       }
145       utx.commit();
146     }
147     catch (Exception JavaDoc e)
148     {
149       try
150       {
151         utx.rollback();
152         throw new RemoteException JavaDoc("Exception getting category list: " + e);
153       }
154       catch (Exception JavaDoc se)
155       {
156         throw new RemoteException JavaDoc("Transaction rollback failed: " + e);
157       }
158     }
159     return html;
160   }
161
162
163   /**
164    * Category related printed functions
165    *
166    * @param category the category to display
167    * @return a string in html format
168    * @since 1.1
169    */

170
171   public String JavaDoc printCategory(Category category) throws RemoteException JavaDoc
172   {
173     String JavaDoc html = "";
174     try
175     {
176       html = (category.printCategory());
177     }
178     catch (RemoteException JavaDoc re)
179     {
180       throw new RemoteException JavaDoc("Unable to print Category 1 (exception: "+re+")");
181     }
182     return html;
183   }
184
185   /**
186    * List all the categories with links to browse items by region
187    * @return a string in html format
188    * @since 1.1
189    */

190   public String JavaDoc printCategoryByRegion(Category category, int regionId) throws RemoteException JavaDoc
191   {
192     String JavaDoc html = "";
193     try
194     {
195       html = (category.printCategoryByRegion(regionId));
196     }
197     catch (RemoteException JavaDoc re)
198     {
199       throw new RemoteException JavaDoc("Unable to print Category 2 (exception: "+re+")<br>\n");
200     }
201     return html;
202   }
203
204   /**
205    * Lists all the categories and links to the sell item page
206    * @return a string in html format
207    * @since 1.1
208    */

209   public String JavaDoc printCategoryToSellItem(Category category, int userId) throws RemoteException JavaDoc
210   {
211     String JavaDoc html= "";
212     try
213     {
214       html = (category.printCategoryToSellItem(userId));
215     }
216     catch (RemoteException JavaDoc re)
217     {
218       throw new RemoteException JavaDoc("Unable to print Category 3 (exception: "+re+")<br>\n");
219     }
220     return html;
221   }
222
223
224
225   // ======================== EJB related methods ============================
226

227   /**
228    * This method is empty for a stateless session bean
229    */

230   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
231   {
232   }
233
234   /** This method is empty for a stateless session bean */
235   public void ejbActivate() throws RemoteException JavaDoc {}
236   /** This method is empty for a stateless session bean */
237   public void ejbPassivate() throws RemoteException JavaDoc {}
238   /** This method is empty for a stateless session bean */
239   public void ejbRemove() throws RemoteException JavaDoc {}
240
241
242   /**
243    * Sets the associated session context. The container calls this method
244    * after the instance creation. This method is called with no transaction context.
245    * We also retrieve the Home interfaces of all RUBiS's beans.
246    *
247    * @param sessionContext - A SessionContext interface for the instance.
248    * @exception RemoteException - Thrown if the instance could not perform the function
249    * requested by the container because of a system-level error.
250    */

251   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
252   {
253     this.sessionContext = sessionContext;
254     if (dataSource == null)
255     {
256       // Finds DataSource from JNDI
257

258       try
259       {
260         initialContext = new InitialContext JavaDoc();
261         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
262       }
263       catch (Exception JavaDoc e)
264       {
265         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
266       }
267     }
268   }
269
270 }
271
Popular Tags