<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Graversen &#187; xml</title>
	<atom:link href="http://graversen.org/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://graversen.org</link>
	<description>Daniels comments</description>
	<lastBuildDate>Wed, 04 Apr 2012 09:47:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>SAP PI XML Mappings using groovy</title>
		<link>http://graversen.org/2009/07/03/sap-pi-xml-mappings-using-groovy/</link>
		<comments>http://graversen.org/2009/07/03/sap-pi-xml-mappings-using-groovy/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 11:50:42 +0000</pubDate>
		<dc:creator>daniel</dc:creator>
				<category><![CDATA[pi]]></category>
		<category><![CDATA[pi71]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[process integration]]></category>
		<category><![CDATA[sap]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[transformation]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://graversen.org/2009/07/03/sap-pi-xml-mappings-using-groovy/</guid>
		<description><![CDATA[Using Groovy as java mappings for transformation of XML documents in the SAP PI or XI. ]]></description>
			<content:encoded><![CDATA[<p>Creating XML mapping in Java have for me always been difficult, it has been possible but I would prefer other tools. I was looking at scripting languages like Ruby/JRuby or Groovy for creating some web apps. Those languages seem quite hot right now. On the <a href="http://redirectingat.com?id=10371X688478&xs=1&url=http%3A%2F%2Fwiki.sdn.sap.com%2Fwiki%2Fdisplay%2Fevents%2FComposition%2Bon%2BGrails%2B-%2BComposition%2BEvolved&sref=rss">SCN Wiki</a> a group has implemented the Grails (groovy on Rails) on the Netweaver system, as Composition on Grails. With this tool it is possible to applications with a Webdynpro look and feel.  Grails is a framework for creating webapps with less coding.</p>
<p><a href="http://redirectingat.com?id=10371X688478&xs=1&url=http%3A%2F%2Fgroovy.codehaus.org%2FGetting%2BStarted%2BGuide&sref=rss">Groovy</a> is a scripting language designed on the basis of Java. Groovy script is compiled into Java classes, and both Java and Groovy can be mixed. This makes the implementation easier, just start writing Java and when you feel like use some of the smarter features of Groovy you can use them.</p>
<p>While I was looking at Grails, I thought that I would be possible to use it in PI. One place could be in java mappings. I&#8217;ll describe the steps that I have taken to implement this.</p>
<ol>
<li><a href="http://redirectingat.com?id=10371X688478&xs=1&url=http%3A%2F%2Fgroovy.codehaus.org%2FDownload&sref=rss">Download</a> and install the groovy library</li>
<li>Get the <a href="http://redirectingat.com?id=10371X688478&xs=1&url=http%3A%2F%2Fgroovy.codehaus.org%2FEclipse%2BPlugin&sref=rss">Groovy plugin</a> to Eclipse, this make developing much easier.</li>
<li>Create a new Eclipse project</li>
<li>Insert the aii_map_api.jar in the project, to be able to implement Streamtransformation service.</li>
<li>Create a new Groovy file in the source folder, with the name GroovyMapIdoc.groovy, then Eclipse know that it is a groovy file.</li>
<li>Create the mapping of your file. I have attached my example code bellow.</li>
<li>Compile the Groovy files using the context menu on the GroovyMapIdoc.groovy file.</li>
<li>Zip the content of the bin-groovy in the project folder and upload it, as an imported archive in the Integration builder. Alternative use <a href="http://redirectingat.com?id=10371X688478&xs=1&url=https%3A%2F%2Fwww.sdn.sap.com%2Firj%2Fscn%2Fweblogs%3Fblog%3D%2Fpub%2Fwlg%2F5341&sref=rss">ant build</a> to create the zip files.</li>
<li>Upload the two files Groovy-1.6.1.jar and asm-2.2.3.jar as imported archives. They can be found in &lt;GROOVY_HOME&gt;\lib</li>
<li>Activate and use the mapping.</li>
</ol>
<p>I would expect people trying this to have a good knowledge of using XI or PI Java mappings, because it is a requirement for the development of mappings.</p>
<p>One example I always have considered, was my first challenging mapping experience. Posting financial post with more than 1000 lines to the FIDCCP02 idoc. The FIDCCP02 only accepts 999 lines. The posting can be created multiply idocs with 998 lines and the post a balance on each item. This way all documents will balance.</p>
<p>The document is transformed from the left document to the right. I have for this example used a max size of 3 to make testing easier.</p>
<p><img src="http://graversen.org/wp-content/070309_1150_SAPPIXMLMap1.png" alt="" height="265" /><img src="http://graversen.org/wp-content/070309_1150_SAPPIXMLMap2.png" alt="" height="265" /></p>
<p>The code that I have used for the mapping is.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.figaf.mapping<br />
<br />
import com.sap.aii.mapping.api.StreamTransformation;<br />
import java.io.InputStream;<br />
import java.io.OutputStream;<br />
import java.util.Map;<br />
<br />
import groovy.xml.MarkupBuilder<br />
<br />
class GroovyMapIdoc implements StreamTransformation{<br />
<br />
&nbsp; &nbsp;Map param;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; void setParameter(Map param) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; this.param = param;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; // Number of lines pr idoc<br />
&nbsp; &nbsp; def step=3 <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; /**<br />
&nbsp; &nbsp; &nbsp;* Implementation of the execution method<br />
&nbsp; &nbsp; &nbsp;*/<br />
&nbsp; &nbsp; void execute(InputStream input, OutputStream out) {<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; // Parse the input using the XMLSlurper<br />
&nbsp; &nbsp; &nbsp; &nbsp; def FICCP01 = new XmlSlurper().parse(input)<br />
&nbsp; &nbsp; &nbsp; &nbsp; // get the different lines using the GPath<br />
&nbsp; &nbsp; &nbsp; &nbsp; def Lines = FICCP01.IDOC.LINE<br />
&nbsp; &nbsp; &nbsp; &nbsp; // create a writer example <br />
&nbsp; &nbsp; &nbsp; &nbsp; def writer = new OutputStreamWriter(out)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; def xml = new MarkupBuilder(writer)<br />
&nbsp; &nbsp; &nbsp; &nbsp; // create the root element and fill data into it. <br />
&nbsp; &nbsp; &nbsp; &nbsp; xml.FICCP01(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get the number of idocs to be created. <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def numIdocs = &nbsp; Lines.size()/step + (Lines.size()%step&gt;0?1:0) &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // loop for each idoc<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( i in 0..numIdocs-1 ) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // find the limit for the current idoc<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def max = Math.min( Lines.size(), i* step+2)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // create sum ellement to create balances<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def sum = 0.0;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;def lineno=1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IDOC(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // create the number segment, using GPATH<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NR(FICCP01.IDOC.NR )<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // for each line in the range do the following<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Lines[i*step..max].each{oldline-&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// create a &nbsp;new Line node, in the out put element<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// with the following content<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LINE(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NO(lineno++)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text(oldline.Text.text())<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Amount(oldline.Amount.toBigDecimal())<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// update the sum<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum +=oldline.Amount.toBigDecimal()<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // create a balancing line, with balances the result<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LINE(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NO(step+1)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text('Balance')<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Amount(-sum)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; // write the xml to output<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writer.flush()<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writer.close()<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; <br />
}</div></div>
<p>Behind the scenes the Groovy file is changed in to java classes. Because Java does not support Closures natively different subclasses are created. Try to have a look on them using a decompiler like <a href="http://redirectingat.com?id=10371X688478&xs=1&url=http%3A%2F%2Fwww.varaneckas.com%2Fjad&sref=rss">jad</a>.</p>
<p><strong>Conclusion</strong></p>
<p>Groovy could be a way to improve the how java mappings are created. The XML generation is easier to handle then how it would have been created in Java and it is more powerful than XSLT. It takes some effort to get use to the closures concept of Groovy and the other notation, but it seems to work real well.</p>
<p>I don&#8217;t think the performance issue with the mapping is a problem. There is an overhead to load the groovy libraries and the code is probably not as optimized if it was written directly in java. I have not made any measurements for this.</p>

<div class="skimlinks-disclosure-button"><p><script class="skimlinks_ref_script" type="text/javascript" src="http://static.skimlinks.com/api/ref.js?p=10371&amp;d=688478&amp;t=1"></script></p></div>
]]></content:encoded>
			<wfw:commentRss>http://graversen.org/2009/07/03/sap-pi-xml-mappings-using-groovy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

