KickJava   Java API By Example, From Geeks To Geeks.

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


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

107   public String JavaDoc printItem(Item item) throws RemoteException JavaDoc
108   {
109     try
110     {
111       return item.printItem();
112     }
113     catch (RemoteException JavaDoc re)
114     {
115       throw new RemoteException JavaDoc("Unable to print Item (exception: "+re+")<br>\n");
116     }
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