SAP接口编程 之 JCo3.0系列(03) : Table参数

时间:2024-02-15 18:29:17

Table参数作为export parameter

BAPI_COMPANYCODE_GETDETAIL是一个适合演示的函数,没有import paramter参数,调用后COMPANYCODE_GETDETAIL 表参数返回SAP系统中所有公司代码的清单。只包括公司代码ID和公司代码名称两个字段。

JCo中,与表参数相关的两个接口是JCoTableJCoRecordMetaDta, JCoTable就是RFM中tabl参数,而JCoRecordMetaDtaJCoTableJCoStructure的元数据。

在.net环境中,我喜欢将IRfcTable转换成DataTable,但Java没有类似的数据结构,所以决定直接在方法中传递JCoTable算了。但为了方便显示,可以考虑使用一个通用代码进行输出:

package jco3.utils;

import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoRecordMetaData;
import com.sap.conn.jco.JCoTable;

public class JCoUtils
{
    public static void printJCoTable(JCoTable jcoTable)
    {
        // header

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

        // line items

        for(int i = 0; i < jcoTable.getNumRows(); i++){
            // Sets the row pointer to the specified position(beginning from zero)
            jcoTable.setRow(i);

            // Each line is of type JCoStructure
            for(JCoField fld : jcoTable){
                System.out.print(String.format("%s\t", fld.getValue()));
            }
            System.out.println();
        }
    }
}

要点说明

对JCoTable,输出表头和行项目。表头通过获取JCoTable的meta-data,然后使用meta-data的getName()方法。

JCoRecordMetaData tableMeta = jcoTable.getRecordMetaData();        
for(int i = 0; i < tableMeta.getFieldCount(); i++){
      System.out.print(String.format("%s\t", tableMeta.getName(i)));                    
}

JCoTable每一行都是一个JCoStructure,可以通过setRow()设置指针的位置,然后再遍历各个field:

        for(int i = 0; i < jcoTable.getNumRows(); i++){
            // Sets the row pointer to the specified position(beginning from zero)
            jcoTable.setRow(i);

            // Each line is of type JCoStructure
            for(JCoField fld : jcoTable){
                System.out.print(String.format("%s\t", fld.getValue()));
            }
            System.out.println();
        }

完成输出之后,接下来就是RFM调用:

package jco3.demo5;

import org.junit.Test;
import com.sap.conn.jco.*;
import jco3.utils.JCoUtils;

public class JCoTableDemo
{
    public JCoTable getCocdList() throws JCoException
    {
        /**
         * Get company code list in SAP
         * using BAPI BAPI_COMPANYCODE_GETLIST.
         * 
         * Since JCoTable is rather flexible, we simply use
         * this interface as return value
         */

        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 printCompanies() throws JCoException
    {
        JCoTable companies = this.getCocdList();
        JCoUtils.printJCoTable(companies);
    }
}

Table参数作为import parameter

table作为输入参数,主要解决填充table的问题,基本模式如下:

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

以RFC_READ_TABLE为例,读取SAP USR04表。

package jco3.demo5;

import org.junit.Test;
import com.sap.conn.jco.*;
import jco3.utils.JCoUtils;

public class JCoTableAsImport
{    
    public JCoTable readTable() throws JCoException
    {
        /**
         * Shows how to process JCoTable (as importing)
         */

        JCoDestination dest = JCoDestinationManager.getDestination("ECC");
        JCoFunction fm = dest.getRepository().getFunction("RFC_READ_TABLE");

        // table we want to query is USR04
        // which is user authorization table in SAP
        fm.getImportParameterList().setValue("QUERY_TABLE", "USR04");

        // output data will be delimited by comma
        fm.getImportParameterList().setValue("DELIMITER", ",");

        // processing table parameters
        JCoTable options = fm.getTableParameterList().getTable("OPTIONS");
        // modification date >= 2012.01.01 and <= 2015.12.31
        options.appendRow();
        options.setValue("TEXT", "MODDA GE \'20120101\' ");
        options.appendRow();
        options.setValue("TEXT", "AND MODDA LE \'20151231\' ");

        // We only care about fields of [user id] and [modification date]        
        String[] outputFields = new String[] {"BNAME", "MODDA"};
        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);

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

        return data;
    }

    @Test
    public void printUsers() throws JCoException
    {
        JCoTable users = this.readTable();
        JCoUtils.printJCoTable(users);
    }
}

在代码中我们使用了两种方法来插入table的行项目,第一种方法:

JCoTable options = fm.getTableParameterList().getTable("OPTIONS");
// modification date >= 2012.01.01 and <= 2015.12.31
options.appendRow();
options.setValue("TEXT", "MODDA GE \'20120101\' ");
options.appendRow();
options.setValue("TEXT", "AND MODDA LE \'20151231\' ");

第二种方法:

String[] outputFields = new String[] {"BNAME", "MODDA"};
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]);            
}

JCoTable重要方法总结


jcoTable_methods.gif



文/StoneWM(简书作者)
原文链接:http://www.jianshu.com/p/a088510cf965
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。