Skip to end of metadata
Go to start of metadata

In a passthrough File to File scenarios (i.e no IR objects involved), there are quite a few times when we have to create a dynamic file names on a receiver (FTP/NFS) application side with some part of source file name.
Take an instance where source is sending a file having name as "Source_123_TimeStamp.txt" and on target Side we want something like "Target_123_TimeStamp.txt". So in order to achieve this we have to do some work arounds.
Here i am jotting one of the ways in which we can achieve this requirement by using Custom Adapter module.
 

Note: This code is very generic you can tweak it to accomplish your requirement. 

package com.DynamicName;

import java.rmi.RemoteException;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
import com.sap.aii.af.lib.mp.module.ModuleContext;
import com.sap.aii.af.lib.mp.module.ModuleData;
import com.sap.aii.af.lib.mp.module.ModuleException;
import com.sap.aii.af.service.auditlog.Audit;
import com.sap.engine.interfaces.messaging.api.Message;
import com.sap.engine.interfaces.messaging.api.MessageKey;
import com.sap.engine.interfaces.messaging.api.MessagePropertyKey;
import com.sap.engine.interfaces.messaging.api.XMLPayload;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
import java.util.Calendar;
import java.text.SimpleDateFormat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.io.InputStream;




/**
 * @author AMISRIVA
 *
 */
public class DynamicNameBean implements SessionBean, TimedObject {

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbPassivate()
	 */
	private SessionContext myContext;
	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbRemove()
	 */
	public void ejbRemove() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
	 */
	public void setSessionContext(SessionContext context) throws EJBException,
	RemoteException {
		myContext = context;
	}

	/* (non-Javadoc)
	 * @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)
	 */
	public void ejbTimeout(Timer arg0) {
		// TODO Auto-generated method stub

	}

	public void ejbCreate() throws javax.ejb.CreateException {

	}

	public ModuleData process(ModuleContext mc,ModuleData inputModuleData)throws ModuleException {
		Object obj = null;
		Message msg = null;
		MessageKey amk = null;

		String filename = null;

		try{
			// Retrieves the current principle data, usually the message , Return type is Object
			obj = inputModuleData.getPrincipalData();
			// A Message is what an application sends or receives when interacting with the Messaging System.
			msg = (Message) obj;
			// MessageKey consists of a message Id string and the MessageDirection
			amk = new MessageKey(msg.getMessageId(),msg.getMessageDirection());
			// Audit log message will appear in MDT of Channel Monitoring
			Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Dynamic File name Module called");
			// Returns the main document as XMLPayload
			XMLPayload xpld = msg.getDocument();
			//Reading the file name from message header
     		MessagePropertyKey mpk = new MessagePropertyKey("FileName","http://sap.com/xi/XI/System/File");
			filename = msg.getMessageProperty(mpk);
			Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "filename is" +filename );

			if(filename == null || filename.length() == 0)
			{
				Audit.addAuditLogEntry(amk, AuditLogStatus.WARNING, "Message Property FileName does not exist");
				filename = "Default";
			}

			String [] temp = filename.split("_");

			//setting the current date and time
			 Calendar currentDate = Calendar.getInstance();
			  SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
			  String CurrentDate = formatter.format(currentDate.getTime());

			  String output = temp[1] + "_" + CurrentDate + ".txt";

			  //setting the new file name in the message header
			msg.setMessageProperty("http://sap.com/xi/XI/System/File","FileName",output);
     		 Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "filename is" +output );
			 Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "File name is Successfully set");

				    // Sets the principle data that represents usually the message to be processed
		    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Message Successfully updated");
		    inputModuleData.setPrincipalData(msg);

		}catch(Exception e) {
			Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR,"Module Exception caught");
			ModuleException me = new ModuleException(e);
			throw me;
		}
		return inputModuleData;
	}



}

Once done with coding part you have to deploy this custom module on PI server (there are many links/blogs available regarding the same).

Setting in PI communication channels:
1) File sender adapter: Check ASMA (FileName) in sender adapter.
2) File Receiver adapter: under module tab add your custom adapter module created (basically its JNDI name).
3) File receiver adapter: Check ASMA(FileName) in file receiver adapter.