View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.ibatis.jpetstore.service;
17  
18  import com.ibatis.dao.client.DaoManager;
19  import com.ibatis.jpetstore.domain.LineItem;
20  import com.ibatis.jpetstore.domain.Order;
21  import com.ibatis.jpetstore.persistence.DaoConfig;
22  import com.ibatis.jpetstore.persistence.iface.ItemDao;
23  import com.ibatis.jpetstore.persistence.iface.OrderDao;
24  import com.ibatis.jpetstore.persistence.iface.SequenceDao;
25  import com.ibatis.common.util.PaginatedList;
26  
27  /***
28   * <p/>
29   * Date: Mar 6, 2004 11:22:36 PM
30   *
31   * @author Clinton Begin
32   */
33  public class OrderService {
34  
35    /* Constants */
36  
37    private static final OrderService instance = new OrderService();
38  
39    /* Private Fields */
40  
41    private DaoManager daoManager = DaoConfig.getDaomanager();
42  
43    private ItemDao itemDao;
44    private OrderDao orderDao;
45    private SequenceDao sequenceDao;
46  
47    /* Constructors */
48  
49    public OrderService() {
50      itemDao = (ItemDao) daoManager.getDao(ItemDao.class);
51      sequenceDao = (SequenceDao) daoManager.getDao(SequenceDao.class);
52      orderDao = (OrderDao) daoManager.getDao(OrderDao.class);
53    }
54  
55    /* Public Methods */
56  
57    public static OrderService getInstance() {
58      return instance;
59    }
60  
61    /* ORDER */
62  
63    public void insertOrder(Order order) {
64      try {
65        // Get the next id within a separate transaction
66        order.setOrderId(getNextId("ordernum"));
67  
68        daoManager.startTransaction();
69  
70        itemDao.updateQuantity(order);
71        orderDao.insertOrder(order);
72  
73        daoManager.commitTransaction();
74      } finally {
75        daoManager.endTransaction();
76      }
77    }
78  
79    public Order getOrder(int orderId) {
80      Order order = null;
81  
82      try {
83        daoManager.startTransaction();
84  
85        order = orderDao.getOrder(orderId);
86  
87        for (int i = 0; i < order.getLineItems().size(); i++) {
88          LineItem lineItem = (LineItem) order.getLineItems().get(i);
89          lineItem.setItem(itemDao.getItem(lineItem.getItemId()));
90        }
91  
92        daoManager.commitTransaction();
93      } finally {
94        daoManager.endTransaction();
95      }
96  
97      return order;
98    }
99  
100   public PaginatedList getOrdersByUsername(String username) {
101     return orderDao.getOrdersByUsername(username);
102   }
103 
104   /* SEQUENCE */
105 
106   public synchronized int getNextId(String key) {
107     return sequenceDao.getNextId(key);
108   }
109 
110 
111 }