SAP接口編程之 JCo3.0 系列(03) : 表參數

Table 作為 exporting parameter

Table parameter 既可以作為 exporting parameter,也可以作為 importing parameter,作為 exporting parameter 比較簡單。以 BAPI_COMPANYCODE_GETLIST 函數為例,該函數無 importing parameter, 使用 table parameter 返回 SAP 系統中包括公司代碼 ID 和公司代碼名稱兩個字段的清單。JCo3.0 中,與表參數 (table parameter) 相關的接口:

  • JCoTable: 對應 table parameter
  • JCoRecordMetaDta: JCoTableJCoStructure 的元數據。

JCoTable 輸出到控制臺

首先編寫一個將 JCoTable 輸出到控制臺的輔助類。

package jco3.jcoUtils;

import com.sap.conn.jco.*;

public class Utils {
    public static void printJcoTable(JCoTable table) {
        printHeader(table);
        printLines(table);
    }

    private static void printHeader(JCoTable table) {
        // JCoRecordMeataData is the meta data of either a structure or a table.
        // Each element describes a field of the structure or table.
        JCoRecordMetaData  tableMeta = table.getRecordMetaData();
        for (int i = 0; i < tableMeta.getFieldCount(); i++){
            System.out.print(
                    String.format("%s\t", tableMeta.getName(i))
            );
        }
        System.out.println(); // new line
    }

    private static void printLines(JCoTable table) {
        for (int i = 0; i < table.getNumRows(); i++){
            // set current row position (starting from 0)
            table.setRow(i);

            // Every row is of type JCoStructure
            // iterate each field in a row
            for (JCoField field : table){
                System.out.print(
                        String.format("%s\t",  field.getValue())
                );
            }
            System.out.println();
        }
    }
}

要點說明

輸出表頭,通過獲取JCoTable的 meta-data,然后使用 meta-data 的 getName() 方法:

JCoTable 每一行都是一個 JCoStructure,可以通過 setRow() 設置指針的位置,然后再遍歷各個 field:

Table 作為 exporting 參數示例

package jco3.demo6;

import com.sap.conn.jco.*;
import jco3.jcoUtils.Utils;
import org.junit.Test;

public class TableAsExportingDemo {
    public JCoTable getCocdList() throws JCoException {
        JCoDestination dest = JCoDestinationManager.getDestination("ECC");
        JCoFunction fm = dest.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST");
        fm.execute(dest);

        JCoTable companies = fm.getTableParameterList().getTable("COMPANYCODE_LIST");

        return companies;
    }

    @Test
    public void testGetCocdList() throws JCoException {
        JCoTable companies = getCocdList();
        Utils.printJcoTable(companies);
    }
}

Table 作為 importing parameter

table 作為輸入參數,需要在代碼中填充 table,方法如下:

someTable.appendRow();
someTable.setValue("FLDNAME", someValue);

RFC_READ_TABLE 函數為例,讀取 spfli (SAP 航空示例數據)表。

package jco3.demo6;

import com.sap.conn.jco.*;
import org.junit.Test;

public class TableAsImportingDemo {
    public JCoTable readTable() throws JCoException {
        JCoDestination dest = JCoDestinationManager.getDestination("ECC");
        JCoFunction fm = dest.getRepository().getFunction("RFC_READ_TABLE");

        // QUERY_TABLE: importing, table name to be queried
        fm.getImportParameterList().setValue("QUERY_TABLE", "SPFLI");

        // DELIMITER: delimiter of output data
        fm.getImportParameterList().setValue("DELIMITER", ",");

        // OPTIONS: Selection entries, valid "WHERE clauses"
        // needing to be populated
        JCoTable options = fm.getTableParameterList().getTable("OPTIONS");

        options.appendRow();
        options.setValue("TEXT", "COUNTRYFR EQ 'US' ");

        options.appendRow();
        options.setValue("TEXT", " OR COUNTRYFR EQ 'DE' ");

        // FIELDS: names to be output in the structure, needing to be specified
        // if we want to restrict, otherwise all fields will be extracted
        String[] outputFields = new String[] {"CARRID", "COUNTRYFR", "CITYFROM", "CITYTO" };
        JCoTable fields = fm.getTableParameterList().getTable("FIELDS");

        int count = outputFields.length;
        fields.appendRows(count);
        for (int i = 0; i < count; i++){
            fields.setRow(i);
            fields.setValue("FIELDNAME", outputFields[i]);
        }

        fm.execute(dest);

        // Get data
        JCoTable data = fm.getTableParameterList().getTable("DATA");

        return data;
    }

    @Test
    public void testReadTable() throws JCoException {
        JCoTable data = readTable();

        for(int i = 0; i < data.getNumRows(); i++){
            data.setRow(i);
            String rowData = data.getValue("WA").toString(); // get value from first column
            String[] values = rowData.split(",");
            for(String item : values) {
                System.out.print(item + "\t");
            }
            System.out.println();
        }
    }
}

在代碼中我們使用了兩種方法來插入 table 的行項目,第一種方法:

第二種方法:

JCoTable 重要方法

源碼

github - sap_interface_jco3

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容