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


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
著作權歸作者所有,轉載請聯系作者獲得授權,並標注“簡書作者”。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM