<%@page import="org.apache.log4j.*,java.lang.*,java.util.*,wt.util.*,ext.tools.*"%>
<%@ taglib uri="http://www.ptc.com/infoengine/taglib/core" prefix="ie"%>
<%!
   /*
   Copyright (C) 2005 martl@sulis.de

   purpose: create a webject invocation ouf of parameters.

   This program 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;
   see http://www.gnu.org/licenses/gpl.html

   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.

   NOTE: on production systems you will want to remove this task
         or restrict access to this task to certain users and/or IPs
         otherwise direct task invocation can be used to run arbitrary webjects on your system!
   */

   Logger logger=Logger.getLogger("tasks.ext.tools.ListAll");
   Group g=null;
%>
<%
   Group sg=getContextGroup("SERVER");
   Group fg=getContextGroup("FORM");
   if(logger.isDebugEnabled()){
	ToolUtils.logGroup(logger,fg);
	ToolUtils.logGroup(logger,sg);
   }
   String outName="output";
   WTProperties props = WTProperties.getLocalProperties();
   String instance=props.getProperty("wt.federation.ie.VMName");
   String pair;
   int off;
   String name=(String)getContextGroup("FORM").getAttributeValue(0,"WEBJECT_NAME");
   if(name==null || name.equals(""))
      throw new IEException("mandatory parameter WEBJECT_NAME missing.");
%>
<ie:webject name="Start-Session" type="ACT">
    <ie:param name="INSTANCE" data="<%=instance%>"/>
    <ie:param name="AUTHORIZATION" data="${@server[]authorization[0]}"/>
    <ie:param name="GROUP_OUT" data="session_id"/>
</ie:webject>
<ie:unit>
<ie:webject name="Start-Transaction" type="ACT">
   <ie:param name="INSTANCE" data="<%=instance%>"/>
   <ie:param name="SESSION_ID" data="${session_id[]SESSION_ID[]}"/>
</ie:webject>
<%
   Webject aw = null;
   if(name.toLowerCase().indexOf("group")>=0) // misses 2 so far ... exotic
      aw=new GroupWebject(name);
   else
      aw=new ActionWebject(name);
   aw.setInputStream (inputStream);
   aw.setOutputStream (outputStream);
   aw.setService (service);
   aw.addParam ("INSTANCE", instance);
   aw.addParam ("GROUP_OUT", outName);
   aw.addParam ("AUTHORIZATION", (String)sg.getAttributeValue(0,"AUTHORIZATION"));
   Param p = new Param ("PARAM", "${@FORM[]PARAM[*]}");
   p.setValueSeparator ("\001");
   p.setDelim ("\001");
   Enumeration e = p.applyBinding (this);
   while(e.hasMoreElements()){
	pair=(String)e.nextElement (); 
	off=pair.indexOf("=");
	if(off == -1) 
	   aw.addParam (pair, "");
	else if(off == pair.length())
	   aw.addParam (pair.substring(0,off),"");
	else
	   aw.addParam (pair.substring(0,off),pair.substring(off+1));
   } // loop pairs
   aw.invoke (this);
%>
<ie:success>
<%
logger.debug("--> success ");
if((g=getGroup(outName))==null){ // e.g. add-content-items
   logger.info("success: null group -> create one.");
   addVdb(g=new Group(outName));
}
g.setSuccess("OK");
if(logger.isDebugEnabled())
   ToolUtils.logGroup(logger,g);
%>
<ie:webject name="Commit-Transaction" type="ACT">
    <ie:param name="INSTANCE" data="<%=instance%>"/>
    <ie:param name="SESSION_ID" data="${session_id[]SESSION_ID[]}"/>
</ie:webject>
<ie:webject name="Return-Groups" type="GRP">
<ie:param name="GROUP_IN" data="<%=outName%>"/>
</ie:webject>
</ie:success>

<ie:failure>
<% 
logger.debug("--> failure ");
%>
<ie:webject name="Copy-Group" type="GRP">
<ie:param name="GROUP_IN" data="@SERVER"/>
<ie:param name="GROUP_OUT" data="<%=outName%>"/>
</ie:webject>
<ie:webject name="Rollback-Transaction" type="ACT">
    <ie:param name="INSTANCE" data="<%=instance%>"/>
    <ie:param name="SESSION_ID" data="${session_id[]SESSION_ID[]}"/>
</ie:webject>
<%
g=getGroup(outName);
g.setSuccess("ERROR");
%>
<ie:webject name="End-Session" type="ACT">
    <ie:param name="INSTANCE" data="<%=instance%>"/>
    <ie:param name="SESSION_ID" data="${session_id[]SESSION_ID[]}"/>
</ie:webject>
<ie:webject name="Return-Groups" type="GRP">
<ie:param name="GROUP_IN" data="<%=outName%>"/>
</ie:webject>
<%
if(logger.isDebugEnabled())
   try{
	logger.debug("exception="+g.getElementAt(0).getValue("exception-message"));
      ((Exception)g.getElementAt(0).getValue("exception-object")).printStackTrace();
   }catch(Exception aSchoWurscht){
      throw new RuntimeException(aSchoWurscht);
   }
%>
</ie:failure>
</ie:unit>
