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

import java.util.Map;
import oracle.portal.provider.v2.PortalConfig;
import oracle.portal.provider.v2.monitor.PerfMonitor;

/**
 * A <code>RenderContext</code> is used to describe to a {@link
 * oracle.portal.provider.v2.PortletInstance} the context in which it is to be
 * renderered. As well as possessing a set of 'bean-like' properties, it has a
 * table of attributes (returned by {@link #getAttributes getAttributes}) that
 * can be used to pass environment-specific parameters into the portlet
 * instance. For example, in a servlet environment, the attribute table might
 * store the servlet request and response.
 */
public class RenderContext
{
    private int mRenderMode;
    private String mPageURL;
    private String mDesignURL;
    private String mBackURL;
    private String mLoginURL;
    private boolean mHasTitle;
    private boolean mHasBorder;
    private boolean mShowDetails;
    private boolean mShowEdit;
    private boolean mShowHelp;
    private boolean mShowAbout;
    private boolean mShowRemove;
    private boolean mShowCollapse;
    private boolean mIsCollapsed;
    private boolean mIsRewriteUrls;
    private String mPortletTemplate;

    private DeviceInfo mDeviceInfo;
    private PortalConfig mPortalConfig;
    private Map mAttributes;
    private int mWCMaxExpiryTime;
    private PerfMonitor mPerfMonitor;

    /**
     * opaque identifier for the portlet reference on a page
     */
    private String mPortletPageReference;

    /**
     * url to event servlet
     */
    private String mEventURL;


    public RenderContext(int renderMode, String pageURL, String designURL,
                         String backURL, String loginURL,
                         boolean hasTitle, boolean hasBorder,
                         boolean showDetails, boolean showEdit,
                         boolean showHelp, boolean showAbout,
                         boolean showRemove, boolean showCollapse,
                         boolean isCollapsed, boolean isRewriteUrls,
                         String portletTemplate,
                         DeviceInfo deviceInfo, PortalConfig portalConfig,
                         Map attributes,
                         int WCMaxExpiryTime,
                         String portletPageReference,
                         String eventURL,
                         PerfMonitor perfMonitor)
    {
        mRenderMode = renderMode;
        mPageURL = pageURL;
        mDesignURL = designURL;
        mBackURL = backURL;
        mLoginURL = loginURL;
        mHasTitle = hasTitle;
        mHasBorder = hasBorder;
        mShowDetails = showDetails;
        mShowEdit = showEdit;
        mShowHelp = showHelp;
        mShowAbout = showAbout;
        mShowRemove = showRemove;
        mShowCollapse = showCollapse;
        mPortletTemplate = portletTemplate;
        mDeviceInfo = deviceInfo;
        mPortalConfig = portalConfig;
        mAttributes = attributes;
        mWCMaxExpiryTime = WCMaxExpiryTime;
        mPortletPageReference = portletPageReference;
        mEventURL = eventURL;
        mIsCollapsed = isCollapsed;
        mPerfMonitor = perfMonitor;
        mIsRewriteUrls = isRewriteUrls;
    }

    /**
     * Gets an integer representing the render mode. Constants for each of
     * the render modes are defined in the {@link PortletRenderer} class.
     *
     * @return an integer representing the render mode.
     */
    public int getMode()
    {
        return mRenderMode;
    }

    /**
     * Gets a URL that returns one to the portal page that invoked this
     * portlet. Note: Query string is included if applicable.
     *
     * @return a URL that returns one to the portal page that invoked this
     * portlet.
     */
    public String getDesignURL() throws java.lang.IllegalStateException
    {
        if (mDesignURL == null) throw new java.lang.IllegalStateException("DesignURL not passed in request.");

        return mDesignURL;
    }

    /**
     * Gets the URL of the page that invoked this portlet. Portlets use this to
     * (re)write URLs in their response that should redisplay the portlet in
     * this page. Note: Query string is included if applicable.
     *
     * @return the URL of the page that invoked this portlet.
     */
    public String getPageURL() throws java.lang.IllegalStateException
    {
        if (mPageURL == null) throw new java.lang.IllegalStateException("PageURL not passed in request.");

        return mPageURL;
    }


    /**
     * Gets the URL that can be used to target portlet events.  Portlets
     * should use this to write URLs in their response that will raise an event
     * they have declared.  This should be used in conjuction with methods
     * from {@link oracle.portal.provider.v2.render.PortletRendererUtil}.
     *
     * @return the URL that can be used to target portlet events.
     */
    public String getEventURL()
    {
        if (   mEventURL == null
            || mEventURL.equals(""))
        {
           throw new java.lang.IllegalStateException("EventURL not passed in request.");
        }

        return mEventURL;
    }


    /**
     * Gets the URL of the page that caused this page to display.
     *
     * @return the URL of the page that caused this page to display.
     */
    public String getBackURL()
    {
        return mBackURL;
    }

    /**
     * Gets the login URL.
     *
     * @return the login URL.
     */
    public String getLoginServerURL() throws java.lang.IllegalStateException
    {
        if (mLoginURL == null) throw new java.lang.IllegalStateException("LoginUrl not passed in request.");

        return mLoginURL;
    }

    /**
     *  Gets an indicator of whether this portlet should draw a title or not.
     *
     *  @return an indicator of whether this portlet should draw a title or not.
     */
    public boolean hasTitle()
    {
        return mHasTitle;
    }

    /**
     * Gets an indicator of whether this portlet should draw its border or
     * not.
     *
     * @return an indicator of whether this portlet should draw its border or
     * not.
     */
    public boolean hasBorder()
    {
        return mHasBorder;
    }


    /**
     * Gets an indicator of whether this portlet should draw its details link or
     * not.
     *
     * @return an indicator of whether this portlet should draw its details link or
     * not.
     */
    public boolean showDetails()
    {
        return mShowDetails;
    }

    /**
     * Gets an indicator of whether this portlet should draw its edit link or
     * not.
     *
     * @return an indicator of whether this portlet should draw its edit link or
     * not.
     */
    public boolean showEdit()
    {
        return mShowEdit;
    }

    /**
     * Gets an indicator of whether this portlet should draw its help link or
     * not.
     *
     * @return an indicator of whether this portlet should draw its help link or
     * not.
     */
    public boolean showHelp()
    {
        return mShowHelp;
    }

    /**
     * Gets an indicator of whether this portlet should draw its about link or
     * not.
     *
     * @return an indicator of whether this portlet should draw its about link or
     * not.
     */
    public boolean showAbout()
    {
        return mShowAbout;
    }

    /**
     * Gets an indicator of whether this portlet should draw its remove link or
     * not.
     *
     * @return an indicator of whether this portlet should draw its remove link or
     * not.
     */
    public boolean showRemove()
    {
        return mShowRemove;
    }


    /**
     * Gets an indicator of whether this portlet should draw its
     * collapse/restore link or not.
     * @return an indicator of whether this portlet should draw its
     * collapse/restore link or not.
     */
    public boolean showCollapse()
    {
        return mShowCollapse;
    }


    /**
     * Gets an indicator of whether this portlet is collapsed or
     * not.
     *
     * @return an indicator of whether this portlet is collapsed or
     * not.
     */
    public boolean isCollapsed()
    {
        return mIsCollapsed;
    }

    /**
     * Gets an indicator of whether this portlet should use rewrite Urls
     * or not.
     *
     * @return an indicator of whether this portlet should use rewrite Urls or
     * not.
     */
    public boolean isRewriteUrls()
    {
        return mIsRewriteUrls;
    }

    /**
     * Gets the Portlet Template for the rendering of this portlet.
     *
     * @return the Portlet Template for the rendering of this portlet.
     */
    public String getPortletTemplate()
    {
        return mPortletTemplate;
    }

    /**
     *  Gets the device information for this request.
     *
     *  @return the device information for this request.
     */
    public DeviceInfo getDeviceInfo()
    {
        return mDeviceInfo;
    }

    /**
     *  Gets an object containing information pertinent to the portlet about
     *  how the portal is currently configured. Portlets may use this
     *  information to add customization fields to its screens or help
     *  information that pertain to mobile.
     *
     *  @return an object containing information about how the portal is
     *  currently configured.
     */
    public PortalConfig getPortalConfig()
    {
        return mPortalConfig;
    }

    /**
     * Gets a Map of 'environment specific' attributes related to this request.
     *
     * @return a Map of 'environment specific' attributes related to this
     * request.
     */
    public Map getAttributes()
    {
        return mAttributes;
    }

    /**
     * Gets the Maximum Expiry time which needs to be used by the portlets
     * which are using invalidation based caching.
     *
     * @return the Maximum Expiry time which needs to be used by the portlets
     * which are using invalidation based caching.
     */
    public int getWCMaxExpiryTime()
    {
        return mWCMaxExpiryTime;
    }

    /**
     * Gets an opaque identifier which uniquely distinguishes the occurence
     * of this portlet on a page.  It is used to scope portlet private
     * parameters so that no collisions occur on the URL.  See
     * {@link oracle.portal.provider.v2.render.PortletRendererUtil}
     * This should not be confused with the PortletReference class which is
     * used to identify an instance of the portlet in the Portal repository
     * for the purposes of customization.
     *
     * @return an opaque identifier which uniquely distinguishes the occurence
     * of this portlet on a page.
     */
    public String getPortletPageReference()
    {
        if (   mPortletPageReference == null
            || mPortletPageReference.equals(""))
        {
           throw new java.lang.IllegalStateException("PortletPageReference not passed in request.");
        }

        return mPortletPageReference;
    }

    /**
     * Gets a performance monitor object that will be used to record performance
     * stats.
     */
    public PerfMonitor getPerfMonitor()
    {
        return mPerfMonitor;
    }
}
