/**
 * 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.global;

//import javax.servlet.ServletException;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import olstore.domain.manager.UserManager;
import olstore.entity.UserLocal;
import olstore.session.ShoppingCartLocal;
import olstore.session.ShoppingCartLocalHome;

import org.apache.struts.action.RequestProcessor;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoRequestProcessor extends RequestProcessor{
    
    private ApplicationContext ctx;
    
    protected boolean processPreprocess( HttpServletRequest request, 
            HttpServletResponse response){ 
        ctx = new ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml");
        System.out.println("CTX IN INIT " + ctx);
        
        try{
            if(request.getRemoteUser()!=null || request.getUserPrincipal()!=null){
              HttpSession session = request.getSession(false);  // session should already be set if logged in.
                if(session.getAttribute("shoppingCart")==null){
                    UserLocal user = ((UserManager)ctx.getBean("userManager")).findByUsername(request.getRemoteUser());
                    
                    ShoppingCartLocalHome shoppingCartHome = (ShoppingCartLocalHome)ctx.getBean("shoppingCart");
                    ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
                    
                    /*
                     * BeanNotOfRequiredTypeException - if the bean is not of the required type 
                     * NoSuchBeanDefinitionException - if there's no such bean definition 
                     * BeansException - if the bean could not be created
                     */
                    session.setAttribute("shoppingCart", shoppingCart);
                }
                return true;			
            }else{
                return true;
            }
        }catch(BeansException e){
            System.out.println("BeansException" + e);
            return false;
        }catch(Exception e){
            System.out.println("General Exception" + e);
            return true;
        }
        
    }
    
    
    
} 

