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.v2.NameValuePair;

public class ShowServlet extends HttpServlet
{
    public static final String SUBMIT_ACTION = "loginFormSubmit";
    public static final String SUBMIT_ACTION_VALUE = "1";

    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 parameters to make the portlet think it is being called in "EDIT" mode 
        out.println(PortletRendererUtil.emitHiddenField(PortletRendererUtil.portletParameter(prr, "_mode"),
                    new String(Integer.toString(PortletRenderer.MODE_EDIT))));
        out.println(PortletRendererUtil.emitHiddenField(PortletRendererUtil.portletParameter(prr, "_title"),
                    "Portlet Title"));

        // add a custom parameter to the URL so we can determine that the
        // submission is coming from the portlet & not from the customize page
        out.println(PortletRendererUtil.emitHiddenField(PortletRendererUtil.portletParameter(prr, SUBMIT_ACTION),
                    SUBMIT_ACTION_VALUE));

        // 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
        {
            NameValuePair[] params = new NameValuePair[1];
            params[0] = new NameValuePair("detailMode", "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 NameValuePair("detailMode", "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 NameValuePair("helpMode", "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 NameValuePair("helpMode", "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>");     
    }

}
