/* Copyright (c) 2000, 2001, Oracle Corporation. All rights reserved. */
package oracle.portal.provider.v2.personalize;

import java.io.*;
import java.util.*;
import oracle.portal.provider.v2.*;
import oracle.portal.log.LogManager;
import oracle.portal.provider.v2.preference.NameValuePreferenceDataObject;

/**
 * Base customization data class. Manages customization data all portlets
 * should support. By default, this is the portlet's title and short title.<p>
 *
 * Generally, portlets should support customization even if they only allow a
 * user to customize the titlebar. Portlets providing further customization
 * (data) should use a subclassed version of this class as a data class and
 * override the init() method so that further data items can be defaulted.
 */
public class NameValuePersonalizationObject 
    extends NameValuePreferenceDataObject
    implements PersonalizationObject
{
    // Names with special associated values
    private static final String TITLE_KEY = "NVPO_TITLE";
    private static final String SHORT_TITLE_KEY = "NVPO_SHORT_TITLE";

    /**
     * path that was used to store this object
     */
    private String mReferencePath = null;

    
    /**
     * Null Constructor.
     */
    public NameValuePersonalizationObject() 
    {
        super();
    }

    /**
     *  Init method used to initialize the data object after instantiation via
     *  its null constructor.  Defined by the PersonalizationObject interface.
     *  Personalization manager is expected to call the init method of data
     *  objects that implement the PersonalizationObject interface.<p>
     *
     *  Behavior is to initialize the title using the (static) title from the
     *  portlet metadata (<code>portlet.getTitle()</code>). The portlet's
     *  default Locale is used to retrieve the title.
     *
     *  @param params collection of parameters that can be used to initialize
     *         this object. Currently 1 parameter is expected - the PortletReference
     */
    public void init(PortletReference pr) 
    {
        if (pr == null)
        {
            LogManager.warning("NameValuePersonalizationObject: Expected " +
                               "initialization parameter - PortletReference - " +
                               "is null. Object not initialized");
            return;
        }
            
        try 
        {
            String title = pr.getPortletDefinition().getTitle(pr.getLocale());
            if (title == null)
            {
                title = "";
            }
            setPortletTitle(title);
        }
        catch (Exception e) 
        {
            LogManager.log("Unable to initialize portlet title. " +
                                e.getMessage(), e);
        }

        try
        {
            String sTitle = pr.getPortletDefinition().getShortTitle(pr.getLocale());
            if (sTitle == null)
            {
                sTitle = "";
            }
            setPortletShortTitle(sTitle);
        }
        catch (Exception e)
        {
            LogManager.log("Unable to initialize portlet short title. " +
                                 e.getMessage(), e);
        }
    }


    /**
     * Optionally records the identifying path string used to store and
     * retrieve the contents of this PersonalizationObject from the
     * storage medium
     *
     * @param path identifying path against which the
     * PersonalizationObject data is stored
     */
    public void setReferencePath(String path)
    {
        mReferencePath = path;
    }


    /**
     * Gets the the identifying path string used to store and retrieve the
     * contents of this PersonalizationObject from the storage medium
     *
     * @return identifying path string
     */
    public String getReferencePath()
    {
        return mReferencePath;
    }

    /**
     * Sets the string to be displayed on the Portlet's title bar.
     *
     * @param title string to be displayed on the Portlet's title bar.
     */
    public void setPortletTitle(String title)
    {
        putString(TITLE_KEY, title);
    }

    /**
     * Gets the string to be displayed on the Portlet's title bar.
     *
     * @return title string to be displayed on the Portlet's title bar.
     */
    public String getPortletTitle()
    {
        return getString(TITLE_KEY);
    }

    /**
     * Sets the string to be displayed for the Portlet's title when there
     * is limited space.  For example the menu item in mobile page.
     *
     * @param title string to be displayed for the Portlet's title when there
     * is limited space.
     */
    public void setPortletShortTitle(String title)
    {
        putString(SHORT_TITLE_KEY, title);
    }

    /**
     * Gets the string to be displayed for the Portlet's title when there
     * is limited space.  For example the menu item in mobile page.
     *
     * @return title string to be displayed for the Portlet's title when there
     * is limited space.
     */
    public String getPortletShortTitle()
    {
        return getString(SHORT_TITLE_KEY);
    }
}


