Skip to Content
Author's profile photo Carlos Ivan Prieto Rubio

Implementing a Java Mapping in SAP PI

Introduction

This blog explain you how to develop a java mapping class for using it in your Interface Mapping in SAP PI. It’s a tutorial for beginers.

Implements a Java class for mapping purpose require the next steps:

  1.- Development Tool like SAP Netweaver Developer Studio, NetBeans, Eclipse ….

  2.- Import aii_map_api.jar library.

  3.- Write the Source code for the mapping program. (We need to know DOM or SAX parsing proccesing)

  4.- Generate .jar file with the compiled class.

  5.- Import Jar File in SAP PI.

  6.- Use the program mapping in Interface Mapping.

1.- Open a Development Tool.

Firstly, we are going to open the SAP Netweaver Developer Studio for create a new project.

ScreenHunter_02Dec.2011.40.jpg

ScreenHunter_02Dec.2011.40.jpg

2.- Next, we must to import the aii_map_api.jar library. The file aii_map_api.jar is located in j2ee\\cluster\\server0\\apps\\sap.com\\com.sap.xi.services directory.

ScreenHunter_03Dec.2016.01.jpg       

3.-  Write Java Source.

package com.xi.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.MappingTrace;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;

public class NameMerge implements StreamTransformation {

    private Map param = null;
    private MappingTrace trace = null;

    public void setParameter(Map param) {
        this.param = param;
        if (param == null) {
            this.param = new HashMap();
        }
    }

    public void execute(InputStream input, OutputStream output)
        throws StreamTransformationException {

        AbstractTrace trace = null;
        String RESULT = new String();

        trace =
            (AbstractTrace) param.get(
                StreamTransformationConstants.MAPPING_TRACE);

        try {
            //Create DOM parser
            DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            //Parse input to create document tree
            Document doc = builder.parse(input);
            trace.addInfo(doc.toString());

            //Map the elements
            Node root = doc.getFirstChild(); // gets the root element
            NodeList children = root.getChildNodes();
       
            for (int item = 0; item < children.getLength(); item++) {
                if (children.item(item) instanceof Element) {
                    root = (Element) children.item(item);
                    NodeList ch = root.getChildNodes();
                    RESULT = RESULT.concat(ch.item(0).getNodeValue() + ” “);
                }

            }

            trace.addInfo(RESULT);

        } catch (Exception e) {
            trace.addDebugMessage(e.getMessage());
        }

        //Return the output document
        String document_exit =
            “<?xml version=\\”1.0\\” encoding=\\”UTF-8\\”?><ns0:Person2 xmlns:ns0=\\”urn:xxxxx.com:test:mapping:lookups\\”><RESULT>”
                + RESULT
                + “</RESULT></ns0:Person2>”;

        try {
            output.write(document_exit.getBytes());
        } catch (IOException e1) {
            trace.addDebugMessage(e1.getMessage());
        }

    }

}

4.- Generate JAR file.

   Go to File->Export in SAP Netweaver Developer Studio a choose the project where you have developed the mapping program and you must export it to a file.

5.- Import JAR File.

   Go to Integration Repository, Mapping Object -> Imported Archives. You must to create a new Imported Archives and import the file that you had generated in SAP NDS.

ScreenHunter_01Dec.2113.25.jpg

6.- Use the program mapping in Interface Mapping

Finally, you have to use the java class in an Interface Mapping.

ScreenHunter_03Dec.2113.31.jpg

The XML used for creating the java test class are:

<?xml version=”1.0″ encoding=”UTF-8″?>

<ns0:Person1 xmlns:ns0=”urn:xxxxx.com:test:mapping:lookups”>
   <NAME>Ivan</NAME>
   <SURNAME>Prieto</SURNAME>
</ns0:Person1>

<?xml version=”1.0″ encoding=”UTF-8″?>

<ns0:Person2 xmlns:ns0=”urn:xxxxxxx:test:mapping:lookups”><RESULT>ivan prieto </RESULT>

</ns0:Person2>

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member
      A great starter kit to understand the basics, I had no problems applying and testing. Thanks a lot for sharing!
      Author's profile photo Former Member
      Former Member
      Hey,

      Good one Bro...Keep blogging

      Cheers.,

      Stallin

      Author's profile photo Carlos Ivan Prieto Rubio
      Carlos Ivan Prieto Rubio
      Blog Post Author
      Thanks, I'll be in touch
      Author's profile photo Former Member
      Former Member
      Very good blog. It really gonna help.