View Javadoc
1   package ejava.examples.asyncmarket.ejb;
2   
3   import java.util.List;
4   
5   import javax.annotation.PostConstruct;
6   import javax.ejb.Stateless;
7   import javax.ejb.TransactionAttribute;
8   import javax.ejb.TransactionAttributeType;
9   import javax.inject.Inject;
10  import javax.persistence.EntityManager;
11  
12  import org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  
15  import ejava.examples.asyncmarket.bo.AuctionItem;
16  import ejava.examples.asyncmarket.bo.Bid;
17  import ejava.examples.asyncmarket.bo.Order;
18  import ejava.examples.asyncmarket.bo.Person;
19  import ejava.examples.asyncmarket.dao.AuctionItemDAO;
20  import ejava.examples.asyncmarket.dao.OrderDAO;
21  import ejava.examples.asyncmarket.dao.PersonDAO;
22  
23  @Stateless
24  @TransactionAttribute(TransactionAttributeType.REQUIRED)
25  public class BuyerEJB implements BuyerRemote, BuyerLocal {
26      private static final Logger logger = LoggerFactory.getLogger(BuyerEJB.class);
27  
28      @Inject
29      EntityManager em;
30      @Inject
31      private AuctionItemDAO auctionItemDAO;
32      @Inject
33      private PersonDAO userDAO;
34      @Inject
35      private OrderDAO orderDAO;
36      @Inject
37      private DtoMapper dtoMapper;
38      
39      @PostConstruct
40      void init() {
41          logger.info("*** BuyerEJB init() ***");
42      }
43  
44      @TransactionAttribute(TransactionAttributeType.REQUIRED)
45      public long bidProduct(long itemId, String userId, double amount) 
46              throws ResourceNotFoundException, InvalidRequestException {
47          logger.debug("bidProduct(itemId={}, userId={}, amount={}", itemId, userId, amount);
48          
49          AuctionItem item = auctionItemDAO.getItem(itemId);
50          if (item==null) {
51              throw new ResourceNotFoundException("itemId[%d] not found", itemId);            
52          }
53          logger.debug("found item for bid: {}", item);
54          
55          Person bidder = userDAO.getPersonByUserId(userId);
56          if (bidder==null) {
57              throw new ResourceNotFoundException("bidderId[%d] not found", userId);                        
58          }
59          logger.info("found bidder for bid: {}", bidder);
60          
61          
62          try {
63              Bid bid = new Bid();
64              bid.setAmount(amount);
65              bid.setItem(item);
66              item.addBid(bid); //can fail if too low
67              bid.setBidder(bidder);
68              bidder.getBids().add(bid);
69  
70              auctionItemDAO.createItem(item);
71              logger.debug("added bid: {}", bid);
72              return bid.getId();            
73          } catch (IllegalArgumentException ex) {
74              throw new InvalidRequestException("invalid bid:%s", ex);
75          } catch (Exception ex) {
76              logger.error("error bidding product", ex);
77              throw new InternalErrorException("error bidding product:%s", ex);
78          }
79      }
80  
81      public List<AuctionItem> getAvailableItems(int index, int count) {
82          try {
83              return dtoMapper.toDTO(
84                  auctionItemDAO.getAvailableItems(index, count));
85          }
86          catch (Exception ex) {
87              logger.error("error getting available items", ex);
88              throw new InternalErrorException("error getting available items: %s", ex);
89          }
90      }
91  
92      public AuctionItem getItem(long itemId) throws ResourceNotFoundException {
93          AuctionItem item = auctionItemDAO.getItem(itemId);
94          if (item==null) {
95              throw new ResourceNotFoundException("itemId[%d] not found", itemId);
96          }
97          
98          try {
99              return dtoMapper.toDTO(item);    
100         }
101         catch (Exception ex) {
102             logger.error("error getting item", ex);
103             throw new InternalErrorException("error getting item: %s", ex);
104         }
105     }
106 
107     public Order getOrder(long orderId) throws ResourceNotFoundException {
108         logger.debug("getOrder(id={})", orderId);
109         Order daoOrder = orderDAO.getOrder(orderId);
110         if (daoOrder==null) {
111             throw new ResourceNotFoundException("orderId[%d] not found", orderId);
112         }
113         logger.debug("daoOrder={}", daoOrder);
114 
115         try {
116             Order dtoOrder = dtoMapper.toDTO(daoOrder);
117             logger.debug("dtoOrder={}", dtoOrder);
118             return dtoOrder;
119         }
120         catch (Exception ex) {
121             logger.error("error getting item", ex);
122             throw new InternalErrorException("error getting order[%d]:%s", orderId, ex);
123         }
124     }
125 
126     @TransactionAttribute(TransactionAttributeType.REQUIRED)
127     public long placeOrder(long productId, String userId, double maxAmount) 
128         throws ResourceNotFoundException, InvalidRequestException {
129         AuctionItem item = auctionItemDAO.getItem(productId);
130         if (item==null) {
131             throw new ResourceNotFoundException("itemId[%d] not found", productId);
132         }
133         
134         Person buyer = userDAO.getPersonByUserId(userId);
135         if (buyer==null) {
136             throw new ResourceNotFoundException("userId[%d] not found", userId);
137         }
138         
139         try {
140             Order order = new Order();
141             order.setItem(item);
142             order.setBuyer(buyer);
143             order.setMaxBid(maxAmount);
144             orderDAO.createOrder(order);
145             return order.getId();
146         }
147         catch (Exception ex) {
148             logger.error("error placing order", ex);
149             throw new InternalErrorException("error placing order: %s", ex);
150         }
151     }
152 }