/**
 * 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 javax.ejb.EJBException;
import javax.ejb.FinderException;

import olstore.dto.AddressValue;
import olstore.dto.CreateUserValue;
import olstore.dto.OrderEntry;
import olstore.dto.UserValue;
import olstore.entity.OrderLocal;
import olstore.entity.UserLocal;
import olstore.entity.UserLocalHome;
import olstore.session.helper.UserHelper;

/**
 * A wrapper class for user 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 UserManager {
    
   /** A reference to the LocalHome inteface of the user EJB */
   private UserLocalHome home;
   /** A refernce to the user helper object */
   private UserHelper helper;
   
   /**
    * Constructor for the manager which is populated by Spring.
    * @param home a reference to the LocalHome interface
    * @param helper a helper object
    */
   public UserManager(UserLocalHome home, UserHelper helper) {
       this.home = home;
       this.helper = helper;
   }
   
   /**
    * Find a user by their username.
    * @param username the username to lookup
    * @return the UserLocal object for the user with the given username
    */
   public UserLocal findByUsername(String username) {
     UserLocal user;
     try {
       user = home.findByUsername(username);
     } catch(FinderException ex) {
       throw new EJBException("Unable to find user", ex);
     }
     
     return user;
     
   }
   
   /**
    * Find all registered users in this store.
    * @return a collection of all users.
    */
   public Collection findAll() {
       Collection c;
       try { 
           c = home.findAll();
       } catch (FinderException ex) {
           throw new EJBException("Unable to find users", ex);
       }
       return c;
   }
   
   /**
    * Find a user with given name and return a POJO for that user.
    * @param username the username to lookup
    * @return a plain java object for the user with the given username
    */
   public UserValue findUserValue(String username) {
       UserValue user;
       try {
         user = helper.UserToDTO(home.findByUsername(username));
       } catch(Exception ex) {
         throw new EJBException("Unable to find user", ex);
       }
       
       return user;
   }
   
   /**
    * Return all the orders that belong to the user.
    * @param username the username of the user whose orders are required
    * @return a collection of OrderEntry objects that belong to this user
    */
   public Collection getOrders(String username) {
       UserLocal user;
       try {
         user = home.findByUsername(username);
       } catch(FinderException ex) {
         throw new EJBException("Unable to find user", ex);
       }
       
       Collection entries = new ArrayList();
       Collection orderLocals = user.getOrders();
       
       Iterator orderIterator = orderLocals.iterator();
       while(orderIterator.hasNext()){
           OrderLocal orderLocal = (OrderLocal) orderIterator.next();
           OrderEntry orderEntry = new OrderEntry(orderLocal);
           entries.add(orderEntry);
       }      
       
       return entries;
   }
   
   public Collection getUserPurchased(String username) {
       UserLocal user;
       try {
           user = home.findByUsername(username);
         } catch(FinderException ex) {
           throw new EJBException("Unable to find user", ex);
         }
       return user.getPurchased();
   }
   
   /**
    * Save a user with the given username and all of their profile information
    * @param user the object representing the user
    * @param address the object that contains the user's address
    * @param username user's username
    * @throws Exception if an error occurs while saving user's information
    */
   public void saveUser(UserValue user, AddressValue address, String username) throws Exception {
       helper.SaveUser(user, address, username);
   }
   
   /**
    * Create a new user for customer in the store.
    * @param user the object representing the user
    * @param address the object representing the customer
    * @throws Exception if an error occures while creating the new user
    */
   public void createUser(UserValue user, AddressValue address) throws Exception {
       CreateUserValue userValue = new CreateUserValue();
       userValue.setUsername(user.getUsername());
       userValue.setPhoneNum(user.getPhoneNum());
       userValue.setPasswd1(user.getPasswd());
       userValue.setLname(user.getLname());
       userValue.setFname(user.getFname());
       userValue.setEmailAdd(user.getEmailAdd());
       helper.CreateUser(userValue, address);
   }

   /**
    * Returns a collection of all friends that have purchased the given item, and
    * are friends of the given user.
    * @param username the user whose friends we are interested in.
    * @param itemId the item ID to see which friends have purchased.
    * @return a collection of all friends that have purchased the given item, and
    * are friends of the given user.
    */
   public Collection findFriendsThatPurchasedItem(String username, Integer itemId) {
       Collection c;
       ArrayList friends = new ArrayList();
       try { 
           c = home.findFriendsThatPurchasedItem(username, itemId);
       } catch (FinderException ex) {
           throw new EJBException("Unable to find user/item", ex);
       }
       
       Iterator i = c.iterator();
       while(i.hasNext()) {
           friends.add(((UserLocal)i.next()).getUsername());
       }
       
       return friends;
   }
}
