// Copyright (c) 2001 Oracle Corporation
package oracle.portal.provider.v1.http;

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

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

import oracle.portal.provider.v1.*;

/**
 * The PageRenderer implements a bridge from the JPDK PortletRenderer model to
 * JSPs and static files.  The PortletRenderer interface specifies the API used
 * by the JPDK to ask a portlet to render itself.  Rather than implement this
 * interface specifically, many developers may find it more convenient to
 * express a rendering as a JSP or a static (HTML) file.  The PageRenderer
 * provides the bridge between the JPDK API and these methods of expressing a
 * rendition.<p> The PageRenderer allows the developer to specify a particular
 * JSP or static file used for each of the seven render modes.  When a render
 * request is recieved, the Page Renderer intantiates and delegates to either
 * {@link JspRenderer} or {@link FileRenderer}. It distinguishes between the
 * two based on the pages's extension.  Registered pages with the extension
 * ".jsp" or ".sqljsp" are passed to {@link JspRenderer}. Otherwise the page is passed
 * to {@link FileRenderer}.<p>The PageRenderer is designed to be used in conjunction
 * with the {@link DefaultProvider}. That is it can be conveniently configured in the
 * provider.xml descriptor file. It utilizes the provider.xml extensibility
 * mechanism that pushes values to it based on matching tag names to accessor
 * methods.  For example, the tag <showPage> corresponds to the accessor
 * setShowPage();
 *
 * @deprecated <code>PageRenderer</code> and its subclasses have been replaced by
 * the more flexible {@link RenderManager}.
 */
public class PageRenderer extends RenderManager
{

    /**
     * Null Constructor
     */
    public PageRenderer()
    {
        super();
        // PageRenderers by default do not render containers for customize mode
        setRenderCustomize(false);
    }

    /**
     *  Indicates whether there is a currently registered page for the requests
     *  current render mode.
     *
     *  @param pr the request object containing the current request details.
     *  @return <code>true</code> if there is a page for this render mode,
     *  <code>false</code> otherwise.
     */
    public boolean isPage(PortletRenderRequest pr)
    {
        try
        {
            ManagedRenderer mr = getManagedRenderer(pr);
            return mr != null;
        }
        catch (PortletException pe)
        {
            return false;
        }
    }

    /**
     * Renders the appropriate header for the required mode. Overrides
     * RenderManager method by calling {@link
     * #renderPortletHeader(PortletRenderRequest,boolean,boolean)} for
     * PageRenderer specific behaviour.
     *
     * @param pr a PortletRenderRequest
     */
    public void renderPortletHeader(PortletRenderRequest pr)
        throws PortletException, IOException, AccessControlException
    {
        boolean isJSP = false;
        try
        {
            isJSP = (getManagedRenderer(pr) instanceof JspRenderer);
        }
        catch (PortletException pe)
        {
        }
        renderPortletHeader(pr, isJSP, true);
    }
        

    /**
     * Renders the appropriate header for the required mode. Provided for
     * backward compatibility with old PageRenderer interface.
     *
     * @param pr a PortletRenderRequest
     * @param isJSP boolean flag to indicate if this is a JSP mode.
     * @param renderContainer boolean flag to indicate whether portlet headers
     * and footers should be rendered.
     */
    public void renderPortletHeader(PortletRenderRequest pr, boolean isJSP,
                                       boolean renderContainer)
        throws PortletException, IOException, AccessControlException
    {
        if (!renderContainer)
        {
            return;
        }
        // PageRenderer never used to render headers and footers for Jsps in
        // HELP and ABOUT modes, so we will have to simulate this
        if ( isJSP && (pr.getMode() == PortletRenderer.MODE_ABOUT ||
                       pr.getMode() == PortletRenderer.MODE_HELP))
        {
            return;
        }
        super.renderPortletHeader(pr);
    }

    /**
     * Renders the appropriate footer for the required mode. Overrides
     * RenderManager method by calling {@link
     * #renderPortletFooter(PortletRenderRequest,boolean,boolean)} for
     * PageRenderer specific behaviour.
     *
     * @param pr a PortletRenderRequest
     */
    public void renderPortletFooter(PortletRenderRequest pr)
        throws PortletException, IOException, AccessControlException
    {
        boolean isJSP = false;
        try
        {
            isJSP = (getManagedRenderer(pr) instanceof JspRenderer);
        }
        catch (PortletException pe)
        {
        }
        renderPortletFooter(pr, isJSP, true);
    }

    /**
     * Renders the appropriate footer for the required mode. Provided for
     * backward compatibility with old PageRenderer interface.
     *
     * @param pr a PortletRenderRequest
     * @param isJSP boolean flag to indicate if this is a JSP mode.
     * @param renderContainer boolean flag to indicate whether portlet headers
     * and footers should be rendered.
     */
    public void renderPortletFooter(PortletRenderRequest pr, boolean isJSP,
                                       boolean renderContainer)
        throws PortletException, IOException, AccessControlException
    {
        if (!renderContainer)
        {
            return;
        }
        // PageRenderer never used to render headers and footers for Jsps in
        // HELP and ABOUT modes, so we will have to simulate this
        if ( isJSP && (pr.getMode() == PortletRenderer.MODE_ABOUT ||
                       pr.getMode() == PortletRenderer.MODE_HELP))
        {
            return;
        }
        super.renderPortletFooter(pr);
    }

    protected String getTitle(PortletRenderRequest pr)
        throws AccessControlException
    {
        return PortletRendererUtil.getTitle(pr);      
    }
}

 
