1 package org.hibernate.ce.auction.command; 2 3 import org.hibernate.ce.auction.model.*; 4 import org.hibernate.ce.auction.dao.*; 5 import org.hibernate.ce.auction.exceptions.*; 6 import org.hibernate.ce.auction.persistence.HibernateUtil; 7 8 import java.math.BigDecimal ; 9 import java.util.Currency ; 10 11 19 public class BidForItemCommand implements Command { 20 21 private Long userId; 22 private Long itemId; 23 private BigDecimal bidAmount; 24 25 private Bid newBid; 26 27 public BidForItemCommand(Long userId, 28 Long itemId, 29 BigDecimal bidAmount) { 30 this.userId = userId; 31 this.itemId = itemId; 32 this.bidAmount = bidAmount; 33 } 34 35 public Bid getNewBid() { 36 return newBid; 37 } 38 39 public void execute() throws CommandException { 40 41 try { 42 ItemDAO itemDAO = new ItemDAO(); 43 UserDAO userDAO = new UserDAO(); 44 45 MonetaryAmount newAmount = 46 new MonetaryAmount(bidAmount, (Currency )Currency.getInstance("usd")); 47 Bid currentMaxBid = itemDAO.getMaxBid(itemId); 48 Bid currentMinBid = itemDAO.getMinBid(itemId); 49 50 Item item = itemDAO.getItemById(itemId, true); 51 newBid = item.placeBid(userDAO.getUserById(userId, false), 52 newAmount, 53 currentMaxBid, 54 currentMinBid); 55 56 HibernateUtil.commitTransaction(); 57 58 } catch (InfrastructureException ex) { 59 HibernateUtil.rollbackTransaction(); 61 throw new CommandException(ex); 62 63 } catch (BusinessException ex) { 64 throw new CommandException(ex); 66 67 } finally { 68 HibernateUtil.closeSession(); 69 } 70 } 71 72 } 73 | Popular Tags |