1 package edu.rice.rubis.beans; 2 3 import java.rmi.RemoteException ; 4 import javax.ejb.SessionBean ; 5 import javax.ejb.SessionContext ; 6 import javax.ejb.FinderException ; 7 import javax.ejb.ObjectNotFoundException ; 8 import javax.ejb.CreateException ; 9 import javax.ejb.RemoveException ; 10 import javax.ejb.EJBException ; 11 import javax.naming.Context ; 12 import javax.naming.InitialContext ; 13 import javax.rmi.PortableRemoteObject ; 14 import javax.sql.DataSource ; 15 import java.io.Serializable ; 16 import javax.transaction.UserTransaction ; 17 import java.util.Enumeration ; 18 import java.util.Iterator ; 19 import java.net.URLEncoder ; 20 21 27 28 public class SB_SearchItemsByRegionBean implements SessionBean 29 { 30 protected SessionContext sessionContext; 31 protected Context initialContext = null; 32 protected DataSource dataSource = null; 33 private UserTransaction utx = null; 34 35 36 42 public String getItems(Integer categoryId, Integer regionId, int page, int nbOfItems) throws RemoteException 43 { 44 Enumeration list; 45 ItemPK itemPK; 46 ItemHome iHome; 47 Item item; 48 Query query; 49 QueryHome qHome; 50 StringBuffer html = new StringBuffer (); 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 e) 58 { 59 throw new RemoteException ("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 e) 66 { 67 throw new RemoteException ("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 e) 85 { 86 try 87 { 88 utx.rollback(); 89 throw new RemoteException ("Cannot get items list: " +e); 90 } 91 catch (Exception se) 92 { 93 throw new RemoteException ("Transaction rollback failed: " + e); 94 } 95 } 96 return html.toString(); 97 } 98 99 100 107 public String printItem(Item item) throws RemoteException 108 { 109 try 110 { 111 return item.printItem(); 112 } 113 catch (RemoteException re) 114 { 115 throw new RemoteException ("Unable to print Item (exception: "+re+")<br>\n"); 116 } 117 } 118 119 120 121 122 124 127 public void ejbCreate() throws CreateException , RemoteException 128 { 129 } 130 131 132 public void ejbActivate() throws RemoteException {} 133 134 public void ejbPassivate() throws RemoteException {} 135 136 public void ejbRemove() throws RemoteException {} 137 138 139 148 public void setSessionContext(SessionContext sessionContext) throws RemoteException 149 { 150 this.sessionContext = sessionContext; 151 if (dataSource == null) 152 { 153 155 try 156 { 157 initialContext = new InitialContext (); 158 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 159 } 160 catch (Exception e) 161 { 162 throw new RemoteException ("Cannot get JNDI InitialContext"); 163 } 164 } 165 } 166 167 } 168 | Popular Tags |