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.Collection ; 18 import java.util.Iterator ; 19 import java.net.URLEncoder ; 20 21 27 28 public class SB_BrowseCategoriesBean 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 43 public String getCategories(String regionName, String username, String password) throws RemoteException 44 { 45 Collection list; 46 Category cat; 47 CategoryHome home = null; 48 String html = ""; 49 int regionId = -1; 50 int userId = -1; 51 52 if (regionName != null && regionName !="") 53 { 54 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 e) 62 { 63 throw new RemoteException ("Cannot lookup Region: " +e); 64 } 65 try 67 { 68 Region region = regionHome.findByName(regionName); 69 regionId = region.getId().intValue(); 70 } 71 catch (Exception e) 72 { 73 throw new RemoteException (" Region "+regionName+" does not exist in the database!<br>(got exception: " +e+")"); 74 } 75 } 76 else 77 { 78 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 e) 89 { 90 throw new RemoteException ("Cannot lookup SB_Auth: " +e); 91 } 92 try 93 { 94 userId = auth.authenticate(username, password); 95 } 96 catch (Exception e) 97 { 98 throw new RemoteException ("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 try 110 { 111 home = (CategoryHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Category"), CategoryHome.class); 112 } 113 catch (Exception e) 114 { 115 throw new RemoteException ("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 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 e) 148 { 149 try 150 { 151 utx.rollback(); 152 throw new RemoteException ("Exception getting category list: " + e); 153 } 154 catch (Exception se) 155 { 156 throw new RemoteException ("Transaction rollback failed: " + e); 157 } 158 } 159 return html; 160 } 161 162 163 170 171 public String printCategory(Category category) throws RemoteException 172 { 173 String html = ""; 174 try 175 { 176 html = (category.printCategory()); 177 } 178 catch (RemoteException re) 179 { 180 throw new RemoteException ("Unable to print Category 1 (exception: "+re+")"); 181 } 182 return html; 183 } 184 185 190 public String printCategoryByRegion(Category category, int regionId) throws RemoteException 191 { 192 String html = ""; 193 try 194 { 195 html = (category.printCategoryByRegion(regionId)); 196 } 197 catch (RemoteException re) 198 { 199 throw new RemoteException ("Unable to print Category 2 (exception: "+re+")<br>\n"); 200 } 201 return html; 202 } 203 204 209 public String printCategoryToSellItem(Category category, int userId) throws RemoteException 210 { 211 String html= ""; 212 try 213 { 214 html = (category.printCategoryToSellItem(userId)); 215 } 216 catch (RemoteException re) 217 { 218 throw new RemoteException ("Unable to print Category 3 (exception: "+re+")<br>\n"); 219 } 220 return html; 221 } 222 223 224 225 227 230 public void ejbCreate() throws CreateException , RemoteException 231 { 232 } 233 234 235 public void ejbActivate() throws RemoteException {} 236 237 public void ejbPassivate() throws RemoteException {} 238 239 public void ejbRemove() throws RemoteException {} 240 241 242 251 public void setSessionContext(SessionContext sessionContext) throws RemoteException 252 { 253 this.sessionContext = sessionContext; 254 if (dataSource == null) 255 { 256 258 try 259 { 260 initialContext = new InitialContext (); 261 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 262 } 263 catch (Exception e) 264 { 265 throw new RemoteException ("Cannot get JNDI InitialContext"); 266 } 267 } 268 } 269 270 } 271 | Popular Tags |