Skip to Content

Creating XML mapping in Java have for me always been difficult, it has been possible but I would prefer other tools. An example on how to create XML in java with dom is created by Enhanced Receiver Determination using JAVA Mapping (DOM Parser). 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

SCN Wiki

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.

Groovy

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.

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’ll describe the steps that I have taken to implement this.

Get the

Groovy plugin

to Eclipse, this make developing much easier.

    1. Create a new Eclipse project
    2. Insert the aii_map_api.jar in the project, to be able to implement Streamtransformation service.
    3. Create a new Groovy file in the source folder, with the name GroovyMapIdoc.groovy, then Eclipse know that it is a groovy file.
    4. Create the mapping of your file. I have attached my example code bellow.
    5. Compile the Groovy files using the context menu on the GroovyMapIdoc.groovy file.

Zip the content of the bin-groovy in the project folder and upload it, as an imported archive in the Integration builder. Alternative use Shortcut for creating Jar files with eclipse to create the zip files. !https://weblogs.sdn.sap.com/weblogs/images/251963718/xmlorg.png|height=315|alt=image|width=289|src=https://weblogs.sdn.sap.com/weblogs/images/251963718/xmlorg.png|border=0! image

The code that I have used for the mapping is.



package com.figaf.mapping
 import com.sap.aii.mapping.api.StreamTransformation;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
 import groovy.xml.MarkupBuilder
 class GroovyMapIdoc implements StreamTransformation{
   Map param;    void setParameter(Map param) {  this.param = param;  }     // Number of lines pr idoc  def step=3     /** 

Implementation of the execution method
 */
 void execute(InputStream input, OutputStream out) {
     // Parse the input using the XMLSlurper
 def FICCP01 = new XmlSlurper().parse(input)
 // get the different lines using the GPath
 def Lines = FICCP01.IDOC.LINE  // create a writer example
  def writer = new OutputStreamWriter(out)    
  def xml = new MarkupBuilder(writer)         // create the root element and fill data into it.
    xml.FICCP01(){     // get the number of idocs to be created.
      def numIdocs =  Lines.size()/step + (Lines.size()%step>0?1:0) 
          // loop for each idoc  for ( i in 0..numIdocs-1 ) {  // find the limit for the current idoc
             def max = Math.min( Lines.size(), i* step+2)  // create sum ellement to create balances
            def sum = 0.0;     
            def lineno=1;
            IDOC(){  // create the number segment, using GPATH  NR(FICCP01.IDOC.NR )   
                    // for each line in the range do the following
                   //     Lines[i*step..max].each{oldline->          // create a  new Line node, in the out put element         
                                             // with the following content    
                      LINE(){
                       NO(lineno++)
                     Text(oldline.Text.text())
                     Amount(oldline.Amount.toBigDecimal())
                  }    // update the sum  
                 sum +=oldline.Amount.toBigDecimal()
               }          // create a balancing line, with balances the result
            LINE(){
              NO(step+1)
               Text('Balance')
              Amount(-sum)
    }  }  }  }
     // write the xml to output
 writer.flush()  writer.close()
   }
}

Update: 20 april 2015 to have formatting. I dont know if I missed something and if it still works

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 jad . The imported archive look like the following.

!https://weblogs.sdn.sap.com/weblogs/images/251963718/importedarchive.png|height=182|alt=image|width=500|src=https://weblogs.sdn.sap.com/weblogs/images/251963718/importedarchive.png|border=0!

Conclusion

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.

I don’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.

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. Volker Janzen
    which PI version do you use? In my interface mapping tests i get always a linkage error:

    java.lang.UnsupportedClassVersionError: groovy/lang/GroovyObject

    If i use older versions of groovy there is an error of type “CustomMetaClassCreationHandle.class not found”.

    Regards,
    Volker

    (0) 
    1. Daniel Graversen
      I used pi71 with java 5. I dont if it groovy runs on java 1.4.2.
      Have you uploaded the groovy jars.
      (0) 

Leave a Reply