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

package oracle.portal.provider.v1;

import java.util.*;

/**
 *  A particular portlet instance is uniquely identified by four values:
 *  providerId + portletId + portletInstanceName + portletUserCustomization.
 *  A PortletIdentifer encapsulates this information.  Of these four values
 *  only the portletUserCustomization is allowed to be null.  When null it
 *  signifies the default (system) portlet instance.
 */

public class PortletReference extends Object {


    private Provider mProvider; // Provider that manages this instances Portlet
    private Portlet mPortlet;  // Portlet that manages this instance ID
    private String mInstanceName; // portlet instance name
    private ProviderUser mCustomization; // particular user customization (if null then identified default instance)

    private Locale mLocale; // locale that should be used when operating on behalf of this reference
    private Locale mDefaultLocale; // Portal's default locale
    private boolean mTranslateDefaultCustomizations; // are we in translate mode?


    /**
     * Constructor
     *
     * @param provider Provider of this instance
     * @param portlet Portlet of this instance
     * @param name portal generated name of this instance
     * @param user specific user customization reference of this instance.  If
     * null it signifies the default (system) instance.
     * @param l the current language/locale setting of this request/user.
     * @param defLocale  the default language/locale setting of the portal
     * @param translate whether or not we are in translate mode.
     */
    public PortletReference(Provider provider, Portlet portlet, String name, ProviderUser user, Locale l, Locale defLocale, boolean translate) {
        mInstanceName = name;
        mCustomization = user;
        mPortlet = portlet;
        mProvider = provider;
        if (l != null) {
            mLocale = l;
        } else if (defLocale != null) {
            mLocale = defLocale;
        } else {
            mLocale = portlet.getDefaultLocale();
        }
        if (defLocale != null) {
            mDefaultLocale = defLocale;
        } else {
            mDefaultLocale = portlet.getDefaultLocale();
        }
        mTranslateDefaultCustomizations = translate;
    }

    /**
     * @return the Provider identified by this reference.
     */
    public Provider getProvider() {
        return mProvider;
    }

    /**
     * @return the Provider ID of the Provider identified by this reference.  This
     * is a convenience method for calling getProvider().getProviderId();
     */
    public long getProviderId() {
        return mProvider.getProviderId();
    }


    /**
     * @return the Portlet identified by this reference.
     */
    public Portlet getPortlet() {
        return mPortlet;
    }

    /**
     * @return the Portlet ID of the Portlet identified by this reference.  This
     * is a convenience method for calling getPortlet().getId();
     */
    public long getPortletId() {
        return mPortlet.getId();
    }


    /**
     * @return the Portlet instance name of this reference.
     */
    public String getName() {
        return mInstanceName;
    }

    /**
     * @return the user whose customization is requested with this instance.  If
     * <b>null</b> then this reference indicates the default (system) instance.
     *
     */
    public ProviderUser getUserCustomization() {
        return mCustomization;
    }

    /**
     *  @return the Locale that should be used on behalf of this reference.
     */
    public Locale getLocale() {
        return mLocale;
    }

    /**
     *  @return the default Locale that should be used on behalf of this reference.  This is
     *  generally the Portal's default locale.  However if not set/passed by the portal then
     *  its the Portlet default locale (see <code>Portlet.getDefaultLocale()</code>)
     */
    public Locale getDefaultLocale() {
        return mDefaultLocale;
    }

    /**
     *  The Portal supports an edit mode that allows a page developer to create
     *  multiple language versions of a page including the default customizations.
     *  The <code>boolean</code> return value from this call indicates whether or
     *  not the Portlet is in this mode.  If <code>false</code> the Portlet should
     *  save all edit_default customizations in the default (language) customization.
     *  If <code>true</code> the Portlet should save all edit_default customizations
     *  in the current language customization.
     *
     *  @return <code>false</code> if not in translate mode.
     */
    public boolean translateDefaultCustomizations() {
        return mTranslateDefaultCustomizations;
    }

    /**
     *  @return true if this instance identifies a default portlet instance
     *  vs. a user (customized) portlet instance.  Will be false if getUserCustomization()
     *  would return <b>null</b>.
     */
    public boolean isDefault() {
        return mCustomization == null;
    }

    /**
     *  @return true if this instance identifies a default portlet instance of a particular
     *  language (that isn't the default language)
     *  vs. a default portlet reference (to the default language) or
     *  a user (customized) portlet instance.  I.e. returns <code>true</code> if
     *  <code>isDefault()</code> is true and this references Locale isn't equal to
     *  the default locale.
     */
    public boolean isLanguageDefault() {
        if (isDefault()) {
            return mLocale != null && mDefaultLocale != null && !mLocale.equals(mDefaultLocale);
        } else {
            return false;
        }
    }

    /**
     *  @return a String representation of this reference.  In the form of
     *  <i>ProviderId.PortletId.PortletInstanceName.UserName</i>.  If this
     *  is a reference to the default (system) instance then the <i>.Username</i>
     *  is ommitted.
     */
    public String toString() {
        if (mCustomization == null) {
            return getProviderId() + "." + getPortletId() + "." + getName();
        } else {
            return getProviderId() + "." + getPortletId() + "." + getName() + "." + getUserCustomization().getName();
        }
    }
}

 