/*  Copyright (c) 2001, 2003 Oracle Corporation. All Rights Reserved. */
package oracle.portal.provider.v2;

import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import oracle.portal.utils.xml.v2.XMLUtil;

public abstract class ProviderContext
{
    public static final String PROVIDER_SERVICE_CLASS = "serviceClass";
    public static final String PROVIDER_LOADER_CLASS = "loaderClass";
    public static final String SHOW_TEST_PAGE = "showTestPage";
    public static final String PROVIDER_DEFINITION = "definition";
    public static final String AUTO_RELOAD  = "autoReload";
    public static final String SERVICE_NAME = "serviceName";
    public static final String DEBUG_LEVEL = "debugLevel";
    public static final String REPOSITORY_PATH = "repositoryPath";
    public static final String TEST_PAGE_URI = "testPageURI";

    private Map mAttributes;
    private boolean mImmutable = false;

    /**
     * Creates instance of ProviderContext.
     *
     * @param repositoryPath
     * @param debugLevel
     * @param test Whether TestPage for the provider enabled
     */
    public ProviderContext(String repositoryPath,
                            int debugLevel,
                            boolean test)
    {
        this(repositoryPath,debugLevel,test,null);
    }

    /**
     * Creates instance of ProviderContext.
     *
     * @param repositoryPath
     * @param debugLevel
     * @param test Whether TestPage for the provider enabled
     * @param testPageURI URI to provider's test page if provider is not defaulting
     *                     to default test page.
     */
    public ProviderContext(String repositoryPath,
                            int debugLevel,
                            boolean test,
                            String testPageURI)
    {
        mAttributes = new HashMap();
        populateMap(repositoryPath, debugLevel, test, testPageURI);
    }

    public ProviderContext(String repositoryPath, int debugLevel, boolean test,
                           String testPageURI, Map attributes)
    {
        mAttributes = (attributes == null) ? (new HashMap()) : attributes;
        populateMap(repositoryPath, debugLevel, test, testPageURI);
    }

    public void setAttribute(String key, Object value) throws IllegalStateException
    {
        if (mImmutable)
        {
            throw new IllegalStateException("ProviderContext has been made " +
                                            "immutable and cannot be updated");
        }

        mAttributes.put(key, value);
    }

    public String getRepositoryPath()
    {
        return (String)mAttributes.get(REPOSITORY_PATH);
    }

    public int getDebugLevel()
    {
        Integer i = (Integer)mAttributes.get(DEBUG_LEVEL);
        return i.intValue();
    }

    public boolean isTestPageOn()
    {
        return XMLUtil.getBooleanValue((String)mAttributes.get(SHOW_TEST_PAGE));
    }

    /**
     * To Get the Test Page name in case Provider is not defaulting to
     * Default Test Page.
     *
     * @return Test Page Name
     */
    public String getTestPageURI()
    {
        return (String)mAttributes.get(TEST_PAGE_URI);
    }

    /**
     * Returns the attribute that corresponds with the supplied name.
     *
     * @param name  The name of the attribute you would like to retrieve.
     *
     * @return  A String representing the attribute that corresponds with the
     *          supplied name.
     */
    public Object getAttribute(String name)
    {
        return mAttributes.get(name);
    }

    /**
     * Returns all of the names of the attributes sored in this
     * <code>ProviderContext</code>.
     *
     * @return  A {@link java.util.Set} containing the names of the attributes
     *          found in this <code>ProviderContext</code>.
     */
    public Set getAttributeNames()
    {
       return mAttributes.entrySet();
    }

    /**
     * Returns the service name for the current provider.
     *
     * @return  A String representing the current provider's service name.
     */
    public String getServiceName()
    {
        return (String)mAttributes.get(SERVICE_NAME);
    }

    public boolean lockDown()
    {
        if (mAttributes.isEmpty())
        {
            return mImmutable;
        }
        else
        {
            mImmutable = true;
            return mImmutable;
        }
    }

    abstract public boolean isInvalidationCachingEnabled();

    // Returns the string value of a boolean so that it may be added to the
    // attribute map
    private String getStringValue(boolean b)
    {
        if (b)
        {
            return "true";
        }
        else
        {
            return "false";
        }
    }

    private void populateMap(String repositoryPath, int debugLevel,
                             boolean test, String testPageURI)
    {
        if (repositoryPath != null)
        {
            mAttributes.put(REPOSITORY_PATH, repositoryPath);
        }
        if (testPageURI != null)
        {
            mAttributes.put(TEST_PAGE_URI, testPageURI);
        }
        Integer i = new Integer(debugLevel);
        if (i != null)
        {
       		mAttributes.put(DEBUG_LEVEL, i);
		}
		String s = getStringValue(test);
		if (s != null)
		{
       		mAttributes.put(SHOW_TEST_PAGE, s);
		}
    }
}

