This is a continuation of Server Programming in JCo Part 2. In this post, I will deal with receiving and parsing the actual call from SAP.
In the previous post, the setup of the JCO.Server class was explained. To actually do anything with this class, we have to override the handleRequest() method. This method gets called when ever the SAP system with the proper credentials contacts your server.
In the handleRequest() function, There are 2 important tasks that are almost always included:
- Get the parameters for input, output, and table structures using the JCO.Function class
- Checking the function name to make sure the right code is run
Here is an example of the basic setup:
protected void handleRequest(JCO.Function function) { JCO.ParameterList input = function.getImportParameterList(); JCO.ParameterList output = function.getExportParameterList(); JCO.ParameterList tables = function.getTableParameterList(); if (function.getName().equals("BAPI_CUSTOMER_GETDETAIL")) { //code to retrieve and manipulate data } }
The first task involves the JCO.Function class to extract objects into one of three different that correspond to import, export, and table structures in SAP. The first step is to convert them into the the JCO.ParameterList object. This object encapsulates all the different parameters for input, output and table structures.
For input and output parameters, scalar fields are very easy to set and get, and no conversion to JCO.Field is necessary.
input.getString("scalar_name"); //getting string input.setValue("value","scalar_name") //setting string
For structures, things get a bit more difficult. First, we need to get call our parameter list and get our structure with a JCO.Structure object and then use the basic set and get methods available to read and store data:
JCO.Structure js = input.getStructure("structure_name"); js.getString("name_of_field_in_structure"); js.setValue("value","name_of_field_in_structure");
For tables, It gets much more complicated. First, we need to get our table data from the JCO.ParameterList by calling the “getTable(String tablename)” method.
JCO.Table jtab = tables.getTable("tablename");
Once the JCO.Table object has the table reference desired, there are several methods to use to manipulate the data. The biggest hurdle in using the JCO.Table object is the internal row pointer system that is used. For instance, if you have an empty table, the first step to add data is to use the “appendRow()” method to create the row and set the internal row pointer to that row. Then, you can set the value of field in that row directly:
jtab.appendRow(); jtab.setValue("fieldvalue","fieldname"); jtab.setValue("fieldvalue","fieldname2");
It is possible to create more than one row at a time using the “appendRows(int rows)” method. In this case the internal table is set at first row that was appended, and you can move to the next row with “nextRow()” method. The “nextRow()” method also returns a boolean value that checks for the end of the dataset, so you can use it to loop through a table. Its also possible to access the row directly. The internal row pointer starts at 0 for the first row. I’ve listed out the most commonly used table functions below:
- isEmpty() – Returns true if the table is empty
- nextRow() – moves the pointer to the next row, returns false if at the end of the dataset
- previousRow() – moves the pointer to the previous row, returns false of at the beginning of the dataset
- isFirstRow() – Returns true of the pointer is on the first row
- firstRow() – Moves the pointer to the first row of the dataset
- isLastRow() – Returns true if the pointer is on last row
- lastRow() – Moves the pointer to the last row of the dataset
- getNumRows() – Returns the number of rows in the dataset
- getRow() – Returns the position of the row pointer
- insertRow(int row) – Inserts a row at the specified location
- deleteRow(int row) – Deletes a row at the specified location
- deleteAllRows() – Deletes all rows in the dataset
- setRow(int row) – Sets the row pointer to specified location
The next blog will tie all this information together and cover error handling.