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

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import oracle.portal.provider.v2.http.HttpCommonConstants;
import oracle.portal.provider.v2.render.PortletRenderRequest;
import oracle.portal.provider.v2.render.RenderContext;
import oracle.portal.provider.v2.render.http.HttpPortletRendererUtil;

public class DetailServlet extends HttpServlet
{
    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
        doGet(req, res);
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
        String mode = req.getParameter(ShowServlet.DETAIL_MODE);
        if (mode != null &&
            mode.equals(ShowServlet.DETAIL_MODE_SUBMIT))
        {
            PortletRenderRequest pr =
                (PortletRenderRequest)req.getAttribute(HttpCommonConstants.PORTLET_RENDER_REQUEST);
            RenderContext renderContext = pr.getRenderContext();
            processLoginFormSubmit(req);
            HttpPortletRendererUtil.sendRedirect(pr, renderContext.getBackURL());
            return;
        }

        PrintWriter out = res.getWriter();
        if (mode != null && mode.equals("1"))
            out.println("<I>This is detail page 1</I>");
        else if (mode != null && mode.equals("2"))
            out.println("<I>This is detail page 2</I>");
        else
            out.println("<I>This is the main detail page</I>");
    }

    private void processLoginFormSubmit(HttpServletRequest req)
    {
        String username = req.getParameter("pv_username");
        String password = req.getParameter("pv_password");
        System.err.println("Username: " + username);
        System.err.println("Password: " + password);
        System.err.flush(); 
    }
}
