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

package oracle.portal.provider.v2.render;

import java.io.PrintWriter;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.Locale;
import java.util.Map;


import oracle.portal.provider.v2.PortletDefinition;
import oracle.portal.provider.v2.resource.ProviderBundle;
import oracle.portal.provider.v2.security.AccessControlException;
import oracle.portal.utils.v2.ParameterMap;
import oracle.portal.utils.v2.SimpleParameterMap;
import oracle.webdb.utils.SimpleStringBuffer;
import oracle.portal.log.LogManager;
import oracle.portal.utils.BiDiUtils;

/**
 *  A PortletContainerRenderer is a utility class that renders the container
 *  for a portlet.
 *
 *  The container that is renderered may vary depending on the SHOW mode that
 *  is being requested. For example, Edit, Edit Defaults. Help, About, and
 *  Show Details are all rendered in "full screen" mode whereas the regular
 *  Show and Preview modes have variable size containers depending on the
 *  size of the portlet.
 */

public class MocContainerRenderer extends DefaultContainerRenderer
{

    /**
     *  Renders a portlet's titlebar. Portlets are responsible for rendering
     *  their container.
     *
     *  This method renders an MOC (my.oracle.com) style portlet titlebar using
     *  information from the request and Portlet meta-data. Typically,
     *  clients will not use this method.  Rather they wil rely on
     *  renderPortletHeader() to render the entire container including the
     *  titlebar.  If however, renderPortletHeader is not used, this method is
     *  provided for convenience.
     *
     *  @param request the portlet render request
     *  @param title the label that should be displayed as the title.  if <b>null</b> then
     *  the method will render the static title retrieved via Portlet.getTitle();
     */
    protected void renderPortletTitlebar(PortletRenderRequest request,
                                         PrintWriter out,
                                         Map params)
        throws IOException
    {
        int renderMode = request.getMode();
        PortletDefinition pd = request.getPortletInstance().getPortletDefinition();;
        String title = (String)params.get(PortletContainerRenderer.PORTLET_TITLE);
        if (title == null)
        {
            try
            {
                title = PortletRendererUtil.getTitle(request);
            }
            catch (AccessControlException ace)
            {
                // log the original exception because we cannot re-throw it
                LogManager.severe("Failed to get portlet title", ace);
                throw new IOException("AccessControlException when trying to get Portlet title");
            }
        }

        out.print("<TABLE BORDER=\"0\" class=\"");
        out.print(PortletRenderer.PORTLET_HEADER_COLOR);
        out.println("\" CELLSPACING=\"0\" CELLPADDING=\"0\" WIDTH=\"100%\">");

        out.print("<TR class=\"");
        out.print(PortletRenderer.PORTLET_HEADER_COLOR);
        out.println("\">");

        out.print("<TD class=\"");
        out.print(PortletRenderer.LEFT_CURVE);
        out.print("\" valign=\"top\" width=\"10\" ");
        out.println("height=\"10\" NOWRAP>&nbsp;&nbsp;&nbsp;</TD>");

        if (pd.hasShowDetails() && !(renderMode == PortletRenderer.MODE_PREVIEW))
        {
            renderTitle(request, title, out, pd,
                        getAnchorRef(request, pd, PortletRenderer.MODE_SHOW_DETAILS, title));
        }
        else
        {
            renderTitle(request, title, out, pd, null);
        }

        out.print("<TD class=\"");
        out.print(PortletRenderer.RIGHT_CURVE);
        out.println("\" width=\"10\" NOWRAP>&nbsp;&nbsp;</TD>");

        if (renderMode == PortletRenderer.MODE_PREVIEW)
        {
            renderDummyActions(out, pd, request);
        }
        else
        {
            renderActions(out, pd, request, title);
        }

        out.println("</TD></TR></TABLE>");
    }



    protected void renderTitle(PortletRenderRequest request,
                                    String title,
                                    PrintWriter out,
                                    PortletDefinition pd,
                                    String href)
        throws java.io.IOException
    {
        String endAnchor;
        if (href == null) {
            href = "";
            endAnchor = "";
        } else {
            endAnchor = "</a>";
        }

        out.print("<TD class=\"");
        out.print(PortletRenderer.PORTLET_HEADER_COLOR);
        out.print("\" NOWRAP=\"\" valign=\"top\">");
        out.print(href);
        out.print("<font class=\"");
        out.print(PortletRenderer.PORTLET_HEADER_TEXT);
        out.print("\">");
        out.print(title);
        out.print(" </font>");
        out.print(endAnchor);
        out.println("&nbsp;</TD>");
    }

    protected void renderActions(PrintWriter out,
                                 PortletDefinition pd,
                                 PortletRenderRequest request,
                                 String title)
        throws java.io.IOException
    {
       	Locale l = request.getLocale();
        out.print("<TD class=\"");
        out.print(PortletRenderer.PAGE_COLOR);
       	out.print("\" align=\"");
       	out.print(BiDiUtils.getAlignment(l, BiDiUtils.RIGHT));
        out.println("\" NOWRAP=\"\" valign=\"top\" width=\"100%\">&nbsp;");

        ResourceBundle res = request.getProviderResource();

        if (pd.hasShowEdit())
        {
            boolean showCustomize = false;
            int userAuthLevel = request.getUser().getAuthenticationLevel();
            showCustomize = (userAuthLevel >= pd.getMinEditAuthLevel());

            if (showCustomize)
            {
                renderAction(out, pd, request, PortletRenderer.MODE_EDIT, res.getString(ProviderBundle.CUSTOMIZE_LABEL), title);
            }
        }

        if (pd.hasHelp())
        {
            renderAction(out, pd, request, PortletRenderer.MODE_HELP,
                         res.getString(ProviderBundle.HELP_LABEL), title);
        }


        if (pd.hasAbout())
        {
            renderAction(out, pd, request, PortletRenderer.MODE_ABOUT,
                         res.getString(ProviderBundle.ABOUT_LABEL), title);
        }
    }

    protected void renderDummyActions(PrintWriter out,
                                      PortletDefinition pd,
                                      PortletRenderRequest request)
        throws java.io.IOException
    {
        out.print("<TD class=\"");
        out.print(PortletRenderer.PAGE_COLOR);
        out.println("\" NOWRAP=\"\" valign=\"top\" width=\"100%\">&nbsp;");

        ResourceBundle res = request.getProviderResource();

        if (pd.hasShowEdit()) {
            renderDummyAction(out, res.getString(ProviderBundle.CUSTOMIZE_LABEL));
        }

        if (pd.hasHelp()) {
            renderDummyAction(out, res.getString(ProviderBundle.HELP_LABEL));
        }

        if (pd.hasAbout()) {
            renderDummyAction(out, res.getString(ProviderBundle.ABOUT_LABEL));
        }
    }



//////////////////////////////////////////////////////////////////////////////
//
//                           DEPRECATED METHODS
//
//////////////////////////////////////////////////////////////////////////////



    /**
     *  Renders a portlet's titlebar. Portlets are responsible for rendering
     *  their container.
     *
     *  This method renders an MOC (my.oracle.com) style portlet titlebar using
     *  information from the request and Portlet meta-data. Typically,
     *  clients will not use this method.  Rather they wil rely on
     *  renderPortletHeader() to render the entire container including the
     *  titlebar.  If however, renderPortletHeader is not used, this method is
     *  provided for convenience.
     *
     *  @param request the portlet render request
     *  @param title the label that should be displayed as the title.  if <b>null</b> then
     *  the method will render the static title retrieved via Portlet.getTitle();
     *
     *  @deprecated use {@link #renderPortletTitlebar(PortletRenderRequest,
     *                                                PrintWriter,
     *                                                Map)}
     */
    protected void renderPortletTitlebar(PortletRenderRequest request,
                                         PrintWriter out,
                                         ParameterMap params)
        throws IOException
    {
        renderPortletTitlebar(request, out, convertParameterMap(params));
    }
}


