<?xml version="1.0"?>
<!--
 -  
 -  $Id: msg.vspx,v 1.3.2.2 2011/02/03 10:35:34 source Exp $
 -
 -  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 -  project.
 -  
 -  Copyright (C) 1998-2006 OpenLink Software
 -  
 -  This project is free software; you can redistribute it and/or modify it
 -  under the terms of the GNU General Public License as published by the
 -  Free Software Foundation; only version 2 of the License, dated June 1991.
 -  
 -  This program is distributed in the hope that it will be useful, but
 -  WITHOUT ANY WARRANTY; without even the implied warranty of
 -  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 -  General Public License for more details.
 -  
 -  You should have received a copy of the GNU General Public License along
 -  with this program; if not, write to the Free Software Foundation, Inc.,
 -  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 -  
-->
<v:page xmlns:v="http://www.openlinksw.com/vspx/" name="message_page" on-deadlock-retry="3">
    <v:variable name="oper" type="varchar" default="null" param-name="oper"/>
    <v:variable name="act" type="varchar" default="''" param-name="act"/>
    <v:variable name="tns" type="varchar" default="null" param-name="tns"/>
    <v:variable name="sty" type="int" default="0" param-name="sty"/>
    <v:variable name="msg" type="any" default="null"/>
    <v:variable name="rest" type="any" default="-1" persist="temp"/>
    <v:variable name="res" type="any" default="null" persist="temp"/>
    <v:variable name="endpurl" type="any" default="''" param-name="endp"/>
    <v:variable name="msg_name" type="any" default="null" param-name="msg-name"/>
    <v:variable name="wsdl" type="any" default="null" persist="session"/>
    <v:variable name="mode" type="int" default="0" persist="0"/>
    <v:on-init>
      set http_charset='utf-8';
    </v:on-init>
    <v:method name="msg_root_node" arglist="in path any">
	if (self.msg is null)
	  return vector ();
	return xpath_eval (path, self.msg, 0);
      </v:method>
    <v:method name="msg_chil_node" arglist="in path any, in node any">
	return xpath_eval ('*', node, 0);
      </v:method>
    <v:method name="get_path" arglist="inout ctr vspx_control">
	declare nod vspx_tree_node;
	declare elm vspx_field;
        declare path varchar;

	path := '';
	nod := ctr.vc_parent;
	elm := nod.vc_find_control ('elm');
	path := concat ('/', elm.ufl_value, path);
	while ((nod := nod.vc_parent) is not null
		and udt_instance_of (nod, fix_identifier_case ('DB.DBA.vspx_tree_node')))
	  {
	    elm := nod.vc_find_control ('pelm');
	    if (elm is not null)
	      path := concat (sprintf ('/*[%d]', nod.tn_position+1), path);
	  }
	return path;
      </v:method>
    <v:method name="get_path2" arglist="inout ctr vspx_control">
	declare nod vspx_control;
	declare elm vspx_field;
        declare path varchar;

	path := '';
	nod := ctr;
	while ((nod := nod.vc_parent) is not null
		and udt_instance_of (nod, fix_identifier_case ('DB.DBA.vspx_tree_node')))
	  {
	    elm := nod.vc_find_control ('pelm');
	    if (elm is not null)
	      path := concat (sprintf ('/*[%d]', (nod as vspx_tree_node).tn_position+1), path);
	  }
	return path;
      </v:method>
    <v:method name="validate_input" arglist="inout tp vspx_field, inout val vspx_field">
	  return;
      </v:method>
    <v:method name="get_nc_name" arglist="in x any">
      declare pos int;
      pos := strrchr (x, ':');
      if (pos)
	return subseq (x, pos+1, length (x));
      return x;
      </v:method>
      <v:method name="do_req" arglist="in fl any, inout e vspx_event">
		  <![CDATA[
		  declare ret, xt, xp, pars, flag, conn, reply_to any;
		  declare l, dir, dbg int;

		  dbg := 1;
		 reply_to := null;

		 pars := vector ();
		 if (self.msg is not null)
	           {
		  xp := xpath_eval ('/*', self.msg, 0);
		  l := length (xp) * 2;
		  pars := make_array (l, 'any');

		  for (declare i int, i := 0; i < l; i := i + 2)
		     {
		       pars [i] := sprintf ('par%d', i/2);
		       pars [i+1] := xml_cut (xp[i/2]);
		       }
		    }

		  flag := 64+128;

		  dir := 1;
		  self.rest := 1;

		  if (fl)
		    {
		      self.rest := 2;
		      flag := 256;
		    }


		  commit work;
		  ret := db..soap_client (
		  	url=>self.endpurl,
			operation=>self.oper,
			soap_action=>self.act,
			style=>self.sty+flag,
			parameters => pars,
			target_namespace=>self.tns,
			direction=>dir
			);
		if (ret is not null)
		  {
		    self.res := xml_tree_doc (ret);
		  }
		  ]]>
      </v:method>
  <html xmlns:v="http://www.openlinksw.com/vspx/">
    <head>
      <link rel="stylesheet" href="default.css" type="text/css"/>
    </head>
    <body onload="popUpck()">
      <script type="text/javascript"><![CDATA[
	function popUpck () {
	  if (opener == null)
	    {
	      document.getElementById('close_btn').innerHTML = '';
	    }
	}
	]]></script>
      <a id="close_btn" href="javascript: void(0);" onclick="javascript: if (opener != null) opener.focus(); window.close()" class="close_btn">
	<img src="close_16.gif" border="0" class="dialog_icon"/>Close
      </a>
      <div class="head1"><h1>Web Services Test Page (VSMX)</h1></div>
      <v:form name="f1" type="simple" method="POST">
	<v:login name="l1" realm="vsmx" mode="url" user-password-check="vsmx_user_check"/>
	<v:local-variable name="dummy">
	  <v:before-data-bind><![CDATA[
	  if (isstring (self.wsdl))
	    self.wsdl := xtree_doc (self.wsdl);
	  if (self.msg is null)
	    {
	      self.msg :=
	       xslt ('file:/vsmx/vsmx_msg.xsl', self.wsdl, vector ('msg', self.get_nc_name (self.msg_name)));
	    }
	  if (isstring (self.msg))
	    {
	      if (length (self.msg))
	        {
 	          self.msg := xtree_doc (self.msg, 0, '', 'UTF-8');
		}
	      else
	        self.msg := null;
            }
	  ]]></v:before-data-bind>
      </v:local-variable>
	<table cellpadding='10' cellspacing='0' border='0' width='100%'>
	  <tr>
	    <td>
	      <table cellpadding="0" cellspacing="0" border="0">
		<tr>
		  <td class="page_tab<?V case when self.mode = 0 then '_selected' else '' end ?>">
		    <v:button action="simple" name="msgview_switch_tree_templ"
		      value="Tree view"
		      style="url"
		      active="--equ (self.mode, 1)"
		      >
		      <v:on-post>
			self.mode := 0;
			control.vc_data_bind (e);
			self.msgview_switch_txt_templ.vc_data_bind (e);
		      </v:on-post>
		    </v:button>
		  </td>
		  <td class="page_tab<?V case when self.mode = 1 then '_selected' else '' end ?>">
		    <v:button action="simple" name="msgview_switch_txt_templ"
		      value="Source view"
		      style="url"
		      active="--equ(self.mode,0)"
		      >
		      <v:on-post>
			self.mode := 1;
			control.vc_data_bind (e);
			self.msgview_switch_tree_templ.vc_data_bind (e);
		      </v:on-post>
		    </v:button>
		  </td>
		  <td class="page_tab_empty" align="center" width="100%">
		    <table cellpadding="0" cellspacing="0">
		      <tr>
			<td width="100%">
			</td>
		      </tr>
		    </table>
		  </td>
		</tr>
	      </table>
	      <table class="tab_page" border="0">
		<tr><td colspan="2">&amp;#160;</td></tr>
		<tr>
		  <td nowrap="1">
		    <span class="schemaHeader2">SOAP Operation</span>
		  </td>
		  <td width="100%">
		    <span class="schemaHeader">"<v:label name="le2" value="--self.oper" render-only="1"/>"</span>
		  </td>
		<tr>
		  <td nowrap="1">
		    <span class="schemaHeader2">Endpoint URL</span>
		  </td>
		  <td width="100%">
		    <span class="schemaHeader">"<v:label name="le1" value="--self.endpurl" render-only="1"/>"</span>
		  </td>
		</tr>
		</tr>
		<tr>
		  <td colspan="2">
		    <!-- MAIN -->
      <H2>Enter input parameters</H2>
      <v:tab name="msgview" initial-active="tree_templ" style="custom" is-input="0" enabled="--case when self.msg is not null then 1 else 0 end">
	<v:template name="tree_templ" type="simple" title="Tree View">
	  <v:tree name="t1" multi-branch="1" orientation="vertical" start-path="/*" open-at="--'//*'" root="self.msg_root_node" child-function="self.msg_chil_node">
	    <v:node-template name="node_tmpl">
	      <div STYLE="margin-left:1em;">
		<v:button name="bt1_toggle" action="simple" style="image" value="--case (control.vc_parent as vspx_tree_node).tn_open when 0 then 'plus.gif' else 'minus.gif' end">
		  <v:on-post>
		    declare path, xp any;
		    path := self.get_path2 (control);
		    path := path || '/*';
		    while ((xp := xpath_eval (path, self.msg)) is not null)
		    XMLReplace (self.msg, xp, null);
		    self.t1.vc_data_bind (e);
		  </v:on-post>
		</v:button>
		<b>
		  <v:label name="pelm" value="--null">
		    <v:after-data-bind>
		      declare xt, xp any;
		      xt := (control.vc_parent as vspx_tree_node).tn_element;
		      xp := cast (xpath_eval ('local-name()',xt) as varchar);
		      control.ufl_value := xp;
		    </v:after-data-bind>
		  </v:label>
		</b>
		<v:node/>
	      </div>
	    </v:node-template>
	    <v:leaf-template name="leaf_tmpl">
	      <div STYLE="margin-left:1em;">
		<v:label name="elm" value="--null">
		  <v:after-data-bind>
		    declare xt, xp any;
		    xt := (control.vc_parent as vspx_tree_node).tn_element;
		    xp := cast (xpath_eval ('local-name()',xt) as varchar);
		    control.ufl_value := xp;
		  </v:after-data-bind>
		</v:label>
		<v:label name="tylabel2" value="--null" format="(%s)">
		  <v:after-data-bind><![CDATA[
		    declare xt, xp any;
		    declare tp varchar;
		    xt := (control.vc_parent as vspx_tree_node).tn_element;
		    tp := cast (xpath_eval ('@type',xt) as varchar);
		    xp := self.get_nc_name (tp);
		    control.ufl_value := coalesce (xp, '');
		    if (tp not like 'http://www.w3.org/2001/XMLSchema:%' and tp <> xp)
		    {
		    declare txt vspx_field;
		    txt := control.vc_parent.vc_find_control ('var_val');
		    txt.vc_enabled := 0;
		    }
		    ]]></v:after-data-bind>
		</v:label>
		<v:text name="var_val" value="" xhtml_size="35">
		  <v:after-data-bind>
		    declare xt, xp any;
		    xt := (control.vc_parent as vspx_tree_node).tn_element;
		    xp := xpath_eval ('string(.)',xt);
		    control.ufl_value := xp;
		  </v:after-data-bind>
		  <v:on-post>
		    declare path, xp, tp any;
		    declare txt vspx_field;
		    path := self.get_path (control);
		    txt := control.vc_parent.vc_find_control ('tylabel2');
		    if (txt is not null)
		    self.validate_input (txt, control);
		    xp := xpath_eval (path||'/text()', self.msg);
		    control.ufl_value := charset_recode (control.ufl_value, 'UTF-8', '_WIDE_');
		    if (xp is not null)
		    XMLReplace (self.msg, xp, control.ufl_value);
		    else
		    XMLAppendChildren (xpath_eval (path, self.msg), control.ufl_value);
		  </v:on-post>
		</v:text>
		<v:button name="addcpx" value="plus.gif" style="image" action="simple">
		  <v:after-data-bind>
		    declare txt vspx_field;
		    txt := control.vc_parent.vc_find_control ('var_val');
		    if (txt.vc_enabled)
		    {
		    control.vc_enabled := 0;
		    return 0;
		    }
		  </v:after-data-bind>
		  <v:on-post>
		    declare frag, nam, stub any;
		    declare xt, xp, path any;
		    xt := (control.vc_parent as vspx_tree_node).tn_element;
		    nam := cast (xpath_eval ('@type',xt) as varchar);
		    frag := xslt ('file://vsmx/vsmx_msg.xsl', self.wsdl,
		    vector ('typ', nam, 'msg', ' none'));
		    path := self.get_path (control);
		    stub := xpath_eval ('/stub/*', frag, 0);
		    foreach (any elm in stub) do
		    {
		    XMLAppendChildren (xpath_eval (path, self.msg), xml_cut (elm));
		    }
		    self.t1.vc_data_bind (e);
		  </v:on-post>
		</v:button>
	      </div>
	    </v:leaf-template>
	  </v:tree>
	</v:template>
	<v:template name="txt_templ" type="simple" title="Source View">
	  <div>
	    <v:textarea name="src_msg" value="--serialize_to_UTF8_xml (self.msg)" xhtml_cols="100" xhtml_rows="15">
	      <v:on-post>
		self.msg := xtree_doc (control.ufl_value, 0, '', 'UTF-8');
	      </v:on-post>
	      <v:before-render>
		control.ufl_value := charset_recode (control.ufl_value, 'UTF-8', '_WIDE_');
	      </v:before-render>
	    </v:textarea>
	  </div>
	</v:template>
      </v:tab>
  <div>
    <br/>
    <v:button name="inv" action="simple" value="Invoke">
      <v:on-post>
	self.msgview.tb_active.vc_focus := 1;
	self.msgview.tb_active.vc_set_childs_focus (1, e);
	self.msgview.tb_active.vc_post (e);
	self.msgview.tb_active.vc_user_post (e);
	self.msgview.tb_active.vc_action (e);
	self.do_req (0,e);
      </v:on-post>
    </v:button>
    <v:button name="prev" action="simple" value="Preview">
      <v:on-post>
	self.msgview.tb_active.vc_focus := 1;
	self.msgview.tb_active.vc_set_childs_focus (1, e);
	self.msgview.tb_active.vc_post (e);
	self.msgview.tb_active.vc_user_post (e);
	self.msgview.tb_active.vc_action (e);
	self.do_req (1,e);
      </v:on-post>
    </v:button>
  </div>
      <br/>
  <div>
    <div>
      <v:label name="pr1" value="--case self.rest when 1 then 'Response' when 2 then 'Request' else '' end" render-only="1"/>
    </div>
    <?vsp
    if (self.res is not null)
      {
       declare xs any;
       xs := xslt ('file:/vsmx/vsmx_res.xsl', self.res, vector ('service', self.oper));
       http_value (xs);
       }
    ?>
  </div>
  <!-- END -->
		  </td>
		</tr>
		<tr><td colspan="2"><v:url name="ub1" value="Back to operations page" url="oper.vspx"/></td></tr>
	      </table>
	    </td>
	  </tr>
	</table>
      </v:form>
      <div class="foot"><span class="foot">Virtuoso Universal Server <?V sys_stat('st_dbms_ver')?> - Copyright&amp;copy; 1998-2011 OpenLink Software.</span></div>
    </body>
  </html>
</v:page>
