Skip to Content

Requirement: File needs to be dropped on a particular directory on the File Server based on the filename.

Description: The filename is a hexadecimal number from which the first two digits are extracted and then converted into its ACII character to determine the directory on the server.

For example:

304A30433…………13232, here the first two characters being 30 the ACII value for the same shall be 0.

414B42………………45325, here the first two characters being 41 the ASCII value shall be A.

The first file to be dropped into the folder “0” and the latter in “A”.

 

Dynamic Configuration for retrieving the filename is used .First two characters of the filename are then passed to the method to convert hexadecimal to ASCII character and this result in determining the directory.

Pre-Requisite:Dynamic Configuration attribute Filename is checked on the Advanced tab of the sender adapter & Directory is checked on the receiver adapter.

Code:

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.HashMap;

import java.util.Map;

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

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

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

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

public class determineDirectory implements StreamTransformation

{

   private Map param = null;

  public void setParameter(Map map)

  {

    param= map;

    if(param ==null)

    param = new HashMap();

   }

public void execute(InputStream in, OutputStream out)throws StreamTransformationException

{

  //Filename in the sender adapter is retreived and need to pass the directory name dynamically.The

   DynamicConfiguration dynamicConfiguration = (DynamicConfiguration)param.get(“DynamicConfiguration”);

   DynamicConfigurationKey directorykey = DynamicConfigurationKey.create(“http://sap.com/xi/XI/System/File“,”Directory”);      

   String directoryIn = dynamicConfiguration.get(directorykey);

   DynamicConfiguration dynamicConfiguration1 = (DynamicConfiguration)param.get(“DynamicConfiguration”);

   DynamicConfigurationKey filenamekey = DynamicConfigurationKey.create(“http://sap.com/xi/XI/System/File“,”FileName”);

   String filenameIn=dynamicConfiguration1.get(filenamekey);

  //filenametemp variable of type String to store the first two characters of the filename retrieved using Dynamic Configuration.

  String filenametemp=filenameIn.substring(0, 2);

  //Method convertHexToASCII is called and the result is the desired directory name.

    DirectoryIn=convertHexToASCII (filenametemp);

//The directory determined is passed onto the Dynamic Configuration directory variable.

   dynamicConfiguration.put(directorykey, directoryIn);

  //The payload need not be changed hence it is passed as it is.

      copyPayload(in,out);

}

   private void copyPayload(InputStream in, OutputStream out) throws StreamTransformationException

  {

    int c;

    try

   {

     c = in.read();

     while (c != -1)

    {

       out.write(c);

       c = in.read();

     }

   }

   catch (IOException e)

{

  throw new StreamTransformationException(e.getMessage());

}

}

public String convertHexToASCII(String filenametemp)

  {

   StringBuilder sb = new StringBuilder();

   StringBuilder temp = new StringBuilder();

   

   for( int i=0;i<filenametemp.length()-1; i+=2 )

   {

     //grab the filenametemp in pairs              

    String output = filenametemp.substring(i,(i + 2));

    //convert filenametemp to decimal

    int decimal = Integer.parseInt(output,16);

    //convert the decimal to character

     sb.append((char)decimal);

    temp.append(decimal);

   }

   //return the ASCII character which becomes the directory.          

        return sb.toString();

}

}

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply