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

import java.io.*;

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.log.LogManager;

public class InvalidationEditRenderer extends BaseManagedRenderer
{

    public void renderBody(PortletRenderRequest pr) throws PortletException
    {
        // 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)pr.getAttribute
                                        (HttpCommonConstants.SERVLET_REQUEST);

        String invalidateCache = prw.getParameter("_invalidate_cache");
        try
        {
            if( (invalidateCache != null) && (invalidateCache.equals("true")) )
            {            
                // Invalidate cached portlet content
                ServletInvalidationContext inv = 
                    ServletInvalidationContextFactory.getServletInvalidationContext();
                LogManager.info("ServletInvalidationContext object created");
                // Set the full provider path and character set for
                // character encoding
                inv.setFullProviderPath((ServletPortletRenderRequest)pr);
                // Set a portlet instance as an invalidation key
                inv.setPortletReference(pr.getPortletReference());
                // Execute invalidation request
                int num = inv.invalidate();
                
                LogManager.info("Number of objects invalidated = " + num);

                // Redirect back to portlet show page
                HttpPortletRendererUtil.sendRedirect(pr,
                                           pr.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(pr);
                
                // Now retrieve the value of that action parameter.
                String action = pr.getParameter(actionParam);

                // Retrieve the data object for later use.
                InvalidationPersonalizationObject data =
                    (InvalidationPersonalizationObject)PortletRendererUtil.getEditData(pr);
                
                // 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(pr.getParameter("mfp_title"));

                    // Submit data object back to the PersonalizationManager
                    // for storing.
                    PortletRendererUtil.submitEditData(pr, data);
                }
                else
                {                    
                    // Get a writer for the edit form.
                    PrintWriter out = pr.getWriter();
                    
                    // Render the form.
                    out.println("<TABLE BORDER=0 CELLSPACING=0>");
                    out.println("<TR><TD>Title:</TD>");
                    out.print("<TD><INPUT TYPE=\"text\" NAME=\"mfp_title\" " +
                              "SIZE=\"50\" VALUE=\"" + data.getPortletTitle() +
                              "\" </TD></TR>");
                }                
            }
        }
        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);
        }
    }
}

