/* Copyright (c) 2000, 2001, Oracle Corporation. All Rights Reserved. */

package oracle.portal.sample.v2.devguide.subscriber;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import oracle.portal.provider.v2.*;
import oracle.portal.provider.v2.preference.*;
import oracle.portal.provider.v2.security.AccessControlException;

/**
 * The SubscriberProvider is a provider which extends ProviderInstance, the
 * JPDKs base implementation class for the Provider interface. The purpose of
 * this class is to illustrate:
 *  - How to extend ProviderInstance
 *  - How to use the Subscription Key
 *  - How to use a PreferenceStore to store custom instance data
 * This class provides the implementation for the getSubscription method and
 * also illustrates a simple way of making the subscription key persistent
 * using a PreferenceStore.
 */
 
public class SubscriberProvider extends ProviderInstance
{
    private static final String SUBSCRIPTION_KEY_PREF_TYPE = "oracle.portal.sample.v2.subscription";
    private static final String SUBSCRIPTION_KEY_DATUM_NAME = "SUBSCRIPTION_KEY";

    private ProviderSubscription mSubscription;

    public SubscriberProvider()
    {
    }

    public void init(String providerId, ProviderDefinition definition) 
        throws ProviderException
    {
        super.init(providerId, definition);
        try
        {
            loadSubscription();
        }
        catch(EntryNotFoundException enfe)
        {
            //If we don't have a stored subscription key, this probably means
            //the provider hasn't been registered yet
        }
        catch(PreferenceStoreException pse)
        {
            throw new ProviderException(pse);
        }
    }

    public ProviderSubscription getSubscription()
    {
        return mSubscription;
    }
    

    public ProviderRegistrationInfo register(String subscriptionKey, 
                                             String portalVersion)
        throws ProviderException, IOException, AccessControlException
    {
        super.register(subscriptionKey, portalVersion);

        mSubscription = new ProviderSubscription(subscriptionKey);

        try
        {
            // Store away this new subscription
            storeSubscription();
        }
        catch(PreferenceStoreException pse)
        {
            throw new ProviderException(pse);
        }

        // We don't have a handle to encryption key as yet so we pass null.
        return new ProviderRegistrationInfo(subscriptionKey, null);
    }
    
    
    public void deregister() throws ProviderException
    {
        super.deregister();

        try
        {
            // Remove this subscription
            destroySubscription();
        }
        catch(PreferenceStoreException pse)
        {
            throw new ProviderException(pse);
        }

    }

    private Preference getSubscriptionPreference()
        throws PreferenceStoreException
    {
        return new Preference(getProviderId(),
                              SUBSCRIPTION_KEY_PREF_TYPE,
                              NameValuePreferenceDataObject.class.getName());
    }

    private Preference getSubscriptionPreference(NameValuePreferenceDataObject data)
        throws PreferenceStoreException
    {
            return new Preference(getProviderId(),
                                  SUBSCRIPTION_KEY_PREF_TYPE,
                                  data);
    }
        
    private void loadSubscription()
        throws EntryNotFoundException, PreferenceStoreException
    {
        Preference subscriptionKeyPref = getSubscriptionPreference();
        getPreferenceStore().read(subscriptionKeyPref);
        NameValuePreferenceDataObject data = (NameValuePreferenceDataObject)subscriptionKeyPref.getData();
        String subsciptionKey = data.getString(SUBSCRIPTION_KEY_DATUM_NAME);
        mSubscription = new ProviderSubscription(subsciptionKey);
    }

    private void destroySubscription()
        throws EntryNotFoundException, PreferenceStoreException
    {
        Preference subscriptionKeyPref = getSubscriptionPreference();
        getPreferenceStore().destroy(subscriptionKeyPref);
        mSubscription = null;
    }

    private void storeSubscription()
        throws PreferenceStoreException
    {
        NameValuePreferenceDataObject data = new NameValuePreferenceDataObject();
        data.putString(SUBSCRIPTION_KEY_DATUM_NAME,
                       mSubscription.getSubscriptionKey());
        Preference subscriptionKeyPref = getSubscriptionPreference(data);
        getPreferenceStore().write(subscriptionKeyPref, true);
    }
}

