KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 items
23  * that belong to a specific category.
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_SearchItemsByCategoryBean 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 the items in a specific category.
38    *
39    * @return a string that is the list of items in html format
40    * @since 1.1
41    */

42   public String JavaDoc getItems(Integer JavaDoc categoryId, int page, int nbOfItems) throws RemoteException JavaDoc
43   {
44     
45     Enumeration JavaDoc list;
46     ItemPK itemPK;
47     ItemHome iHome;
48     Item item;
49     Query query;
50     QueryHome qHome;
51     StringBuffer JavaDoc html = new StringBuffer JavaDoc();
52
53     try
54     {
55       qHome = (QueryHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Query"), QueryHome.class);
56       query = qHome.create();
57     }
58     catch (Exception JavaDoc e)
59     {
60       throw new RemoteException JavaDoc("Cannot lookup Query: " +e);
61     }
62     try
63     {
64       iHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Item"), ItemHome.class);
65     }
66     catch (Exception JavaDoc e)
67     {
68       throw new RemoteException JavaDoc("Cannot lookup Item: " +e);
69     }
70
71     utx = sessionContext.getUserTransaction();
72
73     try
74     {
75       utx.begin();
76       list = query.getCurrentItemsInCategory(categoryId, page*nbOfItems, nbOfItems).elements();
77       while (list.hasMoreElements())
78       {
79         itemPK = (ItemPK)list.nextElement();
80         item = iHome.findByPrimaryKey(itemPK);
81         html.append(printItem(item));
82       }
83       utx.commit();
84     }
85     catch (Exception JavaDoc e)
86     {
87       try
88       {
89         utx.rollback();
90         throw new RemoteException JavaDoc("Cannot get items list: " +e);
91       }
92       catch (Exception JavaDoc se)
93       {
94         throw new RemoteException JavaDoc("Transaction rollback failed: " + e);
95       }
96     }
97     return html.toString();
98   }
99
100
101   /**
102    * Item related printed function
103    *
104    * @param item the item to display
105    * @return a string in html format
106    * @since 1.1
107    */

108   public String JavaDoc printItem(Item item) throws RemoteException JavaDoc
109   {
110     try
111     {
112       return item.printItem();
113     }
114     catch (RemoteException JavaDoc re)
115     {
116       throw new RemoteException JavaDoc("Unable to print Item (exception: "+re+")<br>\n");
117     }
118   }
119   
120
121
122   // ======================== EJB related methods ============================
123

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

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

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

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