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_SearchItemsByCategoryBean 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, int page, int nbOfItems) throws RemoteException 43 { 44 45 Enumeration list; 46 ItemPK itemPK; 47 ItemHome iHome; 48 Item item; 49 Query query; 50 QueryHome qHome; 51 StringBuffer html = new StringBuffer (); 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 e) 59 { 60 throw new RemoteException ("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 e) 67 { 68 throw new RemoteException ("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 e) 86 { 87 try 88 { 89 utx.rollback(); 90 throw new RemoteException ("Cannot get items list: " +e); 91 } 92 catch (Exception se) 93 { 94 throw new RemoteException ("Transaction rollback failed: " + e); 95 } 96 } 97 return html.toString(); 98 } 99 100 101 108 public String printItem(Item item) throws RemoteException 109 { 110 try 111 { 112 return item.printItem(); 113 } 114 catch (RemoteException re) 115 { 116 throw new RemoteException ("Unable to print Item (exception: "+re+")<br>\n"); 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 |