<%
/* Copyright (c) 2002, 2003 Oracle Corporation. All Rights Reserved. */
%>

<%@ taglib uri="http://xmlns.oracle.com/uix/ui" prefix="uix" %>

<%@ page import="java.util.Locale,
           oracle.cabo.share.config.Configuration,
           oracle.cabo.share.nls.LocaleContext,
           oracle.cabo.ui.ServletRenderingContext,
           oracle.webdb.utils.SimpleStringBuffer,
           oracle.portal.log.LogManager,
           oracle.webdb.utils.HTTPUtils,
           oracle.webdb.reformlet.Utility,
           oracle.webdb.providerui.RequestContext,
           oracle.webdb.providerui.ResourceService,
           oracle.webdb.providerui.ProviderBuilderConstants,
           oracle.webdb.providerui.event.ProviderConstants,
           oracle.webdb.providerui.builder.BuilderFactory,
           oracle.webdb.providerui.resource.ProviderUIBundle,
           oracle.webdb.providerui.builder.base.ProviderBuilder"
           session="true" %>

<%! private static final String VALUE_NO = "no";
%>
<%
  RequestContext reqContext = 
                      BuilderFactory.getRequestContext();
  Locale locale = reqContext.getLocale();
%>

<%@ include file="ProviderBuilderCaboSetup.jsp" %>

<%
  int iColumnLong  = 30;  // for server host, http proxy host, no proxy for domains begining with
  int iColumnShort = 8;   // all the rest
%>

<%
  ProviderBuilder providerBuilder = 
    BuilderFactory.
      getProviderBuilder(ProviderBuilderConstants.WEBCLIPPING_PROVIDER_BUILDER);
  String resourceBundleClass = providerBuilder.getResourceBundleClass();
  ResourceService rs =
    BuilderFactory.getResourceService(resourceBundleClass, locale);
 
  String strHeader1Text = rs.getMessage(ProviderUIBundle.EDIT_WEBCLIPPING_PROVIDER_STEP1_TITLE);
  
  String strHeader1Explanation = 
                  rs.getMessage(ProviderUIBundle.EDIT_WEBCLIPPING_PROVIDER_STEP1_DESCRIPTION);
  
  // Location (URL) of the provider  
  String strLocationLabel = rs.getMessage(ProviderUIBundle.PROVIDER_LOCATION);

  // Host & Port of the proxy
  String strProxyTitle = rs.getMessage(ProviderUIBundle.PROXY_TITLE);
  String strProxyExplanation = 
                  rs.getMessage(ProviderUIBundle.WEBCLIPPING_PROXY_DESCRIPTION);

  String strProxyHostLabel = rs.getMessage(ProviderUIBundle.PROXY_HOST);
  String strProxyPortLabel = rs.getMessage(ProviderUIBundle.PROXY_PORT);
  String strProxyDontLabel = rs.getMessage(ProviderUIBundle.DONT_PROXY_FOR);

  // Repository connection information
  String strReposTitle       = rs.getMessage(ProviderUIBundle.REPOS_TITLE);
  String strReposDescription =
       rs.getMessage(ProviderUIBundle.REPOS_DESCRIPTION);

  String strReposNote        = rs.getMessage(ProviderUIBundle.REPOS_NOTE);
  String strReposTip         = rs.getMessage(ProviderUIBundle.REPOS_TIP);
  String strReposHostLabel   = rs.getMessage(ProviderUIBundle.REPOS_HOST);
  String strReposPortLabel   = rs.getMessage(ProviderUIBundle.REPOS_PORT);
  String strReposSidLabel    = rs.getMessage(ProviderUIBundle.REPOS_SID);
  String strReposUserLabel   = rs.getMessage(ProviderUIBundle.REPOS_USERNAME);
  String strReposPassLabel   = rs.getMessage(ProviderUIBundle.REPOS_PASSWORD);

  // Use RAA or not
  String strUseRaaLabel = rs.getMessage(ProviderUIBundle.WEBCLIPPING_RAA);
  String strUseRaaExplanation = 
	rs.getMessage(ProviderUIBundle.WEBCLIPPING_RAA_DESCRIPTION);

  // Use ASO or not
  String strUseAsoLabel = rs.getMessage(ProviderUIBundle.WEBCLIPPING_ASO);
  String strUseAsoExplanation = 
	rs.getMessage(ProviderUIBundle.WEBCLIPPING_ASO_DESCRIPTION);

  // Value of the Location of the Provider
  String locationValue = 
   reqContext.getParameter(ProviderConstants.PROVIDER_URL) == null ? "" : 
    (String) reqContext.getParameter(ProviderConstants.PROVIDER_URL);
  
  // Value of the Proxy Host
  String strProxyHostVal = 
    reqContext.getParameter(ProviderConstants.PROXY_HOST) == null ? "" : 
    (String) reqContext.getParameter(ProviderConstants.PROXY_HOST);
  
  // Value of the Proxy Port
  String strProxyPortVal = 
    reqContext.getParameter(ProviderConstants.PROXY_PORT) == null ? "" : 
    (String) reqContext.getParameter(ProviderConstants.PROXY_PORT);

  // Value of the Proxy Dont
  String strProxyDontVal = 
    reqContext.getParameter(ProviderConstants.DONT_PROXY_FOR) == null ? "" : 
    (String) reqContext.getParameter(ProviderConstants.DONT_PROXY_FOR);

  // Values of the repository connection information
  String strReposHostVal =  
    reqContext.getParameter(ProviderConstants.REPOS_HOST) == null ? "" :
    (String) reqContext.getParameter(ProviderConstants.REPOS_HOST);
  String strReposPortVal =  
    reqContext.getParameter(ProviderConstants.REPOS_PORT) == null ? "" :
    (String) reqContext.getParameter(ProviderConstants.REPOS_PORT);
  String strReposSidVal  =  
    reqContext.getParameter(ProviderConstants.REPOS_SID) == null ? "" :
    (String) reqContext.getParameter(ProviderConstants.REPOS_SID);
  String strReposUserVal =  
    reqContext.getParameter(ProviderConstants.REPOS_USER) == null ? "" :
    (String) reqContext.getParameter(ProviderConstants.REPOS_USER);
  String strReposPassVal =  
    reqContext.getParameter(ProviderConstants.REPOS_PASS) == null ? "" :
    (String) reqContext.getParameter(ProviderConstants.REPOS_PASS);

  // Value of use ASO      
  String strUseAsoVal = 
    reqContext.getParameter(ProviderConstants.USE_ASO) == null ? "":
    (String) reqContext.getParameter(ProviderConstants.USE_ASO);

  String strAsoEnabledExplanation  = 
	rs.getMessage(ProviderUIBundle.WEBCLIPPING_ENABLE_ASO);
  String strAsoDisabledExplanation = 
	rs.getMessage(ProviderUIBundle.WEBCLIPPING_DISABLE_ASO);
  String strAsoSelected = strUseAsoVal.equals("true") ? "1":"0";

  // Value of use RAA (no such parameter implies false)
  String strUseRaaVal = 
    reqContext.getParameter(ProviderConstants.USE_RAA) == null ? "false":
    (String) reqContext.getParameter(ProviderConstants.USE_RAA);

  String strRaaEnabledExplanation  = 
    rs.getMessage(ProviderUIBundle.WEBCLIPPING_ENABLE_RAA);
  String strRaaDisabledExplanation = 
    rs.getMessage(ProviderUIBundle.WEBCLIPPING_DISABLE_RAA);

  String strRaaSelected = strUseRaaVal.equals("true") ? "0":"1";

  String strReposTargetTest =  
    reqContext.getParameter(ProviderConstants.REPOS_TARGET) == null ? "" :
    (String) reqContext.getParameter(ProviderConstants.REPOS_TARGET);

  //The next set of instructions provide a hook to call additional processing URL (Intermediate servlet). This additional processing is required as 
  //OmniPortlet's provider.xml needs to be updated if WebClipping repository config has changed and new vaultID (used by security is generated.
  String contextURL = reqContext.getContextURL();
  
  SimpleStringBuffer additionalActionURLBuf = new SimpleStringBuffer(contextURL);
  additionalActionURLBuf.append("providerui/ProviderAdditionalSettingsServlet");
  reqContext.setParameter(ProviderConstants.ADDITIONAL_ACTION_URL,
                          additionalActionURLBuf.toString(), 
                          RequestContext.SESSION_SCOPE);
  
  SimpleStringBuffer currentURLBuf = new SimpleStringBuffer(contextURL);
  currentURLBuf.append("providerui/EditProvider?");
  //when the page is called from WizardStep.fetchSource, it's a dispatcher.include.
  //with this, the queryString param is lost. Hence store the queryStirng to be used later to
  // reconstruct the entire currentURL.
  if (request.getQueryString() != null)
  {
    currentURLBuf.append(request.getQueryString());
    reqContext.setParameter(ProviderConstants.QUERY_STRING_PARAM, request.getQueryString(), RequestContext.SESSION_SCOPE);
  }
  else 
  {
    currentURLBuf.append((String) reqContext.getParameter(ProviderConstants.QUERY_STRING_PARAM, RequestContext.SESSION_SCOPE));
  }
  
  reqContext.setParameter(ProviderConstants.CURRENT_URL, currentURLBuf.toString(), RequestContext.SESSION_SCOPE);
  
  boolean showProxySection = true;
  String strShowProxySection = (String) reqContext.getParameter(ProviderConstants.SHOW_HTTP_PROXY_SECTION);
  if ((strShowProxySection != null) && (strShowProxySection.trim().equalsIgnoreCase(VALUE_NO)) )
  {
    reqContext.releaseParameter(ProviderConstants.SHOW_HTTP_PROXY_SECTION, RequestContext.REQUEST_SCOPE);

    // add a param in reqContext so that call differentiation may be made as to original 
    //introducer of the current request. Note that the value being sent here is OP - indicating
    //OmniPortlet and not WC as call will come in this clock only if originated from OP test page.
    reqContext.setParameter(ProviderConstants.ORIGINATING_PROVIDER_PARAM, 
                            ProviderConstants.OP_ORIGINATING_PROVIDER, 
                            RequestContext.SESSION_SCOPE);
%>
    <input type="hidden" name="<%= ProviderConstants.SHOW_HTTP_PROXY_SECTION %>" value="<%= VALUE_NO%>">
<%
    showProxySection = false;
  }
%>

<%-- javascript (For UIX 2.1, we can use uix script element) --%>

<script type="text/javascript">

  function checkRaa(p_optionRaa) 
  {
    if (p_optionRaa.value == 'true') {
      // reset the values
      document.EditProviderForm.<%=ProviderConstants.REPOS_HOST%>.value='<%=strReposHostVal%>';
      document.EditProviderForm.<%=ProviderConstants.REPOS_PORT%>.value='<%=strReposPortVal%>';
      document.EditProviderForm.<%=ProviderConstants.REPOS_SID%>.value='<%=strReposSidVal%>';
      document.EditProviderForm.<%=ProviderConstants.REPOS_USER%>.value='<%=strReposUserVal%>';
      document.EditProviderForm.<%=ProviderConstants.REPOS_PASS %>.value='<%=strReposPassVal%>';
    }
    submitForm('EditProviderForm', 1, {'<%=ProviderConstants.REPOS_TARGET%>' : 'true'});
  }
</script>

  <uix:formValue name="<%=ProviderConstants.REPOS_TARGET%>" value="false" />

  <%-- Overall table layout --%>
  <uix:tableLayout width="100%">
    <uix:contents>

    <%
    //showProxySection is true only if call came from OP test page side. 
    //if it did, don't show WebClipping Provider URL as it might confuse the user.
    if (showProxySection)
    {
    %>
          
      <%-- header1 starts here --%>
      <uix:rowLayout>
        <uix:contents>
          <uix:header text="<%=strHeader1Text %>">
            <uix:contents>
              <uix:rowLayout>
                <uix:contents>
                  <uix:styledText styleClass="OraInstructionText"
                        text="<%=strHeader1Explanation %>"/>
                </uix:contents>
              </uix:rowLayout>
            </uix:contents>
          </uix:header>
        </uix:contents>
      </uix:rowLayout>

      <uix:rowLayout>
        <uix:contents>
          <uix:tableLayout hAlign="center" cellPadding="10" cellSpacing="5" width="70%">
            <uix:contents>

              <uix:rowLayout>
                <uix:cellFormat height="8"/>
              </uix:rowLayout>

              <uix:rowLayout>
                <uix:contents>
                  <uix:cellFormat wrappingDisabled="true">
                    <contents>
                      <uix:styledText styleClass="OraFieldText"
                        text="<%=strLocationLabel %>"/>
                    </contents>      
                  </uix:cellFormat>
                  <uix:cellFormat>
                    <contents>
                      <uix:styledText styleClass="OraFieldText"
                        text="<%=locationValue %>"/>
                    </contents>      
                  </uix:cellFormat>
                </uix:contents>
              </uix:rowLayout>

            </uix:contents>
          </uix:tableLayout>
        </uix:contents>
      </uix:rowLayout>
      <%-- header1 ends here --%>
      <%
        }//End of selective display of privder URL location section
      %>      

     <input type="hidden" name="<%=ProviderConstants.PROVIDER_URL %>" value="<%=locationValue %>"/>

      <%-- Repository Connection Settings starts here --%>
      <uix:rowLayout>
        <uix:contents>
          <uix:header text="<%=strReposTitle %>">
            <uix:contents>
              <uix:rowLayout>
                <uix:contents>
                  <uix:styledText styleClass="OraInstructionText"
                        text="<%=strReposDescription %>"/>
                </uix:contents>
              </uix:rowLayout>
              <uix:rowLayout>
                  <uix:styledText styleClass="OraInstructionText"
                        text="<%=strReposNote %>"/>
              </uix:rowLayout>
            </uix:contents>
          </uix:header>
        </uix:contents>
      </uix:rowLayout>

      <uix:rowLayout>
        <uix:contents>

          <%-- Table layout for the database connection information --%>
          <%-- In 904, we should add styleClass='OraFieldText' for all textInput --%>
          <uix:tableLayout hAlign="center" cellSpacing="5" width="70%">
            <uix:contents>

              <uix:rowLayout>
                <uix:cellFormat height="8"/>
              </uix:rowLayout>

            <%-- RAA --%>
            <uix:rowLayout hAlign="left">
              <uix:contents>                
                <uix:cellFormat wrappingDisabled="true">
                  <contents>
                    <uix:styledText styleClass="OraFieldText"
                      text="<%=strUseRaaLabel %>"/>
                  </contents>      
                </uix:cellFormat>
                <uix:cellFormat>
	          <%-- After moving to uix 2.1 (904), we should use uix:list 
		       with onChange attribute --%>
                  <uix:rawText>
                  <select class="OraFieldText" name="<%=ProviderConstants.USE_RAA %>" size="1" onChange="javascript:checkRaa(this)">
                  <% if (strRaaSelected.equals("0")) { %>
                      <option value="true" selected><%=strRaaEnabledExplanation%></option>
                      <option value="false"><%=strRaaDisabledExplanation%></option>
                  <% } else { %>
                      <option value="true"><%=strRaaEnabledExplanation%></option>
                      <option value="false" selected><%=strRaaDisabledExplanation%></option>
                  <% } %>
                  </select>
                  </uix:rawText>
                </uix:cellFormat>
                <uix:cellFormat>
                    <%--
                    <uix:submitButton formName="EditProviderForm" 
                                      name="<%=ProviderConstants.REPOS_TARGET%>"
                                      text="Test" 
                                      value="test"/>
                    --%>
                </uix:cellFormat>
              </uix:contents>
            </uix:rowLayout>

            <% if (strUseRaaVal.equals("false")) { %>
            <uix:rowLayout hAlign="left">
              <uix:contents>
                <uix:cellFormat wrappingDisabled="true">
                  <uix:spacer width="20"/>
                  <uix:styledText styleClass="OraTipLabel"
                    text="<%= strReposTip %>"/>
                </uix:cellFormat>
              </uix:contents>
            </uix:rowLayout>
           <%-- Database Server Host --%>
            <uix:rowLayout hAlign="left">
              <uix:contents>
                <uix:cellFormat>
                  <uix:spacer width="20"/>
                  <uix:styledText styleClass="OraFieldText"
                    text="<%=strReposHostLabel %>"/>
                </uix:cellFormat>
                <uix:cellFormat>
                  <uix:textInput name="<%=ProviderConstants.REPOS_HOST %>"
                    text="<%=strReposHostVal %>" columns="<%= iColumnLong %>"/>
                </uix:cellFormat>
              </uix:contents>
            </uix:rowLayout>

            <%-- Database Listener Port --%>
            <uix:rowLayout hAlign="left">
              <uix:contents>
                <uix:cellFormat>
                  <uix:spacer width="20"/>
                  <uix:styledText styleClass="OraFieldText"
                    text="<%=strReposPortLabel %>"/>
                </uix:cellFormat>
                <uix:cellFormat>
                  <uix:textInput name="<%= ProviderConstants.REPOS_PORT %>"
                    text="<%=strReposPortVal %>" columns="<%= iColumnShort %>"/>
                </uix:cellFormat>
              </uix:contents>
            </uix:rowLayout>

            <%-- Database SID --%>
            <uix:rowLayout hAlign="left">
              <uix:contents>
                <uix:cellFormat>
                  <uix:spacer width="20"/>
                  <uix:styledText styleClass="OraFieldText"
                    text="<%=strReposSidLabel %>"/>
                </uix:cellFormat>
                <uix:cellFormat>
                  <uix:textInput name="<%= ProviderConstants.REPOS_SID %>"
                    text="<%=strReposSidVal %>" columns="<%= iColumnShort %>"/>
                </uix:cellFormat>
              </uix:contents>
            </uix:rowLayout>
    
            <%-- Database Username --%>
            <uix:rowLayout hAlign="left">
              <uix:contents>
                <uix:cellFormat>
                  <uix:spacer width="20"/>
                  <uix:styledText styleClass="OraFieldText"
                    text="<%=strReposUserLabel %>"/>
                </uix:cellFormat>
                <uix:cellFormat>
                  <uix:textInput name="<%= ProviderConstants.REPOS_USER %>"
                    text="<%=strReposUserVal %>" columns="<%= iColumnShort %>"/>
                </uix:cellFormat>
              </uix:contents>
            </uix:rowLayout>

            <%-- Database Password --%>
            <uix:rowLayout hAlign="left">
              <uix:contents>
                <uix:cellFormat>
                  <uix:spacer width="20"/>
                  <uix:styledText styleClass="OraFieldText"
                    text="<%=strReposPassLabel %>"/>
                </uix:cellFormat>
                <uix:cellFormat>
                  <uix:textInput name="<%= ProviderConstants.REPOS_PASS %>"
                    text="<%=strReposPassVal %>" secret="true" columns="<%= iColumnShort %>"
                    maximumLength="30"/>
                </uix:cellFormat>
              </uix:contents>
            </uix:rowLayout>
            <% } else { %>
              <uix:formValue name="<%=ProviderConstants.REPOS_HOST%>" value="<%=strReposHostVal%>" />
              <uix:formValue name="<%=ProviderConstants.REPOS_PORT%>" value="<%=strReposPortVal%>" />
              <uix:formValue name="<%=ProviderConstants.REPOS_SID%>" value="<%=strReposSidVal%>" />
              <uix:formValue name="<%=ProviderConstants.REPOS_USER%>" value="<%=strReposUserVal%>" />
              <uix:formValue name="<%=ProviderConstants.REPOS_PASS%>" value="<%=strReposPassVal%>" />
            <% } %>

            <%-- ASO --%>
            <uix:rowLayout hAlign="left">
              <uix:contents>                
                <uix:cellFormat wrappingDisabled="true">
                  <contents>
                    <uix:styledText styleClass="OraFieldText"
                      text="<%=strUseAsoLabel %>"/>
                  </contents>      
                </uix:cellFormat>
                <uix:cellFormat>
                  <uix:list name="<%=ProviderConstants.USE_ASO %>" size="1"
                    selectedIndex="<%=strAsoSelected %>">
                    <uix:contents>
                      <uix:option text="<%= strAsoDisabledExplanation %>" 
                        value="false"/>
                      <uix:option text="<%= strAsoEnabledExplanation %>" 
                        value="true"/>
                    </uix:contents>
                  </uix:list>
                </uix:cellFormat>
              </uix:contents>
            </uix:rowLayout>
          </uix:contents>
        </uix:tableLayout> <%-- Table Layout to setup the database connection --%>
      </uix:contents>
    </uix:rowLayout>
<%
  if (showProxySection)
  { 
%>

      <%-- Proxy starts here --%>
      <uix:rowLayout>
        <uix:contents>
          <uix:header text="<%=strProxyTitle %>">
            <uix:contents>
              <uix:rowLayout>
                <uix:contents>
                  <uix:styledText styleClass="OraInstructionText"
                    text="<%=strProxyExplanation %>"/>
                </uix:contents>
              </uix:rowLayout>
            </uix:contents>
          </uix:header>
        </uix:contents>
      </uix:rowLayout>

      <uix:rowLayout>
        <uix:contents>
          <uix:tableLayout hAlign="center" cellSpacing="5" width="70%">
            <uix:contents>

              <uix:rowLayout>
                <uix:cellFormat height="8"/>
              </uix:rowLayout>

              <uix:rowLayout>
                <uix:contents>
                  <uix:cellFormat>
                    <uix:styledText styleClass="OraFieldText"
                      text="<%=strProxyHostLabel %>"/>
                  </uix:cellFormat>
                  <uix:cellFormat>
                    <uix:textInput name="<%=ProviderConstants.PROXY_HOST %>"
                      text="<%=strProxyHostVal %>" columns="<%= iColumnLong %>"/>
                  </uix:cellFormat>
                </uix:contents>
              </uix:rowLayout>

              <uix:rowLayout hAlign="left">
                <uix:contents>
                  <uix:styledText styleClass="OraFieldText"
                    text="<%=strProxyPortLabel %>"/>
                  <uix:textInput name="<%= ProviderConstants.PROXY_PORT %>"
                    text="<%=strProxyPortVal %>" columns="<%= iColumnShort %>"/>
                </uix:contents>
              </uix:rowLayout>

              <uix:rowLayout hAlign="left">
                <uix:contents>
                  <uix:styledText styleClass="OraFieldText"
                    text="<%=strProxyDontLabel %>"/>
                  <uix:textInput name="<%= ProviderConstants.DONT_PROXY_FOR %>"
                    text="<%=strProxyDontVal %>" columns="<%= iColumnLong %>"/>
                </uix:contents>
              </uix:rowLayout>

            </uix:contents>
          </uix:tableLayout>
        </uix:contents>
      </uix:rowLayout>
  <%
      } 
  %>


  </uix:contents>
</uix:tableLayout> <%-- Overall table layout --%>
