<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.ResourceBundle" %>
<%@ page import="java.util.Locale" %>
<%@ page import="oracle.portal.provider.v2.*" %>
<%@ page import="oracle.portal.provider.v2.http.*" %>
<%@ page import="oracle.portal.provider.v2.render.*" %>
<%@ page import="oracle.portal.provider.v2.url.UrlUtils" %>
<%@ page import="oracle.portal.provider.v2.render.http.*" %>
<%@ page import="oracle.portal.sample.v2.devguide.feedback.EditRenderer" %>
<%@ page import="oracle.portal.sample.v2.devguide.feedback.resource.FeedbackBundle" %>
<%@ page import="oracle.portal.log.LogManager" %>


<%
    // This portlet uses URL parameters to pass feedback submit information
    // to the edit mode renderer. The edit mode renderer has been overloaded
    // to handle both customization and feedback submit.
    // In the case of feedback submit, the data is written to the database
    // and a redirect is issued back to show mode.
    // The provider session is used to send a result string back to show mode
    // so the provider should be registered with Login Frequency set to
    // Once Per User Session.
    PortletRenderRequest prr = (PortletRenderRequest)
        request.getAttribute(HttpCommonConstants.PORTLET_RENDER_REQUEST);

    // Get parameter names to be passed on the URL to edit mode.
    String paramCustName  = PortletRendererUtil.portletParameter(prr, 
                                EditRenderer.CUSTOMER_NAME_PARAM);
    String paramCompName   = PortletRendererUtil.portletParameter(prr, 
                                EditRenderer.COMPANY_NAME_PARAM);
    String paramText       = PortletRendererUtil.portletParameter(prr, 
                                EditRenderer.FEEDBACK_TEXT_PARAM);
    String paramModeName   = PortletRendererUtil.portletParameter(prr, "_mode");
    String paramModeValue  = Integer.toString(PortletRenderer.MODE_EDIT);
    String paramTitleName  = PortletRendererUtil.portletParameter(prr,"_title");
    String paramTitleValue = "Portlet Title";

    String paramSubmitFeedbackName  = PortletRendererUtil.portletParameter(prr,
                                          EditRenderer.SUBMIT_FEEDBACK_PARAM);
    String paramSubmitFeedbackValue = EditRenderer.SUBMIT_FEEDBACK_VALUE;

    // Result string from submitting feedback is stored in the provider session.
    String feedbackResult = "";
    ProviderSession providerSession = prr.getSession();
    if (providerSession != null)
    {
      String paramName = PortletRendererUtil.portletParameter(prr, 
                              "feedbackResult");
      Object attribute = providerSession.getAttribute(paramName);
      if (attribute != null)
      {
        // Get the feedback result from the provider session.
        feedbackResult = (String)providerSession.getAttribute(paramName);

        // Reset the feedback result in the provider session.
        providerSession.setAttribute(paramName, "");
      }
    }
    else
    {
      LogManager.debug("Feedback portlet edit renderer - no session found");
    }

    //
    // Get non-customizable text.
    //
    Locale locale = prr.getPortletReference().getLocale();
    ResourceBundle bundle =
        prr.getPortletDefinition().getResourceBundle(locale);

    String greeting      = bundle.getString(FeedbackBundle.GREETING_KEY);
    String custNameLabel = bundle.getString(FeedbackBundle.CUSTNAME_KEY);
    String compNameLabel = bundle.getString(FeedbackBundle.COMPNAME_KEY);
    String provideLabel  = bundle.getString(FeedbackBundle.PROVIDE_KEY);
    String submitLabel   = bundle.getString(FeedbackBundle.SUBMIT_KEY);

%>

<p>
<%=greeting%>
</p>

<form name="<%=UrlUtils.htmlFormName(prr, null) %>" 
      action="<%= UrlUtils.htmlFormActionLink(prr, UrlUtils.DESIGN_LINK) %>"
      method="POST" >

<%= UrlUtils.htmlFormHiddenFields(prr, UrlUtils.DESIGN_LINK) %>

<%=
    // add parameters to make the portlet think it's being called in "EDIT" mode
    PortletRendererUtil.emitHiddenField(paramModeName, paramModeValue)
%>

<%= PortletRendererUtil.emitHiddenField(paramTitleName, paramTitleValue) %>

<%=
    // add a custom parameter to the URL so we can determine that the
    // submission is coming from the portlet & not from the customize page
    PortletRendererUtil.emitHiddenField(paramSubmitFeedbackName, 
        paramSubmitFeedbackValue) 
%>


<p><%= feedbackResult %></p>

<label> <%=custNameLabel%> <input type="text" size="30"
    name="<%= paramCustName %>" value=""  > </label> <br>
<label> <%=compNameLabel%> <input type="text" size="30"
    name="<%= paramCompName %>" value=""  > </label> <br>
<b><%=provideLabel%></b> <br>
<textarea cols="42" rows=4 name="<%= paramText %>" value="" wrap=virtual >
</textarea><br>
<input type=submit name="feedback_edit_action" value="<%=submitLabel%>" > 
</form>
