01: package com.ibatis.jpetstore.service;
02:
03: import com.ibatis.common.util.PaginatedList;
04: import com.ibatis.dao.client.DaoManager;
05: import com.ibatis.jpetstore.domain.LineItem;
06: import com.ibatis.jpetstore.domain.Order;
07: import com.ibatis.jpetstore.persistence.DaoConfig;
08: import com.ibatis.jpetstore.persistence.iface.ItemDao;
09: import com.ibatis.jpetstore.persistence.iface.OrderDao;
10: import com.ibatis.jpetstore.persistence.iface.SequenceDao;
11:
12: /**
13: * <p/>Date: Mar 6, 2004 11:22:36 PM
14: *
15: * @author Clinton Begin
16: */
17: public class OrderServiceImpl implements OrderService {
18:
19: /* Private Fields */
20:
21: private DaoManager daoManager = DaoConfig.getDaomanager();
22:
23: private ItemDao itemDao;
24:
25: private OrderDao orderDao;
26:
27: private SequenceDao sequenceDao;
28:
29: /* Constructors */
30:
31: public OrderServiceImpl() {
32: itemDao = (ItemDao) daoManager.getDao(ItemDao.class);
33: sequenceDao = (SequenceDao) daoManager
34: .getDao(SequenceDao.class);
35: orderDao = (OrderDao) daoManager.getDao(OrderDao.class);
36: }
37:
38: /* ORDER */
39:
40: public void insertOrder(Order order) {
41: try {
42: // Get the next id within a separate transaction
43: order.setOrderId(getNextId("ordernum"));
44:
45: daoManager.startTransaction();
46:
47: itemDao.updateQuantity(order);
48: orderDao.insertOrder(order);
49:
50: daoManager.commitTransaction();
51: } finally {
52: daoManager.endTransaction();
53: }
54: }
55:
56: public Order getOrder(int orderId) {
57: Order order = null;
58:
59: try {
60: daoManager.startTransaction();
61:
62: order = orderDao.getOrder(orderId);
63:
64: for (int i = 0; i < order.getLineItems().size(); i++) {
65: LineItem lineItem = (LineItem) order.getLineItems()
66: .get(i);
67: lineItem.setItem(itemDao.getItem(lineItem.getItemId()));
68: }
69:
70: daoManager.commitTransaction();
71: } finally {
72: daoManager.endTransaction();
73: }
74:
75: return order;
76: }
77:
78: public PaginatedList getOrdersByUsername(String username) {
79: return orderDao.getOrdersByUsername(username);
80: }
81:
82: /* SEQUENCE */
83:
84: public synchronized int getNextId(String key) {
85: return sequenceDao.getNextId(key);
86: }
87:
88: }
|