/**
 * 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: Aizaz Ahmed
 *                     Vivek Lakshmanan
 *                     Andrew Overholt
 *                     Matthew Wringe
 *
 */
package olstore.action;

import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import olstore.domain.manager.OrderManager;
import olstore.dto.OrderEntry;
import olstore.form.UpdateOrderStatusForm;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
//import org.apache.commons.beanutils.BeanUtils;


public class OrderStatusAction extends DemoBaseAction {
    
    /**
     * Acts as a relay for all item related tasks with a default action
     * 
     */
    public ActionForward execute ( ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        
        
        String submitType = ((UpdateOrderStatusForm)form).getSubmitType();
        if ( submitType==null || submitType.equals(""))  {
            loadOrders  ( mapping, form, request, response );
        }else if(submitType.equals("update")){
            try{
                updateOrders(mapping, form, request, response);
                loadOrders ( mapping, form, request, response );
                ActionMessages messages = new ActionMessages();
                ActionMessage msg = new ActionMessage("order.update.success");
                messages.add("success", msg);
                saveMessages(request, messages);
            }catch(Exception e){
                ActionErrors errors = new ActionErrors();
                errors.add("error", new ActionError("errors.order.update", e.getMessage() ));
                saveErrors(request, errors);
                return (new ActionForward(mapping.getInput()));  
            }
        }
       
        return mapping.findForward ( "/admin/updateStatus" );
        
    }
    
    public void  resetForm ( ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        ((UpdateOrderStatusForm) form).reset();
    }
    
    public void  loadOrders ( ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        
        OrderManager order = (OrderManager)getWebApplicationContext().getBean("orderManager");
        
        ArrayList orderEntries = order.loadOrdersForUpdate();
        
        if(!orderEntries.isEmpty()){
            ((UpdateOrderStatusForm) form).setOrders(orderEntries);
        } else { 
            ActionMessages messages = new ActionMessages();
            ActionMessage msg = new
            ActionMessage("order.load.empty");
            messages.add("success", msg);
            saveMessages(request, messages);
            resetForm(mapping, form, request, response);		
        }
        
    }
    
    public void  updateOrders ( ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        Iterator ordersIterator = ((UpdateOrderStatusForm) form).getOrders().iterator();
        
        while(ordersIterator.hasNext()){
            OrderEntry orderEntry = (OrderEntry) ordersIterator.next();
            
            OrderManager order = (OrderManager)getWebApplicationContext().getBean("orderManager");
            order.setStatus(orderEntry.getOrderId(), orderEntry.getOrderStatus());
            
        }
    }
    
    
}
