Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
INTRODUCTION:

 

Getting new experience while writing in SCN, its my second blog so please bear with me, as we all improve with our experience. :smile:

Requirement:

Requirement was to map the INVOICE IDoc to  First Target Structure and then create a New target message type which contains only three fields. Value of first two field should be taken from First Target Structure and the third field should contain entire payload (First Target Structure)  encoded in Base64.

Then we need to pass Target structure to third party.So to summaries we have below requirement.



 

Changes done in ESR:

  • Create a two message mapping first message mapping will map IDOC based on the required conditions.




 

  • Create Java Mapping which has input as the target of first mapping and it will create the Target message Type as per the third party.


 

  • Assign the below jar filed and assign in imported Archive so that can be used in operation mapping.


Below is the Java Code which is used:

 
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

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.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class Base64Encoding extends AbstractTransformation {

/*Main method is used only for Testing purpose as PI compiler looks for Transform method if required it can be comment before deploying in PI*/
public static void main(String agrs[]) throws StreamTransformationException
{

try
{
FileInputStream inStream = new FileInputStream ("C:\\Users\\Newuser\\Desktop\\Input.xml"); //INPUT FILE (PAYLOAD)
FileInputStream ins = new FileInputStream ("C:\\Users\\Newuser\\Desktop\\Input.xml");
FileOutputStream outStream = new FileOutputStream ("C:\\Users\\Newuser\\Desktop\\Output.xml");

Base64Encoding mapping = new Base64Encoding();
mapping.execute(inStream,ins, outStream);
}
catch (Exception e) {

e.printStackTrace();
}
}

public void execute(InputStream arg0,InputStream ins, OutputStream arg1) throws IOException{
try {

/*Creating a DOM document "doc" to read entire payload*/

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
Document doc;
dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(arg0);
doc.getDocumentElement().normalize();

/*Searching for the Node "DeliveryInfo" in source payload*/
NodeList nodeList = doc.getElementsByTagName("DeliveryInfo");
Node node = nodeList.item(0);
Element eElement = (Element) node;

/*Creating a DOM document "document" to creaet target payload*/
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();

/*Below code create Namespace for target message */

document.setXmlVersion("1.0");/*****set the XML Version**********/
Element Namespace= document.createElementNS("http://mycompany.com/pi/mapping/INVOIC", "ns0:MT_INVOIC_SOA_Request");
document.appendChild(Namespace);

/*This will create the xml structure for target message type
and will get the values of required nodes from source payload to assign the same in Target structure*/

Element Element1 = document.createElement("BillerID");
Namespace.appendChild(Element1);
Element Element2 = document.createElement("TransactionID");
Namespace.appendChild(Element2);
Element Element3 = document.createElement("Data");
Namespace.appendChild(Element3);
Element1.setTextContent(eElement.getElementsByTagName("BillerID").item(0).getTextContent());
Element2.setTextContent(eElement.getElementsByTagName("TransactionID").item(0).getTextContent());

/*This will read the entire source payload and encode in Base64
and same will be assign to new field to passs in target structure*/

ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[ins.read()];

int len;
while ((len = ins.read(buffer)) > -1 ) {
baos.write(buffer, 0, len);
}
baos.flush();
String Payload= new String(baos.toByteArray());

/*This will remove the Message Type Node from the Payload befor encoding the same in Base64*/

String removeNodes = Payload.substring(0,Payload.indexOf("<ns0:MT_INVOIC_SAP_Request xmlns:ns0=\"http://eaton.com/pi/OTTO_FISCHER/INVOIC\">")) + Payload.substring(Payload.indexOf("<ns0:MT_INVOIC_SAP_Request xmlns:ns0=\"http://mycompany.com/pi/mapping/INVOIC\">") + ("<ns0:MT_INVOIC_SAP_Request xmlns:ns0=\"http://mycompany.com/pi/mapping/INVOIC\">>".length()-1) , Payload.indexOf("</ns0:MT_INVOIC_SAP_Request>"));


String encodedString= DatatypeConverter.printBase64Binary(removeNodes.getBytes());
Element3.setTextContent(encodedString);

/* Assigning the created target message to "TransformationOutput"*/
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(arg1);
transformer.transform(domSource, streamResult);

arg1.flush();
arg1.close();
}
catch (Exception e) {
e.printStackTrace();
}

}
/* The transform method which must be implemented */
public void transform(TransformationInput in, TransformationOutput out)
throws StreamTransformationException {
try {
execute(in.getInputPayload().getInputStream(),in.getInputPayload().getInputStream(),out.getOutputPayload().getOutputStream());
} catch (IOException e) {

e.printStackTrace();
}

}
}

 

Results:

For testing purpose we are using operation mapping we have step 1-->Step 2, so IDOC is directly converted to Target Structure.

Value in field Data Contains the entire payload encoded in Base64, if required same can be checked on base64decode.org.



 

 
2 Comments
Labels in this area