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

import olstore.framework.EJBHomeFactory;

import org.apache.struts.action.*;
//import org.apache.struts.actions.*;
import olstore.entity.UserLocal;
import olstore.entity.UserLocalHome;
import olstore.session.ShoppingCartLocal;
import olstore.session.ShoppingCartLocalHome;

import javax.ejb.FinderException;
//import java.io.IOException;
import javax.naming.NamingException;
import javax.ejb.CreateException;

public class DemoRequestProcessor extends RequestProcessor{
    
    
    protected boolean processPreprocess( HttpServletRequest request, 
            HttpServletResponse response){ 
        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){
                    EJBHomeFactory factory = EJBHomeFactory.getInstance();
                    UserLocalHome userHome = (UserLocalHome) factory.getLocalHome( EJBHomeFactory.USER );
                    ShoppingCartLocalHome shoppingCartHome = (ShoppingCartLocalHome)
                    factory.getLocalHome(EJBHomeFactory.SHOPPINGCART);
                    
                    UserLocal user = null;
                    user=userHome.findByUsername (request.getRemoteUser());
                    ShoppingCartLocal shoppingCart = shoppingCartHome.create(user);
                    session.setAttribute("shoppingCart", shoppingCart);
                }
                return true;			
            }else{
                return true;
            }
        }catch(FinderException e){
            System.out.println("FinderException" + e);
            return false;
        }catch(CreateException e){
            System.out.println("CreateException" + e);
            return false;
        }catch(NamingException e){
            System.out.println("NamingException" + e);
            return false;
        }catch(Exception e){
            System.out.println("General Exception" + e);
            return true;
        }
        
    }
    
    
    
} 

