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 actionParam = PortletRendererUtil.getEditFormParameter(pr);
        String action = pr.getParameter(actionParam);
        
        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 renderEditDefaults(PortletRenderRequest pr)
    {
        renderEdit(pr);
    }

}
