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.Enumeration ; 19 import java.util.Iterator ; 20 import java.net.URLEncoder ; 21 22 28 29 public class SB_AboutMeBean implements SessionBean 30 { 31 protected SessionContext sessionContext; 32 protected Context initialContext = null; 33 protected DataSource dataSource = null; 34 36 37 43 public String getAboutMe(String username, String password) throws RemoteException 44 { 45 int uid = -1; 46 Integer userId; 47 StringBuffer html = new StringBuffer (); 48 49 SB_AuthHome authHome = null; 51 SB_Auth auth = null; 52 try 53 { 54 authHome = (SB_AuthHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/SB_Auth"), SB_AuthHome.class); 55 auth = authHome.create(); 56 } 57 catch (Exception e) 58 { 59 throw new RemoteException ("Cannot lookup SB_Auth: " +e); 60 } 61 try 62 { 63 uid = auth.authenticate(username, password); 64 } 65 catch (Exception e) 66 { 67 throw new RemoteException ("Authentication failed: " +e); 68 } 69 if (uid == -1) 70 { 71 return "You don't have an account on RUBiS!<br>You have to register first.<br>"; 72 } 73 UserHome uHome; 75 try 76 { 77 userId = new Integer (uid); 78 uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/User"), UserHome.class); 79 } 80 catch (Exception e) 81 { 82 throw new RemoteException ("Cannot lookup User: " +e+"<br>"); 83 } 84 try 85 { 86 User u = uHome.findByPrimaryKey(new UserPK(userId)); 87 88 html.append(u.getHTMLGeneralUserInformation()); 89 } 90 catch (Exception e) 91 { 92 throw new RemoteException ("This user does not exist (got exception: " +e+")<br>"); 93 } 94 95 CommentHome cHome; 97 try 98 { 99 cHome = (CommentHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Comment"), CommentHome.class); 100 } 101 catch (Exception e) 102 { 103 throw new RemoteException ("Cannot lookup Comment: " +e+"<br>"); 104 } 105 ItemHome iHome; 107 try 108 { 109 iHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Item"), ItemHome.class); 110 } 111 catch (Exception e) 112 { 113 throw new RemoteException ("Cannot lookup item: " +e+"<br>"); 114 } 115 QueryHome qHome; 117 try 118 { 119 qHome = (QueryHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Query"), QueryHome.class); 120 } 121 catch (Exception e) 122 { 123 throw new RemoteException ("Cannot lookup Query: " +e+"<br>"); 124 } 125 try 126 { 127 html.append(listItem(userId, iHome)); 128 html.append(listBoughtItems(userId, iHome)); 129 html.append(listWonItems(userId, iHome, qHome)); 130 html.append(listBids(userId, username, password, iHome, qHome)); 131 html.append(listComments(cHome, userId, uHome)); 132 133 } 134 catch (Exception e) 135 { 136 throw new RemoteException ("Cannot lookup Query: " +e+"<br>"); 137 } 138 return html.toString(); 139 } 140 141 142 public String listItem(Integer userId, ItemHome iHome) throws RemoteException 143 { 144 Item item; 145 StringBuffer sell = new StringBuffer (); 146 Collection currentItemList, pastItemList; 147 148 try 149 { 150 currentItemList = iHome.findUserCurrentSellings(userId); 151 pastItemList = iHome.findUserPastSellings(userId); 152 } 153 catch (Exception e) 154 { 155 throw new RemoteException ("Exception getting item list: " +e+"<br>"); 156 } 157 158 if ((currentItemList == null) || (currentItemList.isEmpty())) 159 { 160 sell.append("<br>"); 161 sell.append(printHTMLHighlighted("<h3>You are currently selling no item.</h3>")); 162 } 163 else 164 { 165 try 167 { 168 sell.append(printSellHeader("Items you are currently selling.")); 169 170 Iterator it = currentItemList.iterator(); 171 while (it.hasNext()) 172 { 173 item = (Item)it.next(); 175 sell.append(item.printSell()); 177 } 178 sell.append(printItemFooter()); 179 } 180 catch (Exception e) 181 { 182 throw new RemoteException ("Exception getting item: " + e +"<br>"); 183 } 184 } 185 186 if ((pastItemList == null) || (pastItemList.isEmpty())) 187 { 188 sell.append("<br><h3>You didn't sell any item.</h3>"); 189 return sell.toString(); 190 } 191 sell.append("<br>"); 193 sell.append(printSellHeader("Items you sold in the last 30 days.")); 194 try 195 { 196 Iterator it = pastItemList.iterator(); 197 while (it.hasNext()) 198 { 199 item = (Item)it.next(); 201 sell.append(item.printSell()); 203 } 204 sell.append(printItemFooter()); 205 } 206 catch (Exception e) 207 { 208 throw new RemoteException ("Exception getting item: " + e +"<br>"); 209 } 210 211 return sell.toString(); 212 } 213 214 215 public String listBoughtItems(Integer userId, ItemHome iHome) throws RemoteException 216 { 217 BuyNowHome buyHome; 218 BuyNow buy; 219 Item item; 220 Collection buyList=null; 221 int quantity; 222 StringBuffer html = new StringBuffer (); 223 224 try 226 { 227 buyHome = (BuyNowHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/BuyNow"), 228 BuyNowHome.class); 229 } 230 catch (Exception e) 231 { 232 throw new RemoteException ("Cannot lookup BuyNow: " +e+"<br>"); 233 } 234 try 235 { 236 buyList = buyHome.findUserBuyNow(userId); 237 } 238 catch (Exception e) 239 { 240 throw new RemoteException ("Exception getting item list (buy now): " +e+"<br>"); 241 } 242 if ((buyList == null) || (buyList.isEmpty())) 243 { 244 return "<br><h3>You didn't buy any item in the last 30 days.</h3><br>"; 245 } 246 html.append(printUserBoughtItemHeader()); 247 248 Iterator it = buyList.iterator(); 249 while (it.hasNext()) 250 { 251 try 253 { 254 buy = (BuyNow)it.next(); 255 quantity = buy.getQuantity(); 256 } 257 catch (Exception e) 258 { 259 throw new RemoteException ("Exception getting buyNow quantity: " + e +"<br>"); 260 } 261 try 262 { 263 item = iHome.findByPrimaryKey(new ItemPK(buy.getItemId())); 264 html.append(item.printUserBoughtItem(quantity)); 266 } 267 catch (Exception e) 268 { 269 throw new RemoteException ("Exception getting item: " + e +"<br>"); 270 } 271 } 272 html.append(printItemFooter()); 273 return html.toString(); 274 275 } 276 277 278 public String listWonItems(Integer userId, ItemHome iHome, QueryHome qHome) throws RemoteException 279 { 280 Enumeration wonList=null; 281 Query q; 282 Item item; 283 float price; 284 String name; 285 StringBuffer html; 286 287 try 289 { 290 q = qHome.create(); 291 wonList = q.getUserWonItems(userId).elements(); 292 } 293 catch (Exception e) 294 { 295 throw new RemoteException ("Exception getting won items list: " +e+"<br>"); 296 } 297 298 if ((wonList == null) || (!wonList.hasMoreElements())) 299 { 300 return "<br><h3>You didn't win any item in the last 30 days.</h3><br>"; 301 } 302 html = new StringBuffer (printUserWonItemHeader()); 303 304 while (wonList.hasMoreElements()) 305 { 306 try 308 { 309 item = iHome.findByPrimaryKey((ItemPK)wonList.nextElement()); 310 html.append(item.printUserWonItem()); 312 } 313 catch (Exception e) 314 { 315 throw new RemoteException ("Exception getting item: " + e +"<br>"); 316 } 317 } 318 html.append(printItemFooter()); 319 return html.toString(); 320 } 321 322 323 324 public String listComments(CommentHome home, Integer userId, UserHome uHome) throws RemoteException 325 { 326 Collection list; 327 Comment comment; 328 StringBuffer html; 329 try 330 { 331 list = home.findByToUser(userId); 332 html = new StringBuffer ("<br>"); 333 if (list.isEmpty()) 334 html.append(printHTMLHighlighted("<h3>There is no comment yet for this user.</h3>")); 335 else 336 html.append(printHTMLHighlighted("<h3>Comments for this user</h3>")); 337 html.append("<br>"); 338 html.append(printCommentHeader()); 339 Iterator it = list.iterator(); 341 while (it.hasNext()) 342 { 343 comment = (Comment)it.next(); 344 String userName; 345 try 346 { 347 User u = uHome.findByPrimaryKey(new UserPK(comment.getFromUserId())); 348 userName = u.getNickName(); 349 html.append(printComment(userName, comment)); 350 } 351 catch (Exception e) 352 { 353 throw new RemoteException ("This author does not exist (got exception: " +e+")<br>"); 354 } 355 } 356 html.append(printCommentFooter()); 357 } 358 catch (Exception e) 359 { 360 throw new RemoteException ("Exception getting comment list: " + e +"<br>"); 361 } 362 return html.toString(); 363 } 364 365 366 public String listBids(Integer userId, String username, String password, ItemHome iHome, QueryHome qHome) throws RemoteException 367 { 368 Enumeration bidList=null; 369 Query q; 370 BidHome bidHome; 371 Bid bid; 372 Item item; 373 float price; 374 String name; 375 StringBuffer html; 376 377 try 379 { 380 q = qHome.create(); 381 bidList = q.getUserBids(userId).elements(); 382 } 383 catch (Exception e) 384 { 385 throw new RemoteException ("Exception getting bids list: " +e+"<br>"); 386 } 387 if ((bidList == null) || (!bidList.hasMoreElements())) 388 { 389 return printHTMLHighlighted("<h3>You didn't put any bid.</h3>"); 390 } 391 392 try 394 { 395 bidHome = (BidHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Bid"), 396 BidHome.class); 397 } 398 catch (Exception e) 399 { 400 throw new RemoteException ("Cannot lookup Bid: " +e+"<br>"); 401 } 402 403 html = new StringBuffer (printUserBidsHeader()); 404 405 while (bidList.hasMoreElements()) 406 { 407 try 409 { 410 bid = bidHome.findByPrimaryKey((BidPK)bidList.nextElement()); 411 } 412 catch (Exception e) 413 { 414 throw new RemoteException ("Exception getting bid: " + e +"<br>"); 415 } 416 417 try 419 { 420 item = iHome.findByPrimaryKey(new ItemPK(bid.getItemId())); 421 html.append(printItemUserHasBidOn(bid, item, username, password)); 422 } 423 catch (Exception e) 424 { 425 throw new RemoteException ("Exception getting item: " + e +"<br>"); 426 } 427 } 429 html.append(printItemFooter()); 430 return html.toString(); 431 } 432 433 439 public String printUserBoughtItemHeader() 440 { 441 return "<br>"+ 442 printHTMLHighlighted("<p><h3>Items you bouhgt in the past 30 days.</h3>\n")+ 443 "<TABLE border=\"1\" summary=\"List of items\">\n"+ 444 "<THEAD>\n"+ 445 "<TR><TH>Designation<TH>Quantity<TH>Price you bought it<TH>Seller"+ 446 "<TBODY>\n"; 447 } 448 449 455 public String printUserWonItemHeader() 456 { 457 return "<br>"+ 458 printHTMLHighlighted("<p><h3>Items you won in the past 30 days.</h3>\n")+ 459 "<TABLE border=\"1\" summary=\"List of items\">\n"+ 460 "<THEAD>\n"+ 461 "<TR><TH>Designation<TH>Price you bought it<TH>Seller"+ 462 "<TBODY>\n"; 463 } 464 465 471 public String printUserBidsHeader() 472 { 473 return "<br>"+ 474 printHTMLHighlighted("<p><h3>Items you have bid on.</h3>\n")+ 475 "<TABLE border=\"1\" summary=\"Items You've bid on\">\n"+ 476 "<THEAD>\n"+ 477 "<TR><TH>Designation<TH>Initial Price<TH>Current price<TH>Your max bid<TH>Quantity"+ 478 "<TH>Start Date<TH>End Date<TH>Seller<TH>Put a new bid\n"+ 479 "<TBODY>\n"; 480 } 481 482 483 489 public String printItemUserHasBidOn(Bid bid, Item item, String username, String password) throws RemoteException 490 { 491 try 492 { 493 return item.printItemUserHasBidOn(bid.getMaxBid())+"&nickname="+URLEncoder.encode(username)+"&password="+URLEncoder.encode(password)+"\"><IMG SRC=\"/EJB_HTML/bid_now.jpg\" height=22 width=90></a>\n"; 494 } 495 catch (RemoteException re) 496 { 497 throw new RemoteException ("Unable to print Item (exception: "+re+")<br>\n"); 498 } 499 } 500 501 502 508 public String printSellHeader(String title) 509 { 510 return printHTMLHighlighted("<p><h3>"+title+"</h3>\n")+ 511 "<TABLE border=\"1\" summary=\"List of items\">\n"+ 512 "<THEAD>\n"+ 513 "<TR><TH>Designation<TH>Initial Price<TH>Current price<TH>Quantity<TH>ReservePrice<TH>Buy Now"+ 514 "<TH>Start Date<TH>End Date\n"+ 515 "<TBODY>\n"; 516 } 517 518 519 525 public String printItemFooter() 526 { 527 return "</TABLE>\n"; 528 } 529 530 536 public String printCommentHeader() 537 { 538 return "<DL>\n"; 539 } 540 541 549 public String printComment(String userName, Comment comment) throws RemoteException 550 { 551 try 552 { 553 return comment.printComment(userName); 554 } 555 catch (RemoteException re) 556 { 557 throw new RemoteException ("Unable to print Comment (exception: "+re+")<br>\n"); 558 } 559 } 560 561 567 public String printCommentFooter() 568 { 569 return "</DL>\n"; 570 } 571 572 578 public String printHTMLHighlighted(String msg) 579 { 580 return "<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>" + msg + "</B></FONT></TD></TR>\n</TABLE><p>\n"; 581 } 582 583 584 586 589 public void ejbCreate() throws CreateException , RemoteException 590 { 591 } 592 593 594 public void ejbActivate() throws RemoteException {} 595 596 public void ejbPassivate() throws RemoteException {} 597 598 public void ejbRemove() throws RemoteException {} 599 600 601 610 public void setSessionContext(SessionContext sessionContext) throws RemoteException 611 { 612 this.sessionContext = sessionContext; 613 if (dataSource == null) 614 { 615 617 try 618 { 619 initialContext = new InitialContext (); 620 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 621 } 622 catch (Exception e) 623 { 624 throw new RemoteException ("Cannot get JNDI InitialContext"); 625 } 626 } 627 } 628 629 } 630 | Popular Tags |