Thursday 23 February 2017

SAP PI/PO: Java Program to read the input file and rename output file based on validation


Requirement:

Based on the input folder and filename the output filename need to be modified.
This is passthrough interface but when we have more than 5 bank and each bank has 20 codes with 3 prefix (S,N,P)then we need to create 300 flows. This takes more time to develop it. So for reducing it we used the below java mapping code to achieve this requirement.

Eg:

In Input folder name: /home/bankdata/invoices/testin/ICICIBank or /home/bankdata/invoices/testin/HDFCBank

Filename – S_1234.txt,N_3433.txt,P_2354.txt
This S_1234 code present for all banks with different data.

In Output folder name: /home/bankdata/invoices/testin

Output file name: TECH_(bank name)_PSCT_ (code)_timestamp. extension(.csv)

Value Mapping: 
If S_1234.txt, N_3433.txt,P_2354.txt

Source value          Target value
ICICIBank+S   -     TECH_ICICIBank_PSTT_1234_20170223-120123.csv


ICICIBank+N   -     TECH_ICICIBank_PNTT_3433_20170223-120123.csv
ICICIBank+P   -     TECH_ICICIBank_PPTT_2354_20170223-120123.csv
HDFCBank+S   -     TECH_HDFCBank_PSTT_1234_20170223-120123.csv
etc......

Java Mapping Program Code:

package dynamicfilename;

import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.value.api.*;

public class dynamicfile extends AbstractTransformation
{
 private static final String String = null;

public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)                                                                        throws StreamTransformationException
{
  try
  {
   InputStream inputstream = transformationInput.getInputPayload().getInputStream();
   OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
   Map mapParameters = (Map) transformationInput.getInputHeader().getAll();
   // a) Set Output File name
   mapParameters.put(DynamicConfigurationKey.create("http://sap.com/xi/XI/Dynamic",
                                                       StreamTransformationConstants.DYNAMIC_CONFIGURATION),"");
   DynamicConfiguration conf = (DynamicConfiguration) mapParameters.get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
   DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName");
   DynamicConfigurationKey key1 = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "Directory");
   String filename = conf.get(key);
   String directory = conf.get(key1);
   String bankname = directory.substring(31); //Bank name
   String filestart = filename.substring(0,1); //filename start character
   String filename_bank_concat = bankname+"+"+filestart;
  
   String context = "http://sap.com/xi/XI";
   String senderAgency = "ECC";
        String senderScheme = "Directory";
        String receiverAgency = "Bank";
        String receiverScheme = "Filename";
        
        IFIdentifier source = XIVMFactory.newIdentifier(context, senderAgency, senderScheme);
     IFIdentifier target = XIVMFactory.newIdentifier(context, receiverAgency , receiverScheme);
        IFRequest request = XIVMFactory.newRequest(source,target,filename_bank_concat);
        String destvalue = "";
      IFResponse response = XIVMService.executeMapping(request);
           String[] targetValues = response.getTargetValues();
           // take first value of result
      if(targetValues.length>0)
      destvalue= targetValues[0];
     
      String code = filename.substring(2,6);     // output payment code 7570
      DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-hhmmss");
      conf.put(key, (destvalue+code+"_"+dateFormat.format(new Date())+".csv"));
      conf.put(key1, (“/home/bankdata/invoices/testin/"));
   //copy the content of file
   byte[] b = new byte[inputstream.available()];
   inputstream.read(b);
   outputstream.write(b);
  }
  catch (Exception exception)
  {
   getTrace().addDebugMessage(exception.getMessage());
   throw new StreamTransformationException(exception.toString());
  }
 }
}








SAP PI/PO: Java Program to read the content from input file to output file


The below java code is to copy the data from the input file and paste the same data to output file.

package dynamicfilename;

import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.value.api.*;

public class dynamicfile extends AbstractTransformation
{
 private static final String String = null;

public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)                                                                        throws StreamTransformationException
{
  try
  {
   InputStream inputstream = transformationInput.getInputPayload().getInputStream();
   OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
   byte[] b = new byte[inputstream.available()];
   inputstream.read(b);
   outputstream.write(b);
  }
  catch (Exception exception)
  {
   getTrace().addDebugMessage(exception.getMessage());
   throw new StreamTransformationException(exception.toString());
  }
 }
}





SAP PI/PO: Java Program to fetch the values from value mapping table


The below java code in ESR will take the data from the value mapping table maintained in ID

String context = "http://sap.com/xi/XI";
   String senderAgency = "ECC";
        String senderScheme = "Directory";
        String receiverAgency = "Bank";
        String receiverScheme = "Filename";
        
        IFIdentifier source = XIVMFactory.newIdentifier(context, senderAgency, senderScheme);
     IFIdentifier target = XIVMFactory.newIdentifier(context, receiverAgency , receiverScheme);
        IFRequest request = XIVMFactory.newRequest(source,target,filename_bank_concat);
        String destvalue = "";
      IFResponse response = XIVMService.executeMapping(request);
           String[] targetValues = response.getTargetValues();
           // take first value of result
      if(targetValues.length>0)

      destvalue= targetValues[0];

You can use the destvalue and perform your logic.