Monday, 23 October 2017

HTTP PATCH method is not available in HCI as of now:

This interface is just REST API from Oracle system that HCI does not support PATCH method and how to resolve it.

IMG 002:

Data in JSON format:

IMG 003

Fields to be updated:

IMG 004

USE POST method:


IMG 005


Hope this solves your issue.



JSON Code for conversion of String to Integer/ Remove double quotes from JSON:

All input data will consider as the String after converting from the XML to JSON, as the output sends to database which supports only without double quoted for the integer. This can be achieved by the below code

IMG:001

Add the groovy script after the XML to JSON Converter then it would work.

import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.json.*
def Message processData(Message message)
{
def body = message.getBody(java.lang.String) as String;
def jsonSlurper = new JsonSlurper();
def jsonDataObject = jsonSlurper.parseText(body);

//Convert the JSON String to Int
jsonDataObject.Contact.id = convertToInt(jsonDataObject.Contact.id);
jsonDataObject.assignedTo.account.id = convertToInt(jsonDataObject.assignedTo.account.id);
jsonDataObject.assignedTo.staff.id = convertToInt(jsonDataObject.assignedTo.staff.id);
jsonDataObject.c.apf = convertToBoolean(jsonDataObject.c.apf);

//Integer conversion  
message.setBody(new JsonBuilder(jsonDataObject).toPrettyString())
return message
}
   static Object convertToInt(Object inputValue){
        if(inputValue.isNumber()){
          return inputValue.toInteger();
        }else{
          return inputValue;
        }
   }}

//Boolean Conversion
static Object convertToBoolean(Object inputValue){
        if(inputValue.equals("true")){
          return true;
        }else{
          return false;
        }

Input: "id":"3261390"
Output: "id":3261390

Issue fix:
If there is no value for the fields then it could not handle that, throws the error eg: ” property id : line 20 it cannot convert null value”

Instead of

jsonDataObject.assignedTo.account.id = convertToInt(jsonDataObject.assignedTo.account.id);

Add the below code:

if(jsonDataObject.assignedTo!= null)
{if(jsonDataObject.assignedTo.account.id!=null)
{  jsonDataObject.assignedTo.account.id = convertToInt(jsonDataObject.assignedTo.account.id);}}




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.