1 package com.ibatis.jpetstore.service; 2 3 import com.ibatis.dao.client.DaoManager; 4 import com.ibatis.jpetstore.domain.LineItem; 5 import com.ibatis.jpetstore.domain.Order; 6 import com.ibatis.jpetstore.persistence.DaoConfig; 7 import com.ibatis.jpetstore.persistence.iface.ItemDao; 8 import com.ibatis.jpetstore.persistence.iface.OrderDao; 9 import com.ibatis.jpetstore.persistence.iface.SequenceDao; 10 import com.ibatis.common.util.PaginatedList; 11 12 import java.util.List ; 13 14 20 public class OrderService { 21 22 23 24 private static final OrderService instance = new OrderService(); 25 26 27 28 private DaoManager daoManager = DaoConfig.getDaomanager(); 29 30 private ItemDao itemDao; 31 private OrderDao orderDao; 32 private SequenceDao sequenceDao; 33 34 35 36 public OrderService() { 37 itemDao = (ItemDao) daoManager.getDao(ItemDao.class); 38 sequenceDao = (SequenceDao) daoManager.getDao(SequenceDao.class); 39 orderDao = (OrderDao) daoManager.getDao(OrderDao.class); 40 } 41 42 43 44 public static OrderService getInstance() { 45 return instance; 46 } 47 48 49 50 public void insertOrder(Order order) { 51 try { 52 order.setOrderId(getNextId("ordernum")); 54 55 daoManager.startTransaction(); 56 57 itemDao.updateQuantity(order); 58 orderDao.insertOrder(order); 59 60 daoManager.commitTransaction(); 61 } finally { 62 daoManager.endTransaction(); 63 } 64 } 65 66 public Order getOrder(int orderId) { 67 Order order = null; 68 69 try { 70 daoManager.startTransaction(); 71 72 order = orderDao.getOrder(orderId); 73 74 for (int i = 0; i < order.getLineItems().size(); i++) { 75 LineItem lineItem = (LineItem) order.getLineItems().get(i); 76 lineItem.setItem(itemDao.getItem(lineItem.getItemId())); 77 } 78 79 daoManager.commitTransaction(); 80 } finally { 81 daoManager.endTransaction(); 82 } 83 84 return order; 85 } 86 87 public PaginatedList getOrdersByUsername(String username) { 88 return orderDao.getOrdersByUsername(username); 89 } 90 91 92 93 public synchronized int getNextId(String key) { 94 return sequenceDao.getNextId(key); 95 } 96 97 98 } 99 | Popular Tags |