View Javadoc

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