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


import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.ejb.EJBLocalHome;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import olstore.entity.AddressUtil;
import olstore.entity.FriendUtil;
import olstore.entity.ItemUtil;
import olstore.entity.OrderUtil;
import olstore.entity.PictureUtil;
import olstore.entity.PropertyUtil;
import olstore.entity.TypeUtil;
import olstore.entity.UserUtil;
import olstore.session.ShoppingCartUtil;
import olstore.session.helper.ItemHelperUtil;
import olstore.session.helper.TypeHelperUtil;
import olstore.session.helper.UserHelperUtil;

/**
 * Used to get the local homes of the entity and session beans.
 */
public class EJBHomeFactory {
    private Map homes;
    private static EJBHomeFactory factory;
    private Context context;
    
    private static final String NAMESPACE = "java:comp/env/ejb/";
    
    public static final String ITEM = NAMESPACE + "ItemLocal";
    public static final String ITEM_HELPER = NAMESPACE + "ItemHelperLocal";
    public static final String TYPE_HELPER = NAMESPACE + "TypeHelperLocal";
    public static final String PROP = NAMESPACE + "PropertyLocal";
    public static final String PIC = NAMESPACE + "PictureLocal";
    public static final String TYPE = NAMESPACE + "TypeLocal";
    public static final String USER = NAMESPACE + "UserLocal";
    public static final String FRIENDS = NAMESPACE + "FriendLocal";
    public static final String ORDER = NAMESPACE + "OrderLocal";
    public static final String ADDRESS=NAMESPACE + "AddressLocal";
    
    public static final String USER_HELPER= NAMESPACE+"UserHelperLocal";
    public static final String SHOPPINGCART= NAMESPACE+ "ShoppingCartLocal";
    
    
    
    /**
     * Initialize the factory, cache the context and prepare the home cache
     */
    private EJBHomeFactory() throws NamingException {
        homes = Collections.synchronizedMap ( new HashMap() );
        context = new InitialContext ();
    }
    
    /**
     * Use this method to get an instance of the factory
     */
    public static EJBHomeFactory getInstance() throws NamingException {
        if ( factory == null ) {
            factory = new EJBHomeFactory();
        }
        return factory;
    }
    
    /**
     * This is designed only for local home interfaces.
     */
    public EJBLocalHome getLocalHome ( String jndiName ) throws NamingException {
        EJBLocalHome home = null;
        if(jndiName.equals(ITEM))
            home = (EJBLocalHome) ItemUtil.getLocalHome();
        if(jndiName.equals(ITEM_HELPER))
            home = (EJBLocalHome) ItemHelperUtil.getLocalHome();
        if(jndiName.equals(TYPE_HELPER))
            home = (EJBLocalHome) TypeHelperUtil.getLocalHome();
        if(jndiName.equals(PROP))
            home = (EJBLocalHome) PropertyUtil.getLocalHome();
        if(jndiName.equals(PIC))
            home = (EJBLocalHome) PictureUtil.getLocalHome();
        if(jndiName.equals(TYPE))
            home = (EJBLocalHome) TypeUtil.getLocalHome();
        if(jndiName.equals(USER))
            home = (EJBLocalHome) UserUtil.getLocalHome();
        if(jndiName.equals(FRIENDS))
            home = (EJBLocalHome) FriendUtil.getLocalHome();
        if(jndiName.equals(ORDER))
            home = (EJBLocalHome) OrderUtil.getLocalHome();
        if(jndiName.equals(ADDRESS))
            home = (EJBLocalHome) AddressUtil.getLocalHome();
        if(jndiName.equals(USER_HELPER))
            home = (EJBLocalHome) UserHelperUtil.getLocalHome();
        if(jndiName.equals(SHOPPINGCART))
            home = (EJBLocalHome) ShoppingCartUtil.getLocalHome();
        
        //home = (EJBLocalHome)homes.get ( jndiName );
        //if ( home == null ) {
        //   home = (EJBLocalHome) context.lookup ( jndiName );
        //   homes.put ( jndiName, home );
        //}
        return home;
    }  
}
