/**
 * 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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import olstore.dto.ItemValue;
import olstore.dto.PictureValue;
import olstore.framework.EJBHomeFactory;
import olstore.util.WebUtils;

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 olstore.session.ShoppingCartLocal;
import olstore.session.helper.ItemHelperLocal;
import olstore.session.helper.ItemHelperLocalHome;


public class ViewItemAction 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 {
        
        ActionErrors errors = new ActionErrors();
        ActionMessages messages = new ActionMessages();
        
        String itemId = WebUtils.getValue ( "itemId", request );
        String interest = WebUtils.getValue ( "interest", request );
        String purchase = WebUtils.getValue ( "purchase", request );
        
        EJBHomeFactory factory = EJBHomeFactory.getInstance();
        ItemHelperLocalHome itemHelperHome = (ItemHelperLocalHome) factory.getLocalHome ( EJBHomeFactory.ITEM_HELPER );
        ItemHelperLocal itemHelper = itemHelperHome.create();
        
        // Load up the item
        ItemValue item = null;
        if ( itemId != null ) {
            try {
                item = itemHelper.getItemValueForId ( itemId );
            } catch ( Exception e ) {
                //Logging code here
                errors.add("error", new ActionError("errors.item.load", e.getMessage() ));
                saveErrors(request, errors);
                return (new ActionForward(mapping.getInput()));
            }
        }
        if ( item == null ) {
            errors.add("error", new ActionError("errors.item.view") );
            saveErrors(request, errors);
            return (new ActionForward(mapping.getInput()));
        }
        
        request.setAttribute ( "item", item );
        PictureValue pic = itemHelper.getPicture ( itemId );
        if ( pic != null ) {
            request.setAttribute ( "pic", pic );
        }
        
        
        // Mark the item as interesting
        if ( interest != null && interest.equals ( "true" ) ) {
            try {
                itemHelper.markItemAsInteresting ( itemId, request.getRemoteUser() );
                ActionMessage msg = new ActionMessage("item.interest.success", 
                        item.getName() );
                messages.add("success", msg);
                saveMessages(request, messages);
            } catch ( Exception e ) {
                errors.add("error", new ActionError("errors.item.interesting", 
                        item.getName(),
                        request.getRemoteUser(),
                        e.getMessage() ));
                saveErrors(request, errors);
                return (new ActionForward(mapping.getInput()));
            }
        }
        
        
        // Add the item to the shoppng cart
        if ( purchase != null && purchase.equals ( "true" ) ) {
            try {
                
                HttpSession session = request.getSession();
                ShoppingCartLocal cart = (ShoppingCartLocal ) session.getAttribute ( "shoppingCart" );
                cart.updateQuantity( new Integer ( itemId ), new Integer(1) );
                
                ActionMessage msg = new ActionMessage("item.purchase.success", 
                        item.getName() );
                messages.add("success", msg);
                saveMessages(request, messages);
            } catch ( Exception e ) {
                errors.add("error", new ActionError("errors.item.purchase", 
                        item.getName(),
                        request.getRemoteUser(),
                        e.getMessage() ));
                saveErrors(request, errors);
                return (new ActionForward(mapping.getInput()));
            }
        }
        
        return mapping.findForward ( "updateItemView" );
        
    }
}
