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

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

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

public class EditServlet 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 pr;
        pr = (PortletRenderRequest)req.getAttribute(HttpCommonConstants.PORTLET_RENDER_REQUEST);
        RenderContext renderContext = pr.getRenderContext();

        String qualSubmitParam =
            PortletRendererUtil.portletParameter(pr, ShowServlet.SUBMIT_ACTION);
        String loginFormSubmit = req.getParameter(qualSubmitParam);
        String actionParam = PortletRendererUtil.getEditFormParameter(pr);
        String action = pr.getParameter(actionParam);

        if (loginFormSubmit != null &&
            loginFormSubmit.equals(ShowServlet.SUBMIT_ACTION_VALUE))
        {
            processLoginFormSubmit(pr);
            HttpPortletRendererUtil.sendRedirect(pr, renderContext.getBackURL());
            return;
        }
        else
        {
        
            switch(pr.getMode())
            {
            case PortletRenderer.MODE_EDIT:
                renderEdit(pr);
                break;
            case PortletRenderer.MODE_EDIT_DEFAULTS:
                renderEditDefaults(pr);
                break;
            default:
                throw new ServletException("Incorrect mode supplied!!");
            }

            // redirect to the appropriate page based on the button that was clicked
            if (action != null && action.equalsIgnoreCase("OK")) 
            {
                HttpPortletRendererUtil.sendRedirect(pr, renderContext.getBackURL());
                return;
            }
            else if (action != null && action.equalsIgnoreCase("APPLY")) 
            {
                HttpPortletRendererUtil.sendRedirect(pr, renderContext.getPageURL());
                return;
            }
        }
    }

    private void renderEdit(PortletRenderRequest pr)
    {
        try
        {
            PrintWriter out = pr.getWriter();
            String actionParam = PortletRendererUtil.getEditFormParameter(pr);
            String action = pr.getParameter(actionParam);
            ProviderSession session = pr.getSession();
            ServletPersonalization data = (ServletPersonalization)PortletRendererUtil.getEditData(pr);
            if (action != null)
            {
                data.setPortletTitle(pr.getParameter("hw_title"));
                PortletRendererUtil.submitEditData(pr, data);

                // write into session indication that cache is now invalid
                if(session != null)
                {
                    session.setAttribute(PortletRendererUtil.portletParameter(pr, "hasChanged"), new Boolean(true));
                }
            }
            else
            {
                out.println("<table BORDER CELLSPACING=0>");
                out.println("<tr><td>Title:</td><td><INPUT TYPE=\"text\" "+
                            "SIZE=\"40\" MAXLENGTH=\"60\" NAME=\"hw_title" + "\" VALUE=\""
                            + data.getPortletTitle() + "\" /></td></tr>");
            }
        }
        catch (Exception e)
        {
        }

    }

    private void processLoginFormSubmit(PortletRenderRequest 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(); 
    }

    private void renderEditDefaults(PortletRenderRequest pr)
    {
        renderEdit(pr);
    }

}
