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

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

import oracle.portal.provider.v1.PortletRenderRequest;
import oracle.portal.provider.v1.PortletRenderer;
import oracle.portal.provider.v1.PortletRendererUtil;
import oracle.portal.provider.v1.PortletException;
import oracle.portal.provider.v1.http.HttpProvider;
import oracle.portal.utils.v1.NameValuePair;

public class ShowServlet 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
    {
        PortletRenderRequest prr = 
            (PortletRenderRequest)req.getAttribute(HttpProvider.PORTLET_RENDER_REQUEST);

        PrintWriter out = prr.getWriter();

        // construct the form to submit to the DESIGN URL
        out.println("<form method=\"post\" action=\"" + 
                    getURLPrefix(prr, PortletRenderer.MODE_EDIT) + 
                    "\">");
                     
        out.println("<table>");
        
        // add parameters to make the portlet think it is being called in "EDIT" mode 
        out.println(emitHiddenField("_mode", 
                    new String(Integer.toString(PortletRenderer.MODE_EDIT))));
        out.println(emitHiddenField("_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(emitHiddenField("loginFormSubmit", Integer.toString(1)));

        // add all the parameters from the Design URL
        out.println(PortletRendererUtil.htmlFormHiddenFields(prr, PortletRendererUtil.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 = PortletRendererUtil.constructDetailLink(prr,params,true,false);
            out.print("<tr><td><a href=\"");
            out.print(link);
            out.println("\"> Detail Page 1 </a></td></tr>");

            params[0] = new NameValuePair("detailMode", "2");
            link = PortletRendererUtil.constructDetailLink(prr,params,true,false);
            out.print("<tr><td><a href=\"");
            out.print(link);
            out.println("\"> Detail Page 2 </a></td></tr>");

            params[0] = new NameValuePair("helpMode", "1");
            link = PortletRendererUtil.constructHelpLink(prr,params,true,false);
            out.print("<tr><td><a href=\"");
            out.print(link);
            out.println("\"> Help Page 1 </a></td></tr>");

            params[0] = new NameValuePair("helpMode", "2");
            link = PortletRendererUtil.constructHelpLink(prr,params,true,false);
            out.print("<tr><td><a href=\"");
            out.print(link);
            out.println("\"> Help Page 2 </a></td></tr>");
        }
        catch (PortletException pe)
        {
            pe.printStackTrace();
            throw new ServletException(pe.toString());
        }

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

    // utility method to construct the design URL
    private String getURLPrefix(PortletRenderRequest request, int mode)
        throws java.io.IOException
    {
        String ref = request.getDesignURL();
        
        String newRef;

        int i = ref.indexOf('?');
        if (i > 0)
        {
            // strip off the parameters - they will be added as hidden fields
            newRef = ref.substring(0, i);
        }
        else
        {
            newRef = ref;
        }
        return newRef;
    }

    private String emitHiddenFields(String queryString) 
    {
        String hidden = "";
        StringTokenizer qsST = new StringTokenizer(queryString, "&");

        while (qsST.hasMoreTokens()) 
        {
            String param = qsST.nextToken();
            int i = param.indexOf('=');
            hidden = hidden + emitHiddenField(param.substring(0, i), param.substring(i+1));
        }

        return hidden;
    }

    private String emitHiddenField(String name, String value) 
    {
        if (name == null || value == null) return "";
        StringBuffer buffer = new StringBuffer(100);
        buffer.append("<INPUT TYPE=\"hidden\" name=\"");
        buffer.append(name);
        buffer.append("\" value=\"");
        buffer.append(value);
        buffer.append("\" />");
        buffer.append((char) Character.LINE_SEPARATOR);
        return  buffer.toString();
    }

}
