/**
 * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Component of: Red Hat Application Server
 * 
 * Initial Developers: Greg Lapouchnian
 *                     Patrick Smith
 *
 */

package olstore.domain.manager;

import java.math.BigDecimal;
import java.util.ArrayList;

import javax.ejb.CreateException;

import olstore.entity.UserLocal;
import olstore.session.ShoppingCartLocal;
import olstore.session.ShoppingCartLocalHome;

/**
 * A wrapper class for type actions so that there is no need to reference the
 * beans directly, allowing us to swap out the backend beans for a different
 * implementation.
 */
public class CartManager {

    // Reference for cart EJB
    private ShoppingCartLocal cart;

    /**
     * Creates a new CartManger with information provided via Spring injection.
     * @param cartHome Cart EJB home object for finder methods.
     * @param userManager a wrapper class for user related actions.
     * @param username the user name that this cart will belong to.
     */
    public CartManager(ShoppingCartLocalHome cartHome, UserManager userManager, String username) {
        UserLocal user = userManager.findByUsername(username);
        try {
            this.cart = cartHome.create(user);
        } catch (CreateException e) {
            // TODO FIX THIS
            e.printStackTrace();
        }        
    }

    /**
     * Updates the item quantity for the given item with the given amount in this cart.
     * @param itemId the id of the item to update.
     * @param quantity the new quantity of the item.
     */
    public void updateQuantity(Integer itemId, Integer quantity) {
        cart.updateQuantity(itemId, quantity);
    }

    /**
     * Checks if this shopping cart contains the given item.
     * @param itemId the id of the item to check.
     * @return true if this cart contains the given item.
     */
    public boolean containsItem(Integer itemId) {
        return cart.contains(itemId);
    }

    /**
     * Returns a list of all item DTOs in this cart.
     * @return a list of all item DTOs in this cart.
     */
    public ArrayList getItems() {
        return cart.shoppingCartToDTOs();
    }

    /**
     * Returns the number of items currently in this cart.
     * @return the number of items currently in the cart.
     */
    public int getNumberOfItems() {
        return cart.listNumOfItems().intValue();
    }

    /**
     * Returns the total cost for all items and quantities in the cart.
     * @return the total cost for all items and quantities in the cart.
     */
    public BigDecimal getTotalCost() {
        return cart.getTotalCost();
    }

    /**
     * Checkouts this shopping cart and places it as an order.
     */
    public void placeOrder() {
        cart.createOrders();
    }
}
