/**
 * 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.OrderEntry;
import olstore.entity.OrderLocal;
import olstore.entity.OrderLocalHome;

/**
 * A wrapper class for order 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 OrderManager {
    
   /** The olstore bean instance that is provided via Spring. */
   private OrderLocalHome home;
   
   /**
    * Constructs a new order manager with the LocalHome bean injected by
    * spring
    * @param home the LocalHome interface of the Order EJB
    */
   public OrderManager(OrderLocalHome home) {
       this.home = home;
   }
    
   /**
    * Updates the status of a given order.
    * @param id the id of the order
    * @param status the new status of the order
    */
   public void setStatus(Integer id, String status) {
     try {
       home.findByPrimaryKey(id).setStatus(status);
     } catch(FinderException ex) {
       throw new EJBException("Unable to find order", ex);
     }
   }
   
   /**
    * Load orders whose status needs to be updated by the admin.
    * @param status the status of the orders to load
    * @return a list of OrderEntry objects for each order that can be updated
    */
   public ArrayList loadOrdersForUpdate() {
       ArrayList entries = new ArrayList();
       Collection orderLocals;
       try {
           orderLocals = home.findStatusWithout("Delivered");
       } catch (FinderException e) {
           throw new EJBException("Unable to find orders that are not 'Delivered'", e);
       }
       Iterator orderIterator = orderLocals.iterator();
       while(orderIterator.hasNext()){
           OrderLocal orderLocal = (OrderLocal) orderIterator.next();
           OrderEntry orderEntry = new OrderEntry(orderLocal);
           entries.add(orderEntry);
       }
       
       return entries;
   }
   
   /**
    * Load orders whose status needs to be updated by the admin.
    * @param status the status of the orders to load
    * @return a list of OrderEntry objects for each order that can be updated
    */
   public Collection findOrdersForUpdate() {
       Collection orders;
       try {
           orders = home.findStatusWithout("Delivered");
         } catch(FinderException ex) {
           throw new EJBException("Unable to find orders for update.", ex);
         }
         return orders;
   }
}
