1 package edu.rice.rubis.beans; 2 3 import java.rmi.*; 4 import javax.ejb.*; 5 import javax.naming.Context ; 6 import javax.naming.InitialContext ; 7 import javax.rmi.PortableRemoteObject ; 8 import java.util.GregorianCalendar ; 9 10 37 38 public class ItemBean implements EntityBean 39 { 40 private EntityContext entityContext; 41 private transient boolean isDirty; 43 44 45 public Integer id; 46 public String name; 47 public String description; 48 public float initialPrice; 49 public int quantity; 50 public float reservePrice; 51 public float buyNow; 52 public int nbOfBids; 53 public float maxBid; 54 public String startDate; 55 public String endDate; 56 public Integer sellerId; 57 public Integer categoryId; 58 59 60 67 public Integer getId() throws RemoteException 68 { 69 return id; 70 } 71 72 79 public String getName() throws RemoteException 80 { 81 return name; 82 } 83 84 91 public String getDescription() throws RemoteException 92 { 93 return description; 94 } 95 96 103 public float getInitialPrice() throws RemoteException 104 { 105 return initialPrice; 106 } 107 108 115 public int getQuantity() throws RemoteException 116 { 117 return quantity; 118 } 119 120 127 public float getReservePrice() throws RemoteException 128 { 129 return reservePrice; 130 } 131 132 139 public float getBuyNow() throws RemoteException 140 { 141 return buyNow; 142 } 143 144 151 public float getMaxBid() throws RemoteException 152 { 153 return maxBid; 154 } 155 156 163 public int getNbOfBids() throws RemoteException 164 { 165 return nbOfBids; 166 } 167 168 175 public String getStartDate() throws RemoteException 176 { 177 return startDate; 178 } 179 180 187 public String getEndDate() throws RemoteException 188 { 189 return endDate; 190 } 191 192 199 public Integer getSellerId() throws RemoteException 200 { 201 return sellerId; 202 } 203 204 205 212 public Integer getCategoryId() throws RemoteException 213 { 214 return categoryId; 215 } 216 217 218 226 public String getSellerNickname() throws RemoteException 227 { 228 Context initialContext = null; 229 try 230 { 231 initialContext = new InitialContext (); 232 } 233 catch (Exception e) 234 { 235 System.err.print("Cannot get initial context for JNDI: " + e); 236 return null; 237 } 238 239 UserHome uHome; 241 try 242 { 243 uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("UserHome"), 244 UserHome.class); 245 } 246 catch (Exception e) 247 { 248 System.err.print("Cannot lookup User: " +e); 249 return null; 250 } 251 try 252 { 253 User u = uHome.findByPrimaryKey(new UserPK(sellerId)); 254 return u.getNickName(); 255 } 256 catch (Exception e) 257 { 258 System.err.print("This user does not exist (got exception: " +e+")<br>"); 259 return null; 260 } 261 } 262 263 264 271 public String getCategoryName() throws RemoteException 272 { 273 Context initialContext = null; 274 try 275 { 276 initialContext = new InitialContext (); 277 } 278 catch (Exception e) 279 { 280 System.err.print("Cannot get initial context for JNDI: " + e); 281 return null; 282 } 283 284 CategoryHome cHome; 286 try 287 { 288 cHome = (CategoryHome)PortableRemoteObject.narrow(initialContext.lookup("CategoryHome"), 289 CategoryHome.class); 290 } 291 catch (Exception e) 292 { 293 System.err.print("Cannot lookup Category: " +e); 294 return null; 295 } 296 try 297 { 298 Category c = cHome.findByPrimaryKey(new CategoryPK(id)); 299 return c.getName(); 300 } 301 catch (Exception e) 302 { 303 System.err.print("This category does not exist (got exception: " +e+")<br>"); 304 return null; 305 } 306 } 307 308 309 316 public void setName(String newName) throws RemoteException 317 { 318 name = newName; 319 isDirty = true; } 321 322 329 public void setDescription(String newDescription) throws RemoteException 330 { 331 description = newDescription; 332 isDirty = true; } 334 335 342 public void setInitialPrice(float newInitialPrice) throws RemoteException 343 { 344 initialPrice = newInitialPrice; 345 isDirty = true; } 347 348 355 public void setQuantity(int qty) throws RemoteException 356 { 357 quantity = qty; 358 isDirty = true; } 360 361 368 public void setReservePrice(float newReservePrice) throws RemoteException 369 { 370 reservePrice = newReservePrice; 371 } 372 373 380 public void setBuyNow(float newBuyNow) throws RemoteException 381 { 382 buyNow = newBuyNow; 383 isDirty = true; } 385 386 394 public void setMaxBid(float newMaxBid) throws RemoteException 395 { 396 if (newMaxBid > maxBid) 397 maxBid = newMaxBid; 398 isDirty = true; } 400 401 408 public void setNbOfBids(int newNbOfBids) throws RemoteException 409 { 410 nbOfBids = newNbOfBids; 411 isDirty = true; } 413 414 420 public void addOneBid() throws RemoteException 421 { 422 nbOfBids++; 423 isDirty = true; } 425 426 433 public void setStartDate(String newDate) throws RemoteException 434 { 435 startDate = newDate; 436 isDirty = true; } 438 439 446 public void setEndDate(String newDate) throws RemoteException 447 { 448 endDate = newDate; 449 isDirty = true; } 451 452 460 public void setSellerId(Integer id) throws RemoteException 461 { 462 sellerId = id; 463 isDirty = true; } 465 466 474 public void setCategoryId(Integer id) throws RemoteException 475 { 476 categoryId = id; 477 isDirty = true; } 479 480 481 503 public ItemPK ejbCreate(String itemName, String itemDescription, float itemInitialPrice, 504 int itemQuantity, float itemReservePrice, float itemBuyNow, int duration, 505 Integer itemSellerId, Integer itemCategoryId) throws CreateException, RemoteException, RemoveException 506 { 507 GregorianCalendar start = new GregorianCalendar (); 508 IDManagerHome home = null; 510 IDManager idManager = null; 511 512 try 513 { 514 InitialContext initialContext = new InitialContext (); 515 home = (IDManagerHome)PortableRemoteObject.narrow(initialContext.lookup( 516 "java:comp/env/ejb/IDManager"), IDManagerHome.class); 517 } 518 catch (Exception e) 519 { 520 throw new EJBException("Cannot lookup IDManager: " +e); 521 } 522 try 523 { 524 IDManagerPK idPK = new IDManagerPK(); 525 idManager = home.findByPrimaryKey(idPK); 526 id = idManager.getNextItemID(); 527 name = itemName; 528 description = itemDescription; 529 initialPrice = itemInitialPrice; 530 quantity = itemQuantity; 531 reservePrice = itemReservePrice; 532 buyNow = itemBuyNow; 533 sellerId = itemSellerId; 534 categoryId = itemCategoryId; 535 nbOfBids = 0; 536 maxBid = 0; 537 startDate = TimeManagement.dateToString(start); 538 endDate = TimeManagement.dateToString(TimeManagement.addDays(start, duration)); 539 } 540 catch (Exception e) 541 { 542 throw new EJBException("Cannot create item: " +e); 543 } 544 return null; 545 } 546 547 549 public void ejbPostCreate(String itemName, String itemDescription, float itemInitialPrice, 550 int itemQuantity, float itemReservePrice, float itemBuyNow, int duration, 551 Integer itemSellerId, Integer itemCategoryId) 552 { 553 isDirty = true; } 555 556 558 public void ejbLoad() throws RemoteException 559 { 560 isDirty = false; 561 } 562 563 565 public void ejbStore() throws RemoteException 566 { 567 isDirty = false; 568 } 569 570 571 public void ejbActivate() throws RemoteException {} 572 573 public void ejbPassivate() throws RemoteException {} 574 575 public void ejbRemove() throws RemoteException, RemoveException {} 576 577 594 public void setEntityContext(EntityContext context) throws RemoteException 595 { 596 entityContext = context; 597 } 598 599 616 public void unsetEntityContext() throws RemoteException 617 { 618 entityContext = null; 619 } 620 621 622 629 public boolean isModified() 630 { 631 return isDirty; 632 } 633 634 635 642 public String printItem() throws RemoteException 643 { 644 return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewItem?itemId="+id+"\">"+name+ 645 "<TD>"+maxBid+ 646 "<TD>"+nbOfBids+ 647 "<TD>"+endDate+ 648 "<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutBidAuth?itemId="+id+"\"><IMG SRC=\"/EJB_HTML/bid_now.jpg\" height=22 width=90></a>\n"; 649 } 650 651 658 public String printUserBoughtItem(int qty) throws RemoteException 659 { 660 return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewItem?itemId="+id+"\">"+name+"</a>\n"+ 661 "<TD>"+qty+"\n"+"<TD>"+buyNow+"\n"+ 662 "<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+sellerId+"\">"+getSellerNickname()+"</a>\n"; 663 } 664 665 672 public String printItemUserHasBidOn(float bidMaxBid) throws RemoteException 673 { 674 return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewItem?itemId="+id+"\">"+name+ 675 "<TD>"+initialPrice+"<TD>"+maxBid+"<TD>"+bidMaxBid+"<TD>"+quantity+"<TD>"+startDate+"<TD>"+endDate+ 676 "<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+sellerId+"\">"+getSellerNickname()+ 677 "<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutBid?itemId="+id; 678 } 679 680 681 688 public String printSell() throws RemoteException 689 { 690 return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewItem?itemId="+id+"\">"+name+ 691 "<TD>"+initialPrice+"<TD>"+maxBid+"<TD>"+quantity+"<TD>"+reservePrice+"<TD>"+buyNow+"<TD>"+startDate+"<TD>"+endDate+"\n"; 692 } 693 694 701 public String printUserWonItem() throws RemoteException 702 { 703 return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewItem?itemId="+id+"\">"+name+"</a>\n"+ 704 "<TD>"+maxBid+"\n"+ 705 "<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+sellerId+"\">"+getSellerNickname()+"</a>\n"; 706 } 707 708 715 public String printItemDescriptionToBuyNow(int userId) throws RemoteException 716 { 717 String result = "<TABLE>\n"+"<TR><TD>Quantity<TD><b><BIG>"+quantity+"</BIG></b>\n"+ 718 "<TR><TD>Seller<TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+sellerId+"\">"+ 719 getSellerNickname()+"</a> (<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.PutCommentAuth?to="+sellerId+"&itemId="+id+"\">Leave a comment on this user</a>)\n"+ 720 "<TR><TD>Started<TD>"+startDate+"\n"+"<TR><TD>Ends<TD>"+endDate+"\n"+ 721 "</TABLE>"+ 722 "<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n"+ 723 "<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>Item description</B></FONT></TD></TR>\n"+ 724 "</TABLE><p>\n"+description+"<br><p>\n"+ 725 "<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n"+ 726 "<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>Buy Now</B></FONT></TD></TR>\n"+ 727 "</TABLE><p>\n"+ 728 "<form action=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.StoreBuyNow\" method=POST>\n"+ 729 "<input type=hidden name=userId value="+userId+">\n"+ 730 "<input type=hidden name=itemId value="+id+">\n"+ 731 "<input type=hidden name=maxQty value="+quantity+">\n"; 732 if (quantity > 1) 733 result = result + "<center><table><tr><td>Quantity:</td>\n"+ 734 "<td><input type=text size=5 name=qty></td></tr></table></center>\n"; 735 else 736 result = result + "<input type=hidden name=qty value=1>\n"; 737 result = result + "<p><input type=submit value=\"Buy now!\"></center><p>\n"; 738 return result; 739 } 740 } 741 | Popular Tags |