Table參數作為export parameter
BAPI_COMPANYCODE_GETDETAIL
是一個適合演示的函數,沒有import paramter參數,調用后COMPANYCODE_GETDETAIL
表參數返回SAP系統中所有公司代碼的清單。只包括公司代碼ID和公司代碼名稱兩個字段。
JCo中,與表參數相關的兩個接口是JCoTable
和JCoRecordMetaDta
, JCoTable
就是RFM中tabl參數,而JCoRecordMetaDta
是JCoTable
或JCoStructure
的元數據。
在.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
著作權歸作者所有,轉載請聯系作者獲得授權,並標注“簡書作者”。
原文鏈接:http://www.jianshu.com/p/a088510cf965
著作權歸作者所有,轉載請聯系作者獲得授權,並標注“簡書作者”。