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

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

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

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);
        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!!");
        }
    }

    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"));
                data.putString(ServletPersonalization.GREETING,
                               pr.getParameter(ServletPersonalization.GREETING));

                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=\"50\" MAXLENGTH=\"60\" NAME=\"hw_title" + 
                            "\" VALUE=\"" + data.getPortletTitle() + 
                            "\" /></td></tr>");
                out.println("<TR><TD>Greeting:</TD>");
                out.print("<TD><INPUT TYPE=\"text\" NAME=\"" +
                          ServletPersonalization.GREETING + "\"  SIZE=\"50\" "+ 
                          "VALUE=\"");
                out.print(data.getString(ServletPersonalization.GREETING) +
                          "\" </TD></TR>");
                out.println("</TABLE>");
            }
        }
        catch (Exception e)
        {
        }

    }

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

}
