Show TOC Start of Content Area

This graphic is explained in the accompanying text Example: Using Generated Proxies to Call Function Modules  Locate the document in its SAP Library structure

To demonstrate the use of Java proxies, consider the simple example of calling the ABAP function module BAPI_FLIGHT_GETLIST from within the SAP NetWeaver Developer Studio. You will generate the java classes directly into the package that you are working with, add the relevant jar files to the build path, and then write the code to:

·        Import the relevant packages for the SAP Java Connector and SAP Enterprise Connector.

·        Connect to the SAP System.

·        Create an instance of the generated input class and set the input parameters.

·        Create an instance of the generated proxy class.

·        Supply the proxy instance with the JCO.Client instance.

·        Create an instance of the generated output class and get the output parameters.

·        Disconnect from the SAP System.

To keep it simple, the code in this example does not represent best practice. In particular, you would not normally hard code the logon information.

Prerequisites

You are in the SAP NetWeaver Developer Studio and are familiar with the basic functions.

You have a user in an SAP System that contains the flight data model.

Preparations

       1.      Create a Java project. Call it MyProject.

       2.      Create a package in the project. Call it mypackage.

       3.      Using the SAP Enterprise Connector, generate the proxy classes for the function module BAPI_FLIGHT_GETLIST into your package. Use the name MyProxy_PortType.

       4.      Add these files to the build path:

aii_proxy_rt.jar

aii_util_misc.jar

SAPmdi.jar

sapjco.jar

They can be found under the plugins com.sap.ide.jcb.core\lib\ and com.sap.mw.jco\lib\.

       5.      In your package, create a new class containing a main method. Call the class MyMainClass.

You should now see something like this:

This graphic is explained in the accompanying text

Connecting to the SAP System

To establish a connection to the SAP System, instantiate a JCO.Client object using the JCO.createClient() method. You can then use the object’s connect() and disconnect() methods. Between these two methods, you will later add code for calling the function module, and setting and getting the parameter values.

       1.      Add this code to your main method, replacing the strings with ones appropriate for your system:

JCO.Client jcoclient =

   JCO.createClient(

       "CLIENT",

       "USER",

       "PASSWORD",

       "LANGUAGE",

       "SERVER",

       "SYSTEM",

       "GROUP");

jcoclient.connect();

jcoclient.disconnect();

       2.      Import the com.sap.mw.jco package:

import com.sap.mw.jco.*;

Setting the Input parameters

You need to construct an instance of the generated input class. If the function module has mandatory input parameters, these must also be filled. BAPI_FLIGHT_GETLIST only has optional parameters, but for this example you will enter a value for the airline.

       1.      Add the following code:

Bapi_Flight_Getlist_Input input = new Bapi_Flight_Getlist_Input();

input.setAirline("LH");

Choose an appropriate value for the airline.

Configuring the Proxy and Calling the Function module

You need to create an instance of the generated proxy class, supply the proxy instance with a JCo connection, and get the output from the function module.

       1.      Add the following code:

MyProxy_PortType myproxy = new MyProxy_PortType();

myproxy.messageSpecifier.setJcoClient(jcoclient);

Bapi_Flight_Getlist_Output output =                            myproxy.bapi_Flight_Getlist(input);

       2.      Choose Save.

You will see that you need to handle exceptions.

       3.      Add the following to the main method:

throws ApplicationFaultException, SystemFaultException

       4.      Import the com.sap.aii.proxy.framework.core package:

import com.sap.aii.proxy.framework.core.*;

Using the Returned Values

After the call to the function module, the output instance will contain all the exporting and table parameters of the function module. A returned table is represented in Java as a list.

You can enter the  following code before or after the disconnect.

       1.      Instantiate the list and find out how many objects it contains.

BapisfldatType_List list = output.get_as_listFlight_List();

int listsize = list.size();

       2.      Import the package containing the class BapisfldatType_List.

import mypackage.util.*;

       3.      Add the following code to display the arrival dates and times in the console:

for (int i = 0; i < listsize; i++) {

   BapisfldatType elem = list.getBapisfldatType(i);

   System.out.println(

      "Date: "

         + elem.getArrdate()

         + '\t'

         + "Arrive: "

         + elem.getArrtime());

   }

Executing

       1.      Choose Run Run As Java Application

Depending on the data, you should see something like this:

This graphic is explained in the accompanying text

Complete Code for the Example

Remember to change the logon information. Additional lines have been added to fill all the input parameters.

package mypackage;

 

import mypackage.util.*;

import com.sap.aii.proxy.framework.core.*;

import com.sap.mw.jco.*;

import java.sql.Date;

 

public class MyMainClass {

   public static void main(String[] args)

      throws ApplicationFaultException, SystemFaultException {

 

      JCO.Client jcoclient =

         JCO.createClient(

             "CLIENT",

             "USER",

             "PASSWORD",

             "LANGUAGE",

             "SERVER",

             "SYSTEM",

             "GROUP");

 

      jcoclient.connect();

 

      Bapi_Flight_Getlist_Input input = new Bapi_Flight_Getlist_Input();

 

      BapisfldstType fromAirport = new BapisfldstType();

      BapisfldstType toAirport = new BapisfldstType();

      fromAirport.setAirportid("JFK");

      toAirport.setAirportid("FRA");

 

      BapisfldraType dateRange1 = new BapisfldraType();

      dateRange1.setSign("I");

      dateRange1.setOption("BT");

      dateRange1.setLow(Date.valueOf("2002-10-01"));

      dateRange1.setHigh(Date.valueOf("2003-01-01"));

 

      BapisfldraType dateRange2 = new BapisfldraType();

      dateRange2.setLow(Date.valueOf("2003-06-01"));

      dateRange2.setHigh(Date.valueOf("2003-11-01"));

      dateRange2.setOption("BT");

      dateRange2.setSign("I");

 

      BapisfldraType_List dateRangeList = new BapisfldraType_List();

      dateRangeList.addBapisfldraType(dateRange1);

      dateRangeList.addBapisfldraType(dateRange2);

 

      input.setAirline("LH");

      input.setDestination_From(fromAirport);

      input.setDestination_To(toAirport);

      input.setDate_Range(dateRangeList);

 

      MyProxy_PortType myproxy = new MyProxy_PortType();

      myproxy.messageSpecifier.setJcoClient(jcoclient);

      Bapi_Flight_Getlist_Output output =                                  myproxy.bapi_Flight_Getlist(input);

 

      jcoclient.disconnect();

 

      BapisfldatType_List list = output.get_as_listFlight_List();

      int listsize = list.size();

      for (int i = 0; i < listsize; i++) {

         BapisfldatType elem = list.getBapisfldatType(i);

         System.out.println(

            "Date: "

               + elem.getArrdate()

               + '\t'

               + "Arrive: "

               + elem.getArrtime());

      }

   }

}

 

End of Content Area