/**
 * 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.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import olstore.dto.ItemValue;
import olstore.dto.PictureValue;
import olstore.entity.ItemLocal;
import olstore.entity.ItemLocalHome;
import olstore.session.helper.ItemHelper;

/**
 * A wrapper class for item 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 ItemManager {

    // An item local home to use the finder methods.
    private ItemLocalHome home;

    // A helper class for item bean operations.
    private ItemHelper helper;

    /**
     * Constructs a new manager with the appropriate beans supplied via Spring.
     * @param home the ItemLocalHome that provides finder methods.
     * @param helper the ItemHelper which provides all other item methods.
     */
    public ItemManager(ItemLocalHome home, ItemHelper helper) {
        this.home = home;
        this.helper = helper;
    }

    /**
     * Returns an ItemValue from the helper bean that matches the ID.
     * @param itemId the id of the item to return.
     * @return an ItemValue for the item requested.
     * @throws Exception
     */
    public ItemValue getItemById(String itemId) throws Exception {
        return helper.getItemValueForId(itemId);
    }

    /**
     * Returns a PictureValue for the picture that belongs to the item
     * given by the itemId.
     * @param itemId the item whose picture we want to return.
     * @return the picture associated with the item given by itemId.
     * @throws Exception
     */
    public PictureValue getPictureById(String itemId) throws Exception {
        return helper.getPicture(itemId);
    }

    /**
     * Returns a collection of DTOs that have the same content as the collection of
     * given locals.
     * @param locals a collection of locals that need to be converted to DTOs.
     * @return a Collection of DTOs that have the same content as the collection of locals given.
     * @throws Exception
     */
    private Collection convertToDTOs(Collection locals) throws Exception {
        Iterator i = locals.iterator();
        Collection dtos = new ArrayList();

        while (i.hasNext()) {
            dtos.add(helper.convertToDTO((ItemLocal) i.next()));
        }

        return dtos;
    }

    /**
     * Returns a collection of all items in the database.
     * @return a collectio of all items in the database.
     */
    public Collection getAllItems() {
        try {
            return convertToDTOs(home.findAll());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Returns a collection of all items whose type matches the supplied type.
     * @param type the type of items that will be returned.
     * @return a collection of all items whose type matches the supplied type.
     */
    public Collection getItemsByType(String type) {
        try {
            return convertToDTOs(home.findByType(type));
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Return a collection of all items that have been marked for the main page and
     * whose friends of username have purchased.
     * @param username the user whose friends are considered into the collection.
     * @return a collection of all items that have been marked for the main page and
     * whose friends of username have purchased.
     */
    public Collection findByMainPageAndFriendsOf(String username) {
        try {
            return home.findByMainPageAndFriendsOf(username);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Returns a collection of items that have been marked for the main page and
     * have been marked as a special offer.
     * @return a collectino of items that have been marked for the main page and
     * have been marked as a special offer.
     */
    public Collection findByMainPageAndSpecialOffer() {
        try {
            return home.findByMainPageAndSpecialOffer();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Returns a collection of all items that have been marked
     * for the main page.
     * @return a collection of all items that have been marked
     * for the main page.
    */
    public Collection findByMainPage() {
        try {
            return home.findByMainPage();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Saves the given item into the backend storage.
     * @param item the item to save.
     * @throws Exception
     */
    public void saveItem(ItemValue item) throws Exception {
        helper.saveItem(item);
    }

    /**
     * A collection of all properties for the given type.
     * @param type the type for which properties will be returned.
     * @return a collection of all properties for the given type.
     * @throws Exception
     */
    public Collection getPropertiesForType(String type) throws Exception {
        return helper.getPropertiesForType(type);
    }
    
    /**
     * Record this item as interesting to the user.
     * 
     * @param itemId the ID of the item the user wants to mark as interesting
     * @param user the username of the currently logged in user
     * @throws Exception if there is an error making the change
     */
    public void markItemAsInteresting(Integer itemId, String user) throws Exception {
        helper.markItemAsInteresting(itemId.toString(), user);
    }

}
