/* Copyright (c) 2001, 2002 Oracle Corporation. All Rights Reserved. */
package oracle.portal.sample.v2.devguide.partnerApp;

import java.io.IOException;

import oracle.portal.provider.v2.PortletException;
import oracle.portal.provider.v2.render.PortletRenderRequest;
import oracle.portal.provider.v2.render.http.BaseManagedRenderer;
import oracle.portal.utils.xml.v2.NodeHandlerException;

/**
 * Acts as the portlet entry point to the Fancy Flights partner
 * application.
 */
public class FlightRenderer extends BaseManagedRenderer
{
    // Holds the url mapping to the Flights servlet. Needed because in a
    // portlet context, getServletPath() returns the path to the JPDK servlet.
    private String mFlightServletPath = null;
    
    // Fancy Flights application object which does all the work.
    private FlightDispatch mFlightDispatch = null;

    /**
     * Sets the context relative path to the Fancy Flights servlet. This is
     * required to build up deep links in the Flights application.
     *
     * @param path path to the Fancy Flights servlet.
     */
    public void setFlightServletPath(String path)
    {
        mFlightServletPath = path;
    }

    /**
     * Checks that this <code>FlightRenderer</code> is configured with the
     * Fancy Flights servlet path and then instantiates a {@link
     * FlightDispatch} object.
     *
     * @exception {@link oracle.portal.utils.xml.v2.NodeHandlerException} if
     * this <code>FlightRenderer</code> is not configured properly.
     */
    public void postInitialize() throws NodeHandlerException
    {
        super.postInitialize();
        if (mFlightServletPath == null)
        {
            throw new NodeHandlerException("A FlightRenderer must be " +
                                           "configured with the  Flights " +
                                           "Servlet path.");
        }
        mFlightDispatch = new FlightDispatch(mFlightServletPath);
    }

    /**
     * Delegates to a Fancy Flights application object, {@link FlightDispatch},
     * to perform the rendition.
     *
     * @param pr the {@link
     * oracle.portal.provider.v2.render.PortletRenderRequest} for this
     * rendition.
     */
    public void renderBody(PortletRenderRequest pr) throws PortletException
    {
        // If we get to this stage we are guaranteed to have a valid
        // FlightDispatch instance, so just invoke it to perform the rendition.
        try
        {
            mFlightDispatch.process(pr);
        }
        catch(IOException ioe)
        {
            throw new PortletException(ioe);
        }
    }
}
