Part 2: Generating metadata for an RFC structure

Working with SAP's Java Connector, JCo? Then you should check out this collection of handy utilities for working with RFC/BAPI interfaces when doing development with JCo. These truly are development utilities in the sense that their intent is to provide generic support for almost any programming effort that utilizes JCo to interface with SAP.

Metadata, what the heck do you mean by metadata? In this context, metadata simply refers to the structural definition of a specific RFC. This includes the parameters, structures, and tables required to call the RFC as well as the fields included within each.

Often, I find it useful to be able to automatically generate an object-based structure for an RFC interface. This means that, unlike an XML or plain text view, the object structure of the RFC can easily be used to create other formats or views of the structure. By modifying this utility you could (for example) output a Java bean interface, an MQ message format, or even an XML document type definition for any RFC/BAPI interface in SAP. In order to keep things simple, the included source code generates and displays a plain text view of any RFC you care to specify.

Note that the output formatting in the formatRfc() method is pretty clumsy and would normally be replaced with something more elegant. The intent was to create human-readable output of the RFC interface in a plaintext file.

import java.io.FileWriter;
import com.sap.mw.JCo.*;

public class RetrieveRFC {
 static final String[][] sapParams = { {"client", "000"}, {"user", "myUsername"},
 {"passwd", "myPassword"}, {"lang", "en"}, {"ashost", "mySapHost"}, {"sysnr", "00"}};
 static final String interfaceName = "RFC_SYSTEM_INFO";
    static final String filePath = "c:/dev/interface.rfc";
 public static void main(String[] args) {
  try {
   JCo.Client connection = JCo.createClient(sapParams);
   connection.connect();
   IRepository repository = new JCo.Repository("SAPRep", connection);
   JCo.Function interfaceFunction = 
repository.getFunctionTemplate("RFC_GET_FUNCTION_INTERFACE").getFunction();
   interfaceFunction.getImportParameterList().setValue(interfaceName, "FUNCNAME");
   connection.execute(interfaceFunction);
   JCo.Function structFunction = 
repository.getFunctionTemplate("RFC_GET_STRUCTURE_DEFINITION").getFunction();
   JCo.Table interfaceTab = interfaceFunction.getTableParameterList().getTable("PARAMS");
   JCo.Table structTab;
      FileWriter out = new FileWriter(filePath, false);
      out.write("Type" + "/t/t/t");
      out.write("Name" + "/t/t/t/t/t");
      out.write("Description" + "/t/t/t/t");
      out.write("/n");      
   while (interfaceTab.nextRow()) {
  structFunction.getImportParameterList().setValue((String)interfaceTab.getValue("TABNAME"), "TABNAME");
  connection.execute(structFunction);
  structTab = structFunction.getTableParameterList().getTable("FIELDS");
  formatRFC(interfaceTab, structTab, out);
   }
  out.close(); 
  } catch (Exception ex) {ex.printStackTrace();}
     System.out.println("Output written to " + filePath);
 }
 private static void formatRFC(JCo.Table interfaceTab, JCo.Table structTab, FileWriter out) {
   try {
 if ("I".equals((String)interfaceTab.getValue("PARAMCLASS"))) {
  out.write("Parameter" + "/t/t" +
    (String)interfaceTab.getValue("PARAMETER"));
        if (((String)interfaceTab.getValue("PARAMETER")).length() < 18)
            out.write("/t/t/t");
        else
            out.write("/t/t");        
  out.write((String)interfaceTab.getValue("PARAMTEXT") + "/t/t/t");
  out.write("/n");
 }
 if ("T".equals((String)interfaceTab.getValue("PARAMCLASS")) ||
  "E".equals((String)interfaceTab.getValue("PARAMCLASS"))) {
 if ("T".equals((String)interfaceTab.getValue("PARAMCLASS")))
    out.write("Table/t/t/t" + (String)interfaceTab.getValue("PARAMETER"));
 else
    out.write("Structure/t/t" + (String)interfaceTab.getValue("PARAMETER"));
 if (((String)interfaceTab.getValue("PARAMETER")).length() < 18)
  out.write("/t/t/t");
 else
  out.write("/t/t");
 out.write((String)interfaceTab.getValue("PARAMTEXT") + "/n");
 while (structTab.nextRow()) {
  out.write("Field/t/t/t" + (String) structTab.getValue("FIELDNAME") + "/n");
 }
 }
 out.write("/n");
   } catch (Exception ex) {ex.printStackTrace();}
  }
}
The key to this utility is the use of two specific RFCs that allow the application to retrieve both function module and structure definitions. The first step is to pass the name of the RFC/BAPI to RFC_GET_FUNCTION_INTERFACE. This retrieves a list of all parameters, structures, and tables used for this interface. The next step is to call RFC_GET_STRUCTURE_DEFINITION for each table and structure in the RFC/BAPI. This will return a list of the fields and proper fieldnames that comprise a given structure or table. Once the structure definitions have been retrieved, the application then writes them out in the specified (in this case, plain text) format.

THE JCO UTILITY COOKBOOK

 Home: Introduction
 Part 1: Writing an RFC structure to XML
  Part 2: Generating metadata for an RFC structure
 Part 3: Load testing SAP with JCo
 Part 4: Creating a serialized RFC interface
 Review: Conclusion and more resources
This was first published in August 2005

Dig deeper on SAP ABAP

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close