Java Mapping Step by Step

Posted on May. 05, 2010 at 01.15 PM - Kuala Lumpur, Malaysia

Mapping programs can be implemented in Java this way

    1. Develop your Java code locally e.g. with your IDE
    2. Create a .jar or a .zip
      • Using the jar command of the jdk
      • Using a built-in IDE functionality
    3. Create a new Imported Archive and import the .jar to the Integration Repository

The java program should be structured this way

    1. Define a Java class that implements the Java interface com.sap.aii.mapping.api.StreamTransformation
    2. The interface contains two methods:
      • public void execute(java.io.InputStream in, java.io.OutputStream out) to perform the mappings.
      • public void setParameter(java.util.Map param) to access to runtime constants possible (for example: INTERFACE, SENDER_NAME)
    3. The required libraries are contained in the aii_map_api.jar, which is part of your SAP J2EE Engine installation
    4. DOM and SAX Parsers provided by the SAP XML Toolkit for Java can be used
    5. API to execute value mappings

Example of Java mapping

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

import com.sap.aii.mapping.api.StreamTransformation;

/**

* Input: MtCustomerName - Surname

* - Name

* Output: MtName

*/

public class NameMergeJava extends DefaultHandler implements StreamTransformation {

private Map map;

private OutputStream out;

private String MtName;

/* need some constants to know, which tag we have now */

private int tag = 0;

private final int NAME_TAG = 1;

private final int SURNAME_TAG = 2;

private final int MTNAME_TAG = 3;

private final int OTHER_TAG = 0;

/**

* method setParamters is required, but we do not anything with it

*/

public void setParameter(Map param) {

map = param;

}

/**

* method execute is called by the PI mapping program

*/

public void execute(InputStream in, OutputStream out) throws com.sap.aii.mapping.api.StreamTransformationException {

DefaultHandler handler = this;

SAXParserFactory factory = SAXParserFactory.newInstance();

try {

SAXParser saxParser = factory.newSAXParser();

this.out = out;

saxParser.parse(in, handler);

} catch (Throwable t) {

t.printStackTrace();

}

}

//===========================================================

// SAX DocumentHandler methods

//===========================================================

/**

* startDocument is called at the beginning, so we write the xml header

*/

public void startDocument() throws SAXException {

write("<?xml version='1.0' encoding='UTF-8'?>");

}

/**

* endDocument is called at the end, so we clear the buffer

*/

public void endDocument() throws SAXException {

try {

out.flush();

} catch (IOException e) {

throw new SAXException("I/O error", e);

}

}

/**

* startElement is called when a new tag appears. We store the information, which tag we have now.

* Each beginning tag in the input creates a different tag in the output

*/

public void startElement(String namespaceURI, String lName, String qName, Attributes attrs) throws SAXException {

String eName = lName; // element name

if ("".equals(eName)) {

eName = qName; // namespaceAware = false

}

if (eName.equals("Surname")) {

tag = SURNAME_TAG;

write("<ns:MtName xmlns:ns=\"urn:education.com:BIT460:mapping\">");

} else if (eName.equals("Name")) {

tag = NAME_TAG;

} else {

tag = OTHER_TAG;

}

}

/**

* endElement is called, when an end tag appears.

* We choose the right end tag for the output

*/

public void endElement(String namespaceURI, String sName, String qName) throws SAXException {

String eName = sName;

if ("".equals(eName)) {

eName = qName;

}

if (eName.equals("Name")) {

write("</ns:MtName>");

}

}

/**

* method characters is always called, when we are outside of tags.

* The output is depending from the tag we have.

*/

public void characters(char buf[], int offset, int len) throws SAXException {

String s = new String(buf, offset, len);

switch (tag) {

case SURNAME_TAG :

MtName = s; // save surname for later output

break;

case NAME_TAG :

write(s + " " + MtName); // output now: name + " " + surname

break;

}

tag = OTHER_TAG;

}

/**

* Wrap I/O exceptions in SAX exceptions, to suit handler signature requirements

*/

private void write(String s) throws SAXException {

try {

out.write(s.getBytes());

out.flush();

} catch (IOException e) {

throw new SAXException("I/O error", e);

}

}

}

Following steps are to create Java mapping using NWDS

1. Create Java project (File->New->Project and choose Java project)

2. Create a new package in the project (File->New->Package)

3. Create Java mapping or import from java program (Import->FileSystem->Next)

4. If there is error because of the aii.map_apai.jar is not possible, we can reference it as external Java archive.

5. Compile the class by saving it.

6. Export it as Jar file by using context menu from the project Export->Java->JAR File

The next step is to import the java mapping into PI archive

Don't forget to use this mapping in the operation mapping, and refer to this operation mapping in your interface determination.

Life is beautiful! Let's make it meaningful and colorful!