KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > command > BidForItemCommand


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 JavaDoc;
9 import java.util.Currency JavaDoc;
10
11 /**
12  * An example of the EJB command pattern.
13  * <p>
14  * Some parameters are passed in, the control logic is executed, the
15  * result comes back.
16  *
17  * @author Christian Bauer <christian@hibernate.org>
18  */

19 public class BidForItemCommand implements Command {
20
21     private Long JavaDoc userId;
22     private Long JavaDoc itemId;
23     private BigDecimal JavaDoc bidAmount;
24
25     private Bid newBid;
26
27     public BidForItemCommand(Long JavaDoc userId,
28                              Long JavaDoc itemId,
29                              BigDecimal JavaDoc 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 JavaDoc)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             // Rethrow as a checked exception
60
HibernateUtil.rollbackTransaction();
61             throw new CommandException(ex);
62
63         } catch (BusinessException ex) {
64             // Rethrow as a checked exception
65
throw new CommandException(ex);
66
67         } finally {
68             HibernateUtil.closeSession();
69         }
70     }
71
72 }
73
Popular Tags