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

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

import oracle.portal.provider.v2.render.PortletRenderRequest;
import oracle.portal.provider.v2.render.PortletRenderer;
import oracle.portal.provider.v2.render.PortletRendererUtil;
import oracle.portal.provider.v2.url.UrlUtils;
import oracle.portal.provider.v2.PortletException;
import oracle.portal.provider.v2.http.HttpCommonConstants;
import oracle.portal.utils.NameValue;

public class ShowServlet extends HttpServlet
{
    public static final String DETAIL_MODE = "detailMode";
    public static final String DETAIL_MODE_SUBMIT = "submit";
    public static final String HELP_MODE = "helpMode";

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

    public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
        PortletRenderRequest prr = 
            (PortletRenderRequest)req.getAttribute(HttpCommonConstants.PORTLET_RENDER_REQUEST);

        PrintWriter out = prr.getWriter();

        // construct the form to submit to the DESIGN URL
        out.println("<form method=\"post\" action=\"" + 
                    UrlUtils.htmlFormActionLink(prr, UrlUtils.DESIGN_LINK) +
                    "\" name=\"" +
                    UrlUtils.htmlFormName(prr,null) +
                    "\">");
                     
        out.println("<table>");
        
        // add parameter to make the portlet think it is being called in "SHOW_DETAILS" mode 
        out.println(PortletRendererUtil.emitHiddenField(
            "_mode",
            Integer.toString(PortletRenderer.MODE_SHOW_DETAILS)));

        // add a custom parameter to the URL so we can determine that the
        // the full page request is a data submission and not a request for content
        out.println(PortletRendererUtil.emitHiddenField(DETAIL_MODE, DETAIL_MODE_SUBMIT));

        // add all the parameters from the Design URL
        out.println(UrlUtils.htmlFormHiddenFields(prr, UrlUtils.DESIGN_LINK));

        // add the displayed displayed username/password fields
        out.println("<tr><td>Username: </td><td><input type=text name=pv_username></td></tr>"); 
        out.println("<tr><td>Password: </td><td><input type=password name=pv_password></td></tr>"); 
        out.println("<tr><td colspan=2><input type=submit></td></tr>");

        try
        {
            NameValue[] params = new NameValue[1];
            params[0] = new NameValue(DETAIL_MODE, "1");
            String link = UrlUtils.constructDetailHTMLLink(
                                       prr,params," Detail Page 1 ",
                                       "",true,false);
            out.print("<tr><td>");
            out.print(link);
            out.println("</td></tr>");

            params[0] = new NameValue(DETAIL_MODE, "2");
            link = UrlUtils.constructDetailHTMLLink(
                                     prr,params," Detail Page 2 ","",true,false);
            out.print("<tr><td>");
            out.print(link);
            out.println("</td></tr>");

            params[0] = new NameValue(HELP_MODE, "1");
            link = UrlUtils.constructHelpHTMLLink(
                                      prr,params," Help Page 1 ", "",true,false);
            out.print("<tr><td>");
            out.print(link);
            out.println("</td></tr>");

            params[0] = new NameValue(HELP_MODE, "2");
            link = UrlUtils.constructHelpHTMLLink(
                                      prr,params," Help Page 2 ","",true,false);
            out.print("<tr><td>");
            out.print(link);
            out.println("</td></tr>");
        }
        catch (PortletException pe)
        {
            pe.printStackTrace();
            throw new ServletException(pe.toString());
        }

        out.println("</table>");
        out.println("</form>");     
    }

}
