mapping constants (runtime)sap-xi/pi · pdf filemapping constants (runtime) sap-xi/pi ... the...

16
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com © 2011 SAP AG 1 Mapping Constants (Runtime) SAP- XI/PI Applies to: This article talks about how to access mapping constant in different mapping programs during runtime (Message, java, xslt mappings), this is applicable to SAP-XI/PI versions 7.0 or 7.1 or 7.3. For more information, visit the SOA Management homepage . Summary Mapping programs can access mapping runtime constants; mapping constants are available in message header. The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants to access mapping runtime constants. Author: Prasad Babu Nemalikanti Company: Mahindra Satyam Created on: 26 July 2011 Author Bio Prasad Babu Nemalikanti, SAP-XI/PI certified consultant has more than 7 years of SAP experience including ,SAP-XI/PI,SAP-MDM, BIW and has good knowledge in ABAP, ALE and Java (certified).Currently working as a Team Lead in Mahindra Satyam prior to this associated with Fujitsu Consulting India Pvt Ltd.

Upload: truongdat

Post on 31-Jan-2018

300 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 1

Mapping Constants (Runtime) SAP-

XI/PI

Applies to:

This article talks about how to access mapping constant in different mapping programs during runtime (Message, java, xslt mappings), this is applicable to SAP-XI/PI versions 7.0 or 7.1 or 7.3. For more information, visit the SOA Management homepage.

Summary

Mapping programs can access mapping runtime constants; mapping constants are available in message header. The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants to access mapping runtime constants.

Author: Prasad Babu Nemalikanti

Company: Mahindra Satyam

Created on: 26 July 2011

Author Bio

Prasad Babu Nemalikanti, SAP-XI/PI certified consultant has more than 7 years of SAP experience including ,SAP-XI/PI,SAP-MDM, BIW and has good knowledge in ABAP, ALE and Java (certified).Currently working as a Team Lead in Mahindra Satyam prior to this associated with Fujitsu

Consulting India Pvt Ltd.

Page 2: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 2

Table of Contents

Introduction ......................................................................................................................................................... 3

1. Accessing Mapping Constants by Message Mapping ................................................................................ 4 Container Object: ......................................................................................................................................................... 4

Global Container Object: .............................................................................................................................................. 4

2. Accessing Mapping Constants by Java Mapping ....................................................................................... 5 Java Code (AbstractTransformation) ........................................................................................................................... 5

Java Code (StreamTransformation) ............................................................................................................................. 8

3. Accessing Mapping Constants by XSLT Mapping .................................................................................... 10

Related Content ................................................................................................................................................ 15

Disclaimer and Liability Notice .......................................................................................................................... 16

Page 3: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 3

Introduction

Sometimes it may be necessary to access fields in message header (mapping runtime constants) into mapping programs. The mapping constants are saved as key-value pairs. When we use keys to access mapping constants, if keys are changed then the mappings could be invalid. So there are mapping constants are provided to access the mapping constants.

This article describes the accessing of mapping runtime constants from message mapping, and from java mapping with both APIs (AbstractTransformation, StreamTrasformation) and from xslt mapping.

Message mapping (Container object, GlobalContainer).

Java mapping (AbstractTranformation, StreamTransformation).

XSLT mapping through Java classes (Implementing of AbstractTransformation or

StreamTransformation).

Description of the Example: Simple example is taken for demonstration of accessing mapping constants.

In target message type there is a field called “MessageID” which is populated with the mapping constant

MESSAGE_ID of runtime.

So the agenda here is to get message MESSAGE_ID of message processing at runtime in different mapping

programs.

Source Message

Target Message

Page 4: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 4

1. Accessing Mapping Constants by Message Mapping

Accessing mapping constants in message mapping can be done in two ways; By “Container” object (“getTransformationParameters” method) and by “GlobalContainer” object (“getParameters” method).

Container Object:

public String messageid(Container container) throws StreamTransformationException

{ Map map;

String message = "";

map = container.getTransformationParameters();

message = (String) map.get(StreamTransformationConstants.MESSAGE_ID);

return message;

Global Container Object:

public String messageid(Container container) throws StreamTransformationException { Map map; String message = ""; GlobalContainer gc; gc = container.getGlobalContainer(); map=gc.getParameters(); message=(String) map.get (StreamTransformationConstants.MESSAGE_ID); return message; } Message mapping

Page 5: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 5

Message monitoring

2. Accessing Mapping Constants by Java Mapping

Java mapping can access mapping runtime constants through two different APIs (Netweaver 2004 and 7.0, Netweaver 7.1)

a) Java mapping with “AbstractTransformation” (abstract class)

b) Java mapping with “StreamTrasformationConstants” and “Map” object (interface)

Note: Here Java mapping is pretty simple; we must have seen similar kind of mapping programs in couple of places.

Here the point is accessing mapping constant with help of both available APIs.

Java Code (AbstractTransformation)

There are two classes “InputHeaer” and “OutputHeader” for read and write access of mapping constants. InputHeader to read from header fields of the source message.OutputHeader to set target Header fields.

Note: String message=in.getInputHeader().getMessageID() ; String receiver=in.getInputHeader().getInterface(); String namespace=in.getInputHeader().getInterfaceNamespace(); Likewise there are methods available for all mapping constants.

Complete Java program:

* Created on Jul 6, 2011

* @author Prasad Nemalikanti

*/

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

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

Page 6: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 6

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

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

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

public class MappingConst extends AbstractTransformation

{

String result = "";

public void transform(TransformationInput in,TransformationOutput out) throws

StreamTransformationException

{

//Accessing mapping constants

String message = in.getInputHeader().getMessageId();

InputStream input = in.getInputPayload().getInputStream();

String fresult="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

fresult = fresult.concat("<ns0:MT_StudentsDetails

xmlns:ns0=\"http://mappingconstants.com\">");

try //Reading Input xml file

{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder.parse(input);

traversingXML(doc);

}

catch (Exception e){}

fresult=fresult.concat(result);

fresult=fresult.concat("<MessageID>");

fresult=fresult.concat(message);

fresult=fresult.concat("</MessageID>");

fresult=fresult.concat("</ns0:MT_StudentsDetails>");

try // writing output xml file

{

out.getOutputPayload().getOutputStream().write(fresult.getBytes());

}

catch(IOException e1){}

}

public void traversingXML(Node node)

{

NodeList children = node.getChildNodes();

for(int i=0;i<children.getLength();i++)

{

Node child = children.item(i);

short childType = child.getNodeType();

if(childType==Node.ELEMENT_NODE)

{

String nodeName=child.getNodeName();

String targetNodeName=null;

if(nodeName.equals("Students"))

targetNodeName="Students";

if(nodeName.equals("StudentID"))

targetNodeName="StudentID";

if(nodeName.equals("StudentName"))

Page 7: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 7

targetNodeName="StudentName";

if(nodeName.equals("StudentGrade"))

targetNodeName="StudentGrade";

if(targetNodeName!=null)

result=result.concat("<"+targetNodeName+">");

traversingXML(child);

if(targetNodeName!=null)

result=result.concat("</"+targetNodeName+">");

}

else if(childType==Node.TEXT_NODE)

{

String nodeValue=child.getNodeValue();

result=result.concat(nodeValue);

}

}

}

}

Operation mapping

Message Monitoring

Page 8: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 8

Java Code (StreamTransformation)

/*

* Created on Jul 13, 2011

* @author Prasad Nemalikanti

*/

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import java.util.*;

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

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

public class MappConstStrm implements StreamTransformation

{

private Map par=null;

String result="";

public void setParameter(Map param)

{

par = param;

if(par==null)

par = new HashMap();

}

public void execute(InputStream in,OutputStream out)

{

String messageid="";

String fresult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

fresult = fresult.concat("<ns0:MT_StudentsDetails

xmlns:ns0=\"http://mappingconstants.com\">");

try

{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder.parse(in);

traversingXML(doc);

}

catch(Exception e){ }

fresult = fresult.concat(result);

// Accessing Mapping Constants.

messageid = (String)par.get(StreamTransformationConstants.RECEIVER_NAME);

fresult=fresult.concat("<MessageID>");

fresult=fresult.concat(messageid);

fresult=fresult.concat("</MessageID>");

fresult=fresult.concat("</ns0:MT_StudentsDetails>");

try{

out.write(fresult.getBytes());

}

Page 9: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 9

catch(Exception e){}

}

public void traversingXML(Node node)

{

NodeList children = node.getChildNodes();

for(int i=0;i<children.getLength();i++)

{

Node child = children.item(i);

short childType = child.getNodeType();

if(childType==Node.ELEMENT_NODE)

{

String nodeName=child.getNodeName();

String targetNodeName=null;

if(nodeName.equals("Students"))

targetNodeName="Students";

if(nodeName.equals("StudentID"))

targetNodeName="StudentID";

if(nodeName.equals("StudentName"))

targetNodeName="StudentName";

if(nodeName.equals("StudentGrade"))

targetNodeName="StudentGrade";

if(targetNodeName!=null)

result=result.concat("<"+targetNodeName+">");

traversingXML(child);

if(targetNodeName!=null)

result=result.concat("</"+targetNodeName+">");

}

else if(childType==Node.TEXT_NODE)

{

String nodeValue=child.getNodeValue();

result=result.concat(nodeValue);

}

}

}

}

Page 10: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 10

Operation mapping

Message Monitoring

3. Accessing Mapping Constants by XSLT Mapping

XSLT mapping can access the mapping runtime constants, but it needs Java archive which implements Java mapping APIs.XSLT mapping requires java mapping APIs (AbstractTransformation or StreamTransformation) to access the mapping runtime constants.

There is no need of java mapping, but it just needs a java class which implements either “AbstractTransformation” or use “StreamTransformationConstants”.

So there are two ways that the java methods can access mapping runtime constants.

1. “AbstractTransformation”: - When “AbstractTransformation” is used by XSLT mapping to get mapping constants, “transform” method must be overridden. There is no mapping logic in the “transform” method; just it has a single line which accesses the MessageID.

Page 11: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 11

There is another static method “message” is taken to return required mapping constant to xslt mapping.

XSLT mapping:-

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:const="java:prasad.cons.MapConsXsltTr">

<xsl:template match="/">

<ns0:MT_StudentsDetails xmlns:ns0="http://mappingconstants.com">

<Students>

<xsl:for-each select="ns0:MT_Students/Students">

<xsl:element name="StudentID">

<xsl:value-of select="StudentID"/>

</xsl:element>

<xsl:element name="StudentName">

<xsl:value-of select="StudentName"/>

</xsl:element>

<xsl:element name="StudentGrade">

<xsl:value-of select="StudentGrade"/>

</xsl:element>

</xsl:for-each>

</Students>

<xsl:element name="MessageID">

<xsl:value-of select="const:message()"/>

</xsl:element>

</ns0:MT_StudentsDetails>

</xsl:template>

</xsl:stylesheet>

Java Archive (AbstractTransformation):-

/**

* @author Prasad Babu Nemalikanti

* Created on Jul 20, 2011

*/

package prasad.cons;

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

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

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

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

public class MapConsXsltTr extends AbstractTransformation

{

static String message="";

public void transform(TransformationInput in,TransformationOutput out) throws

StreamTransformationException

{

message = in.getInputHeader().getMessageId();

}

public static String message()

{

return message;

Page 12: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 12

XSLT Archive

Java archive

Message processing (SXMB_MONI)

Page 13: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 13

2.”StreamTransformationConstants”:- XSLT mapping can access mapping constants by triggering java method which uses “StreamTransformationConstants” and “Map” object. This is another way to access mapping constants into xslt mappining program during runtime.

xslt archive:-

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:const="java:prasad.cons.MapConsXslt">

<xsl:param name="inputparam"/>

<xsl:template match="/">

<ns0:MT_StudentsDetails xmlns:ns0="http://mappingconstants.com">

<Students>

<xsl:for-each select="ns0:MT_Students/Students">

<xsl:element name="StudentID">

<xsl:value-of select="StudentID"/>

</xsl:element>

<xsl:element name="StudentName">

<xsl:value-of select="StudentName"/>

</xsl:element>

<xsl:element name="StudentGrade">

<xsl:value-of select="StudentGrade"/>

</xsl:element>

</xsl:for-each>

</Students>

<xsl:element name="MessageID">

<xsl:value-of select="const:messageID($inputparam)"/>

</xsl:element>

</ns0:MT_StudentsDetails>

</xsl:template>

</xsl:stylesheet>

Java Archive (StreamTransformationConstants):-

/**

* @author Prasad Babu Nemalikanti

* Created on Jul 19, 2011

*/

package prasad.cons;

import java.util.Map;

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

public class MapConsXslt {

public static String messageID(Map param)

{ String message =

(String)param.get(StreamTransformationConstants.MESSAGE_ID);

return message;

Page 14: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 14

XSLT archive

Java archive (StreamTransformationConstants)

Message Processing (SXMB_MONI)

Note: To access Mapping runtime constants into xslt mapping, prior java mapping is not required. Whereas java archive (user defined class) is required to access mapping constants with help of “AbstractTransformation” or “StreamTrasformationConstants” and Map object

Page 15: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 15

Related Content

Mapping constants

Mapping constants with XSLT mapping

xslt with java enhancements

For more information, visit the SOA Management homepage.

Page 16: Mapping Constants (Runtime)SAP-XI/PI · PDF fileMapping Constants (Runtime) SAP-XI/PI ... The mapping runtime saves the fields as key-value pairs. Mapping API provides string constants

Mapping Constants (Runtime) SAP-XI/PI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com

© 2011 SAP AG 16

Disclaimer and Liability Notice

This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.

SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk.

SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.