01: package com.odal.petstore.service;
02:
03: import com.completex.objective.components.persistency.transact.Transaction;
04: import com.completex.objective.components.persistency.transact.TransactionManager;
05: import com.odal.petstore.OdalPetstoreException;
06: import com.odal.petstore.domain.Order;
07: import com.odal.petstore.persistence.iface.ItemDao;
08: import com.odal.petstore.persistence.iface.OrderDao;
09:
10: import java.util.List;
11:
12: /**
13: * @author Gennady Krizhevsky
14: */
15: public class OrderService {
16:
17: private ItemDao itemDao;
18: private OrderDao orderDao;
19: private TransactionManager transactionManager;
20:
21: public OrderService(TransactionManager transactionManager,
22: ItemDao itemDao, OrderDao orderDao) {
23: this .itemDao = itemDao;
24: this .orderDao = orderDao;
25: this .transactionManager = transactionManager;
26: }
27:
28: public void insertOrder(final Order order)
29: throws OdalPetstoreException {
30: Transaction transaction = transactionManager.beginUnchecked();
31: try {
32: itemDao.updateAllQuantitiesFromOrder(order);
33: orderDao.insertOrder(order);
34: transaction.commitUnchecked();
35: } finally {
36: transactionManager.rollbackSilently(transaction);
37: }
38: }
39:
40: public Order getOrder(final long orderId)
41: throws OdalPetstoreException {
42: Transaction transaction = transactionManager.beginUnchecked();
43: try {
44: Order order = orderDao.getOrder(orderId);
45: order.getLineItems(); // Load line items by calling getter since they are lazily loaded.
46: return order;
47: } finally {
48: transactionManager.rollbackSilently(transaction);
49: }
50: }
51:
52: public List getOrdersByUsername(final String username,
53: final int pageNumber) throws OdalPetstoreException {
54: Transaction transaction = transactionManager.beginUnchecked();
55: try {
56: return orderDao.getOrdersByUsername(username, pageNumber);
57: } finally {
58: transactionManager.rollbackSilently(transaction);
59: }
60: }
61:
62: }
|