/* Copyright (c) 2002, 2003, Oracle Corporation. All Rights Reserved. */
package oracle.portal.sample.v2.devguide.eventform;

import java.util.ResourceBundle;
import oracle.portal.provider.v2.personalize.PortletReference;
import oracle.portal.provider.v2.personalize.NameValuePersonalizationObject;
import oracle.portal.sample.v2.devguide.eventform.resource.EventFormBundle;

/**
 * A class that implements the sample event portlet's customization storage.
 */
public class EventFormCustomization extends NameValuePersonalizationObject
{
    /**
     * Key for the value of paramter1 when firing the 'submit' event on a
     * mobile display.
     */
    private static final String PARAMETER1_VALUE_KEY = "PARAM1VALUE";

    /**
     * Key for the value of paramter2 when firing the 'submit' event on a
     * mobile display.
     */
    private static final String PARAMETER2_VALUE_KEY = "PARAM2VALUE";

    /**
     * Sample event portlet customization constructor.
     */
    public EventFormCustomization()
    {
    }

    /**
     * Set initial values for customizable values.
     */
    public void init(PortletReference ref)
    {
        // Because we don't want any values with localized defaults to be fixed
        // until customization, including the portlet title, we do not call
        // super.init()

        setParameter1ValueForMobile("");
        setParameter2ValueForMobile("");
    }

    /**
     * Removes edit default data.
     *
     * This class is used to store both customizations and edit defaults data.
     * When using an object for customization this method should be called
     * to remove the initialized edit defaults values.
     */
    public void removeEditDefaultData()
    {
        remove(EventFormBundle.SPLASHTEXT_KEY);
        remove(EventFormBundle.PARAMETER1_LABEL_KEY);
        remove(EventFormBundle.PARAMETER2_LABEL_KEY);
        remove(PARAMETER1_VALUE_KEY);
        remove(PARAMETER2_VALUE_KEY);
        remove(EventFormBundle.SUBMIT_LABEL_KEY);
        remove(EventFormBundle.CANCEL_LABEL_KEY);
    }

    public String getSplashText(PortletReference portletRef)
    {
        return getLocalizedString(EventFormBundle.SPLASHTEXT_KEY, portletRef);
    }

    public void setSplashText(String splashText)
    {
        putString(EventFormBundle.SPLASHTEXT_KEY, splashText);
    }

    public String getParameter1Label(PortletReference portletRef)
    {
        return getLocalizedString(EventFormBundle.PARAMETER1_LABEL_KEY, portletRef);
    }

    public void setParameter1Label(String parameter1Label)
    {
        putString(EventFormBundle.PARAMETER1_LABEL_KEY, parameter1Label);
    }

    public String getParameter1ValueForMobile()
    {
        return getString(PARAMETER1_VALUE_KEY);
    }

    public void setParameter1ValueForMobile(String parameter1Value)
    {
        putString(PARAMETER1_VALUE_KEY, parameter1Value);
    }

    public String getParameter2Label(PortletReference portletRef)
    {
        return getLocalizedString(EventFormBundle.PARAMETER2_LABEL_KEY, portletRef);
    }

    public void setParameter2Label(String parameter2Label)
    {
        putString(EventFormBundle.PARAMETER2_LABEL_KEY, parameter2Label);
    }

    public String getParameter2ValueForMobile()
    {
        return getString(PARAMETER2_VALUE_KEY);
    }

    public void setParameter2ValueForMobile(String parameter2Value)
    {
        putString(PARAMETER2_VALUE_KEY, parameter2Value);
    }

    public String getSubmitLabel(PortletReference portletRef)
    {
        return getLocalizedString(EventFormBundle.SUBMIT_LABEL_KEY, portletRef);
    }

    public void setSubmitLabel(String submitLabel)
    {
        putString(EventFormBundle.SUBMIT_LABEL_KEY, submitLabel);
    }

    public String getCancelLabel(PortletReference portletRef)
    {
        return getLocalizedString(EventFormBundle.CANCEL_LABEL_KEY, portletRef);
    }

    public void setCancelLabel(String cancelLabel)
    {
        putString(EventFormBundle.CANCEL_LABEL_KEY, cancelLabel);
    }

    private String getLocalizedString(String key, PortletReference portletRef)
    {
        String value = getString(key);
        if (value == null)
        {
            ResourceBundle bundle = portletRef.getPortletDefinition()
                .getResourceBundle(portletRef.getLocale());
            value = bundle.getString(key);
        }
        return value;
    }
}

