001: /**
002: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Greg Lapouchnian
022: * Patrick Smith
023: *
024: */package olstore.domain.manager;
025:
026: import java.math.BigDecimal;
027: import java.util.ArrayList;
028:
029: import javax.ejb.CreateException;
030:
031: import olstore.entity.UserLocal;
032: import olstore.session.ShoppingCartLocal;
033: import olstore.session.ShoppingCartLocalHome;
034:
035: /**
036: * A wrapper class for type actions so that there is no need to reference the
037: * beans directly, allowing us to swap out the backend beans for a different
038: * implementation.
039: */
040: public class CartManager {
041:
042: // Reference for cart EJB
043: private ShoppingCartLocal cart;
044:
045: /**
046: * Creates a new CartManger with information provided via Spring injection.
047: * @param cartHome Cart EJB home object for finder methods.
048: * @param userManager a wrapper class for user related actions.
049: * @param username the user name that this cart will belong to.
050: */
051: public CartManager(ShoppingCartLocalHome cartHome,
052: UserManager userManager, String username) {
053: UserLocal user = userManager.findByUsername(username);
054: try {
055: this .cart = cartHome.create(user);
056: } catch (CreateException e) {
057: // TODO FIX THIS
058: e.printStackTrace();
059: }
060: }
061:
062: /**
063: * Updates the item quantity for the given item with the given amount in this cart.
064: * @param itemId the id of the item to update.
065: * @param quantity the new quantity of the item.
066: */
067: public void updateQuantity(Integer itemId, Integer quantity) {
068: cart.updateQuantity(itemId, quantity);
069: }
070:
071: /**
072: * Checks if this shopping cart contains the given item.
073: * @param itemId the id of the item to check.
074: * @return true if this cart contains the given item.
075: */
076: public boolean containsItem(Integer itemId) {
077: return cart.contains(itemId);
078: }
079:
080: /**
081: * Returns a list of all item DTOs in this cart.
082: * @return a list of all item DTOs in this cart.
083: */
084: public ArrayList getItems() {
085: return cart.shoppingCartToDTOs();
086: }
087:
088: /**
089: * Returns the number of items currently in this cart.
090: * @return the number of items currently in the cart.
091: */
092: public int getNumberOfItems() {
093: return cart.listNumOfItems().intValue();
094: }
095:
096: /**
097: * Returns the total cost for all items and quantities in the cart.
098: * @return the total cost for all items and quantities in the cart.
099: */
100: public BigDecimal getTotalCost() {
101: return cart.getTotalCost();
102: }
103:
104: /**
105: * Checkouts this shopping cart and places it as an order.
106: */
107: public void placeOrder() {
108: cart.createOrders();
109: }
110: }
|