<%--
  Copyright 2003 Sun Microsystems, Inc. All rights reserved
  Use is subject to license terms.
 --%>
<%@ page import=
    "com.sun.portal.providers.simplewebservice.SimpleWebServiceProviderAdapter,
	com.sun.portal.providers.simplewebservice.SimpleWebServiceParameter,
	com.sun.portal.providers.simplewebservice.util.XList,
	com.sun.portal.providers.simplewebservice.SimpleWebServiceState,
	com.sun.portal.providers.simplewebservice.SimpleWebServiceException,
	com.sun.portal.providers.simplewebservice.SimpleWebServiceProcessException,
	java.util.Iterator,java.util.HashMap,java.util.Map,
	com.sun.portal.providers.ProviderException"
%>

<%@ taglib uri="/tld/desktop.tld" prefix="dt" %>
<%@ taglib uri="/tld/desktopProviderContext.tld" prefix="dtpc" %>
<%@ taglib uri="/tld/jx.tld" prefix="jx" %>
<script>
function cloneRow(orId, drId,arrayName){
theRow=document.getElementById(orId);
theRow.parentNode.insertBefore(theRow.cloneNode(true), document.getElementById(drId));
arraySize=document.getElementById(arrayName);
arraySize.value++;
}

function toggleInvokeAction()
{
  var x=document.getElementById("SampleSimpleWebServiceConfigurable_invokeAction");
  x.value="false";
  return true;
}

</script>

<%!
private void writeArraySizes(PageContext pContext,SimpleWebServiceParameter pdesc) throws Exception
{
   if(pdesc.isArrayType()  )
   {
     int arraySize = 1;
     XList tmp =(XList)pdesc.getValue();
     if(tmp != null)
	 {
		arraySize = tmp.size();
	 }
    writeData(pContext,"<b><text>"+"Size of "+pdesc.getFullName()+"</text></b>");
    writeData(pContext,"<input type=\"text\" name=\""+pdesc.getFullName()+"\" id=\""+pdesc.getFullName()+"\" value=\""+arraySize+"\">");
    writeData(pContext,"<br/>");
   }

   Map elementSizes = new HashMap();
   SimpleWebServiceParameter childParam = null;
   Integer ii = new Integer(0);

   if(pdesc.getType().equals(XList.class))
   {
        XList children = (XList)pdesc.getValue();
        Iterator childIterator = children.iterator();
        while(childIterator.hasNext())
        {
           childParam = (SimpleWebServiceParameter)childIterator.next();
           if(childParam.isRepeatable())
	       {
             if((ii=(Integer)elementSizes.get(childParam.getFullName())) == null)
             {
              elementSizes.put(childParam.getFullName(),new Integer(1));
             }
             else
             {
              elementSizes.put(childParam.getFullName(),new Integer(ii.intValue()+1));
             }
           }
       }
      

       Iterator keys = elementSizes.keySet().iterator();
       Integer arraySize = null;
       while(keys.hasNext())
       {
          String name = (String )keys.next();
          arraySize =(Integer) elementSizes.get(name);
          writeData(pContext,"<b><text>"+"Size of "+ name+"</text></b>");
          writeData(pContext,"<input type=\"text\" name=\""+name+"\" id=\""+name+"\" value=\""+arraySize+"\">");
          writeData(pContext,"<br/>");
       }

  }

   return;
}
private void writeArraySizes(PageContext pContext,SimpleWebServiceParameter[] pdescArray) throws Exception
{
    for(int i=0;i < pdescArray.length ;i++)
        writeArraySizes(pContext,pdescArray[i]);
}
private  void writeData(PageContext pContext,String s) throws IOException
{
   JspWriter out = pContext.getOut();
   out.println(s);
}
public  String printSimpleWebServiceParameter(PageContext pContext,SimpleWebServiceParameter pdesc) throws IOException,ProviderException
{

    JspWriter out = pContext.getOut();
    SimpleWebServiceProviderAdapter provider =
            (SimpleWebServiceProviderAdapter)pContext.getAttribute("JSPProvider");

    StringBuffer strBuf = new StringBuffer();

    if(pdesc == null)
    {
      return strBuf.toString();
    }

    if(!pdesc.getType().equals(XList.class))
    {

          strBuf.append("<sws:simpleType name=\""+pdesc.getFullName()+"\" class=\""+pdesc.getType().getName()+"\"/>\n");
          writeBeginTR(pContext,pdesc.getFullName());
          writeBeginTD(pContext);
          writeData(pContext,pdesc.getFullName());
          writeEndTD(pContext);
          writeBeginTD(pContext);
          String value = "";
          if(!pdesc.getStrValue().equals(""))
	      {
       	    value = pdesc.getStrValue();
          }
		  else
		  {
		   value = pdesc.getTypeHint();
		  }
          writeData(pContext,"<input type=\"text\" name=\""+provider.makeUniqueStr(pdesc.getFullName())+"\" value=\""+value+"\"/>");
          writeEndTD(pContext);
          writeEndTR(pContext);
    }
    else
    {
      if(pdesc.isArrayType() )
      {
	   writeBeginTR(pContext,"Array"+pdesc.getFullName());
      }
      else
      {
        writeBeginTR(pContext,pdesc.getFullName());
      }
      writeBeginTD(pContext);
      writeData(pContext,pdesc.getFullName());


      if(pdesc.isArrayType()  )
      {
        int arraySize = 1;
	    XList tmp =(XList)pdesc.getValue();
        if(tmp != null)
	    {
	    	arraySize = tmp.size();
		}


      }
      else
      {
      }
      writeEndTD(pContext);
    }
    if(pdesc.getType().equals(XList.class))
    {
       writeBeginTD(pContext);
       writeBeginTable(pContext);
       XList paramDescriptorList = (XList)pdesc.getValue();
       if(paramDescriptorList != null)
       {
           Iterator paramDescriptorListIter = paramDescriptorList.iterator();
           while (paramDescriptorListIter.hasNext()) {
			    Object childParam = paramDescriptorListIter.next();
			    if(childParam instanceof SimpleWebServiceParameter)
			    {
                  SimpleWebServiceParameter childDesc =
                                (SimpleWebServiceParameter)childParam;
	              printSimpleWebServiceParameter(pContext,childDesc);
			    }
			    else
			    {
				 writeBeginTR(pContext);
			     writeBeginTD(pContext);
				 writeData(pContext,"<text>Item</text>");
                 writeEndTD(pContext);
				 writeBeginTD(pContext);
			     writeData(pContext,"<text>" + childParam.toString() + "</text>");
				 writeEndTD(pContext);
				 writeEndTR(pContext);
			    }
				

           }
        }
        writeEndTable(pContext);
        writeEndTR(pContext);
    }


return null;

}
private  void writeBeginTR(PageContext pContext,String id) throws IOException
{
  JspWriter out = pContext.getOut();
  out.println("<TR id=\""+id+"\">");
}
private  void writeBeginTR(PageContext pContext) throws IOException
{
  JspWriter out = pContext.getOut();
  out.println("<TR>");
}
private  void writeEndTR(PageContext pContext) throws IOException
{
  JspWriter out = pContext.getOut();
  out.println("</TR>");
}
private   void writeBeginTD(PageContext pContext) throws IOException
{
  JspWriter out = pContext.getOut();
  out.println("<TD>");
}
private  void writeEndTD(PageContext pContext) throws IOException
{
  JspWriter out = pContext.getOut();
  out.println("</TD>");
}
private  void writeBeginTable(PageContext pContext) throws IOException
{
  JspWriter out = pContext.getOut();
  out.println("<table border=\"1\">");
}
private  void writeEndTable(PageContext pContext) throws IOException
{
   JspWriter out = pContext.getOut();
   out.println("</table>");
}

%>

<%
        SimpleWebServiceProviderAdapter provider =
            (SimpleWebServiceProviderAdapter)pageContext.getAttribute("JSPProvider");
 %>
<dt:obtainChannel channel="$JSPProvider">
  <dtpc:providerContext>
	        <dtpc:getStringProperty key="fontFace1" id="fontFace"/>
	        <jx:declare id="fontFace" type="java.lang.String"/>

<table border=1>
    <tr valign=top>
        <td><font face="<%=fontFace%>" size="+0">WSDL:</font></td>
	<td><font face="<%=fontFace%>" size="+0"><form><textarea name="simple" rows="1" cols="25" wrap="OFF"><%= provider.getWSDL() %></textarea></form></font></td>
    </tr>
    <tr valign=top>
        <td><font face="<%=fontFace%>" size="+0">Method Name:</font></td>
        <td><font face="<%=fontFace%>" size="+0"><%= provider.getMethodName() %></font></td>
    </tr>
</table>
<br>
<P>
</P>

<%
try {
boolean isDefaultAvailable = provider.isDefaultAvailable();

%>
	<FORM NAME="content_form" METHOD=POST
				ENCTYPE="application/x-www-form-urlencoded">

	    <INPUT TYPE=HIDDEN NAME="myProvider" SIZE=-1
					VALUE=<%= provider.getName() %> >
	    <% if (provider.showInputForm(request)) { %>
		<INPUT TYPE=HIDDEN
			NAME=<%= provider.makeUniqueStr("invokeAction") %> ID=<%=provider.makeUniqueStr("invokeAction") %>
			SIZE=-1 VALUE="true" >
		<table border=1>
		<%
		SimpleWebServiceParameter[] fields =
			provider.defineInputForWebService(request);
		for ( int i = 0; fields != null && i < fields.length; i++) {

              try{

              printSimpleWebServiceParameter(pageContext,fields[i]);
              }
              catch(Exception e)
              {
		e.printStackTrace();
	      }

			}
			%>
		</table>
               <%
                try{
		writeArraySizes(pageContext,fields);
                }catch(Exception e)
		{ 
		   e.printStackTrace();
		} %>
		<INPUT TYPE=SUBMIT NAME="Submit" VALUE="Run" CLASS="button">
		<INPUT TYPE=SUBMIT NAME="Update" Value="Update" CLASS="button" onClick="return toggleInvokeAction();">

             
	    <% 
} else {
		SimpleWebServiceState result = provider.processRequest(request);
		SimpleWebServiceParameter output = result.getOutputState();
		SimpleWebServiceParameter[] input = result.getInputState();
		SimpleWebServiceProcessException processError = result.getProcessError();
	    %>

		<%--
			DISPLAY  INPUT USED
		--%>
		<P>
		<font face="<%=fontFace%>" size="+0"><b>INPUT VALUES</b></font>
		</P>
		<table border=1>
		<%
		for ( int i = 0; input != null && i < input.length; i++) {
			printSimpleWebServiceParameter(pageContext,input[i]);
		}
		%>
		</table>

		<%--
			DISPLAY OUTPUT
		--%>
		<INPUT TYPE=HIDDEN
			NAME=<%= provider.makeUniqueStr("invokeAction") %>
			SIZE=-1 VALUE="false" ID=<%= provider.makeUniqueStr("invokeAction") %> >

			<%
			if ( output != null) {
			%>
				<P>
				<b>OUTPUT VALUES</b>
				</P>
			<table border="1">
			<% printSimpleWebServiceParameter(pageContext,output);
			} %>
			</table>
			<%
			if ( processError != null ) {
			%>
				<P>
				<b>ERROR</b>
				</P>
				<table border=1>
					<tr valign=top>
						<td>Error:</td>
						<td><%= processError.getMessage() %></td>
					</tr>
				</table>
			<%
			}
			%>

		<br>
		<INPUT TYPE=SUBMIT NAME="Submit" VALUE="OK" CLASS="button">
	    <%
	    }
	    %>
	</FORM>
<%
}
catch(SimpleWebServiceException ex) {
%>
	<table border=1>
	    <tr valign=top>
		<td>Error:</td>
		<td><%= ex.getMessage() %></td>
	    </tr>
	</table>
<%
}
%>

  </dtpc:providerContext>
</dt:obtainChannel>




<!-- Channel width is p.getWidth() -->
<%-- This is a JSP comment, should not appear in HTML source --%>
