Quantcast
Channel: Process Integration (PI) & SOA Middleware
Viewing all articles
Browse latest Browse all 741

Conversion of HTML wrapped excel file into CSV file using java mapping in SAP PI 7.31

$
0
0

Business Requirement:

Purchase requisition information is sent from a partner via Email as a excel attachment(HTML wrapped one). PI has to poll the Email server, retrieve these mails and convert it into CSV files in PI with exactly the same name as the mail attachment.


Solution:

Conversion of HTML wrapped excel file into CSV with dynamic file name configuration can be achieved by using java mapping. This solution avoids file content conversion (CSV format) in the receiver channel. Since it has been maintained in the java mapping itself. Also it avoids creating custom adapter modules. External jar files for HTML content conversion is not required in below java mapping.


Sender Mail Channel Settings:

‘Keep Attachments’ option should be selected in sender communication channel , which is required to retain the attachment. Also check the ‘Set Adapter-Specific Message Attributes’ option to enable the Dynamic Configuration attributes to be available.


MultipartHeaderBean:

MultipartHeaderBean enables us to access the attributes of other payloads that are appended to the XI message as an additional attachment. In the ‘Module’ tab add the MultipartHeaderBean with the following parameters:

Parameter Name

Parameter Value      

requiredHeaders

All

dcNamespace

http://sap.com/xi/XI/System/Mail

Attachment file name will be available in the Dynamic configuration attribute Part[1].Content-Description which will be used in java mapping.


PayloadSwapBean:

We can replace the application payload of the XI message i.e. email content, with another payload that is appended to the XI message i.e. the attachment using payloadswap bean. Add the PayloadSwapBean with the following parameters:

Parameter Name

Parameter Value       

  1. swap.keyName   

Payload-Name

  1. swap.keyValue   

MailAttachment-1

 

 

PayloadZipBean:

We can unzip the attachment payload using payload zip bean. Add the PayloadZipBean with the following parameters:

Parameter Name

Parameter Value       

  1. zip.mode

unzip



All the above mentioned java beans will be configured as below in the sender channel,

 

Java Mapping: (HTML into CSV format)

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

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

import java.util.HashMap;

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

import java.io.BufferedReader;

import java.io.File;

import java.io.InputStreamReader;

 

public class classname implements StreamTransformation{

 

private Map map = null;

private AbstractTrace trace = null;

public void setParameter(Map arg0) {

map = arg0;

if (map == null) {

  1. this.map = new HashMap();

}

}

public void execute(InputStream inputstream, OutputStream outputstream) {

String xmldata= "";

String inputFileName;

String header = "";

String lineItems = "";

Boolean startHeader = false;

Boolean doneHeader = false;

int fieldCount = 0;

int headerFieldCount = 0;

 

try {

 

    //Writing file name dynamically

DynamicConfiguration conf = (DynamicConfiguration)map.get("DynamicConfiguration");


//File name will be available in below attribute

DynamicConfigurationKey keyFileName1 = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/Mail", "Part[1].Content-Disposition");

inputFileName = conf.get(keyFileName1);

inputFileName = inputFileName.replaceAll("\"","");

int k = inputFileName.lastIndexOf("=") + 1;


//Constant value “PR” will be added to file name

String xlsname = "PR_" + inputFileName.substring(k);

String fileName=xlsname.replace("xls.zip", "csv");

DynamicConfigurationKey keyFilename = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName");


//File name has been passed dynamically

conf.put(keyFilename,fileName);

 

//Reading HTML wrapped excel file

BufferedReader br = new BufferedReader(new InputStreamReader(inputstream));

String line = null;

while ((line = br.readLine()) != null) {

if (line.startsWith("<tr>") && startHeader && !doneHeader){

doneHeader=true;

xmldata= xmldata + formatHeaderLine(header);

}

if(line.startsWith("<th>")){

header += line;

headerFieldCount++;

startHeader=true;

}

else if(line.startsWith("<td>") && doneHeader){

if(fieldCount<headerFieldCount){

lineItems += line;

fieldCount++;

if(!line.contains("</td>")){

if ((line = br.readLine()) != null) {

lineItems += line;

}

}

}

if(fieldCount==headerFieldCount){

fieldCount=0;

xmldata= xmldata + formatLine(lineItems);

lineItems = "";

}

}         

}

 

 

xmldata.getBytes();

br.close();

byte by[] = xmldata.getBytes();

outputstream.write(by);

inputstream.close();

outputstream.close();

}

 

 

catch (Exception e)

{

e.printStackTrace();

}

}

 

//Conversion of HTML content into comma separated values

 

static String formatHeaderLine(String line){

String line1 = line.replaceAll("</th><th>", "\"" + "," + "\"");

String line2 = line1.replaceAll("</th>", "\"" + "\n");

return line2.replaceAll("<th>", "\"");

}

static String formatLine(String line){

String line1 = line.replaceAll("</td><td>", "\"" + "," + "\"");

String line2 = line1.replaceAll("</td>", "\"" + "\n");

return line2.replaceAll("<td>", "\"");

}

}

 

Receiver Channel Setting:

ASMA property should be enabled in receiver channel for creating file name dynamically.


 

 

Testing:

Sample mail with PR file as an attachment is sent to the mail account which the sender email adapter is polling in PI.


Input Data:


Dynamic Configuration result:


Dynamic Configuration message attributes was populated in SOAP document as shown below,

Note : Part[1].Content-Description has been set to 20160226_08_28_58PM_Export.xls at runtime.

 

 

Payload was swapped successfully and the attachment data has been set to the main payload as shown below.

 

 

Output Data:


HTML data was converted into CSV format successfully and file was placed in PI directory.



Viewing all articles
Browse latest Browse all 741

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>