// Copyright (c) 2002 Oracle Corporation

package oracle.portal.sample.v2.devguide.esi;

import java.io.*;

import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;

import oracle.portal.provider.v2.personalize.PortletReference;
import oracle.portal.provider.v2.render.http.BaseManagedRenderer;
import oracle.portal.provider.v2.render.PortletRenderRequest;
import oracle.portal.provider.v2.render.http.ServletPortletRenderRequest;
import oracle.portal.provider.v2.http.HttpCommonConstants;
import oracle.portal.provider.v2.render.PortletRendererUtil;
import oracle.portal.provider.v2.render.http.HttpPortletRendererUtil;
import oracle.webdb.provider.v2.utils.http.ProviderRequestWrapper;
import oracle.portal.provider.v2.PortletException;
import oracle.portal.provider.v2.cache.invalidation.ServletInvalidationContext;
import oracle.portal.provider.v2.cache.invalidation.InvalidationException;
import oracle.portal.provider.v2.cache.invalidation.ServletInvalidationContextFactory;
import oracle.portal.sample.v2.devguide.esi.ESICustomization;
import oracle.portal.sample.v2.devguide.esi.resource.ESIBundle;
import oracle.webdb.utils.SimpleStringBuffer;

import oracle.portal.log.LogManager;

/**
 * A class that implements the sample ESI portlet's edit mode renderer.
 */
public class ESIEditRenderer extends BaseManagedRenderer
{
  private static final String URL_PARAM_TITLE      = "ptlt_title";
  private static final String URL_PARAM_INV_HOST   = "inv_host";
  private static final String URL_PARAM_INV_PREFIX = "inv_prefix";
  private static final String URL_PARAM_INV_FILE   = "inv_file";

  /**
   * Renders the portlet in edit mode.
   */
  public void renderBody(PortletRenderRequest prr) throws PortletException
  {
    try
    {
      // Check for the invalidate_cache parameter in the original request. If
      // it is found, send the invalidation request and redirect back to the
      // calling page.
      ProviderRequestWrapper prw = (ProviderRequestWrapper)prr.getAttribute
                                        (HttpCommonConstants.SERVLET_REQUEST);

      HttpServletRequest httpServletRequest = (HttpServletRequest)prr.getAttribute(HttpCommonConstants.SERVLET_REQUEST);

      // Retrieve the data object for later use.
      ESICustomization data =
          (ESICustomization)PortletRendererUtil.getEditData(prr);

      String invalidateCache = prw.getParameter("_invalidate_cache");
      if( (invalidateCache != null) && (invalidateCache.equals("true")) )
      {            
        // Invalidate cached portlet content
        ServletInvalidationContext inv = ServletInvalidationContextFactory.getServletInvalidationContext();
        LogManager.info("ServletInvalidationContext object created");

        String invalidationHost = data.getInvalidationHost(httpServletRequest);
        String invalidationPrefix = data.getInvalidationPrefix(httpServletRequest);
        String invalidationFile = data.getInvalidationFile(httpServletRequest);

        // Execute invalidation request
        int num = inv.invalidateURI(invalidationHost, invalidationPrefix, 
                      invalidationFile);
                
        SimpleStringBuffer infoMessage = new SimpleStringBuffer("Number of objects invalidated = ");
        infoMessage.append(num);
        LogManager.info(infoMessage.toString());

        // Redirect back to portlet show page
        HttpPortletRendererUtil.sendRedirect(prr, prr.getRenderContext().getBackURL());
      }
      else
      {
        // Normal customisation functions
        //
        // Retrieve the name of the parameter used to represent the user
        // action taken on the edit form. This name was assigned by
        // RenderManager but can be overridden by the portlet developer.
        String actionParam = PortletRendererUtil.getEditFormParameter(prr);
                
        // Now retrieve the value of that action parameter.
        String action = prr.getParameter(actionParam);
                
        // If the action was non null, then we have to save our newly
        // customized data. Otherwise, we have to render the edit form.
        if (action != null)
        {
          // Write the personalized values into the data object.
          data.setPortletTitle(prr.getParameter(URL_PARAM_TITLE));

          String invalidationHost = prr.getParameter(URL_PARAM_INV_HOST);
          if (invalidationHost != null && invalidationHost.length() > 0)
          {
            data.setInvalidationHost(invalidationHost);
          }

          String invalidationPrefix = prr.getParameter(URL_PARAM_INV_PREFIX);
          if (invalidationPrefix != null && invalidationPrefix.length() > 0)
          {
            data.setInvalidationPrefix(invalidationPrefix);
          }

          String invalidationFile = prr.getParameter(URL_PARAM_INV_FILE);
          if (invalidationFile != null && invalidationFile.length() > 0)
          {
            data.setInvalidationFile(invalidationFile);
          }

          // Submit data object back to the PersonalizationManager
          // for storing.
          PortletRendererUtil.submitEditData(prr, data);
        }
        else
        {                    
          // Get a writer for the edit form.
          PrintWriter out = prr.getWriter();

          String portletTitle       = data.getPortletTitle();
          String invalidationHost   = data.getInvalidationHost(httpServletRequest);
          String invalidationPrefix = data.getInvalidationPrefix(httpServletRequest);
          String invalidationFile   = data.getInvalidationFile(httpServletRequest);
          // Get non-customizable strings.
          ResourceBundle bundle = prr.getPortletDefinition().getResourceBundle(prr.getPortletReference().getLocale());
          String titleLabel =
              bundle.getString(ESIBundle.TITLE_LABEL_KEY);
          String invalidationHostLabel =
              bundle.getString(ESIBundle.INVALIDATIONHOST_LABEL_KEY);
          String invalidationPrefixLabel =
              bundle.getString(ESIBundle.INVALIDATIONPREF_LABEL_KEY);
          String invalidationFileLabel =
              bundle.getString(ESIBundle.INVALIDATIONFILE_LABEL_KEY);
          String invalidationExplanation =
              bundle.getString(ESIBundle.INV_EXPL_KEY);

          // Render the form.
          out.print("<TABLE BORDER=0 CELLSPACING=0>");
          out.print("<TR><TD>Title:</TD><TD><INPUT TYPE=\"text\" NAME=\"");
          out.print(URL_PARAM_TITLE);
          out.print("\" SIZE=\"50\" VALUE=\"");
          out.print(portletTitle);
          out.println("\" </TD></TR>");
          out.print("<TR><TD>");
          out.print(invalidationHostLabel);
          out.print("</TD><TD><INPUT TYPE=\"text\" NAME=\"");
          out.print(URL_PARAM_INV_HOST);
          out.print("\" SIZE=\"50\" VALUE=\"");
          out.print(invalidationHost);
          out.println("\" </TD></TR>");
          out.print("<TR><TD>");
          out.print(invalidationPrefixLabel);
          out.print("</TD><TD><INPUT TYPE=\"text\" NAME=\"");
          out.print(URL_PARAM_INV_PREFIX);
          out.print("\" SIZE=\"50\" VALUE=\"");
          out.print(invalidationPrefix);
          out.println("\" </TD></TR>");
          out.print("<TR><TD>");
          out.print(invalidationFileLabel);
          out.print("</TD><TD><INPUT TYPE=\"text\" NAME=\"");
          out.print(URL_PARAM_INV_FILE);
          out.print("\" SIZE=\"50\" VALUE=\"");
          out.print(invalidationFile);
          out.println("\" </TD></TR></TABLE>");
          out.println(invalidationExplanation );
        }                
      }
    }
    catch (InvalidationException ie)
    {
      LogManager.throwing("An error occurred while trying to set up and execute an invalidation request", ie);
      throw new PortletException(ie);
    }
    catch (IOException ioe)
    {
      throw new PortletException(ioe);
    }
  }
}



