Java, RFC bağlantısı ile ABAP SAP'den veri alma ve gönderme kodu kullanımı örneği (Java, getting and fetching data from ABAP SAP with RFC connection usage example)

IMPORTING :Fonksiyona göndereceğiniz değerleri burada işliyoruz. EXPORTING :Fonksiyondan alacağımız değerleri burada işliyoruz. STRUCTURE :Fonksiyona göndereceğiniz ve fonksiyondan alacağımız yapı değerleri burada işliyoruz. STRUCTURE’ı kendi için de IMPORTING STRUCTURE ve EXPORTING STRUCTURE olarak ta ikiye bölüp düşünebiliriz. TABLES :Fonksiyona göndereceğiniz ve fonksiyondan alacağımız tablo değerleri burada işliyoruz. TABLES’ı kendi için de IMPORTING TABLES ve EXPORTING TABLES olarak ta ikiye bölüp düşünebiliriz. IMPORTING bölümü için aşağıdaki gibi “fonksiyon” örneğimizi çağırıp bu örneğimizin “getImportParameterList()” özelliğini çağırıp bu özelliğin “setValue” metodunu çağırıp parametre değerimizi ve import değerimizin RFC’deki adını aşağıdaki gibi yazıyoruz. [code language=”java”] // importing direct function.getImportParameterList().setValue("myParam01", "IV-RFC-IMPORT-PARAMETER_NAME_01"); // CHAR // importing non direct myImpParamList = function.getImportParameterList(); myImpParamList.setValue("myParam11", "IV-RFC-IMPORT-PARAMETER_NAME_11"); // CHAR myImpParamList.setValue("myParam12", "IV-RFC-IMPORT-PARAMETER_NAME_12"); // CHAR [/code] IMPORTING STRUCTURE, STRUCTURE bölümü için gönderme işleminde aşağıda gibi ABAP, CHAR ve DATS değerleri için yapı(structure) parametrelerimizi göndermek için hazırlıyoruz. [code language=”java”] // importing structure myImpStrct = function.getTableParameterList().getTable("IS-RFC-TABLES-PARAMETER_NAME"); myImpStrct.setValue("myTableParam", "STRCUTURE_PARAM_CHAR"); // CHAR myImpStrct.setValue("20180328", "STRCUTURE_PARAM_DATS"); // [/code] IMPORTING TABLES, TABLES bölümü için gönderme işleminde aşağıda gibi ABAP, CHAR ve DATS değerleri için tablo parametrelerimizi göndermek için hazırlıyoruz. [code language=”java”] // importing tables myImpTbl = function.getTableParameterList().getTable("IT-RFC-TABLES-PARAMETER_NAME"); myImpTbl.appendRow(); myImpTbl.setValue("myTableParam", "TABLE_PARAM_CHAR"); // CHAR myImpTbl.setValue("20170921", "TABLE_PARAM_DATS"); // [/code] Parametre değerlerimizi doldurduktan sonra fonksiyonumuzu aşağıdaki gibi çalıştırıyoruz. [code language=”java”] // execute function mConnection.execute(function); [/code] EXPORTING bölümü için ise yukarıdaki gibi fonksiyon çalıştırdıktan sonra aşağıdaki gibi gelen değerleri yönetebiliriz. [code language=”java”] // exporting String tmpParam01 = function.getExportParameterList().getString("EV-RFC-EXPORT-PARAMETER_NAME_01"); // CHAR myExpParamList = function.getExportParameterList(); String tmpParam11 = myExpParamList.getString("EV-RFC-EXPORT-PARAMETER_NAME_11"); // CHAR String tmpParam12 = myExpParamList.getString("EV-RFC-EXPORT-PARAMETER_NAME_12"); // CHAR [/code] EXPORTING STRUCTURE, EXPORTING bölümünde ABAP tarafında structure adlanan yapının elde edilişi ve işlenmesi aşağıdaki gibidir. [code language=”java”] // exporting structure myExpStrctr = function.getExportParameterList().getStructure("ES-RFC-EXPORT-PARAMETER_NAME"); Account account = new Account(); account.setId(myExpStrctr.getString("PERNR")); // CHAR String respCommonName = myExpStrctr.getField("ENAME").getValue().toString(); // CHAR account.setCommonName(respCommonName); byte[] accountImageByteArr = myExpStrctr.getByteArray("FOTOGRAF"); // XSTRING, RAWSTRING account.setPhotoByteArr(accountImageByteArr); String accountImageBase64 = Base64.encodeBase64String(accountImageByteArray); account.setImageBase64(accountImageBase64); [/code] EXPORTING STRUCTURE, EXPORTING bölümünde ABAP tarafından structure adlanan yapının elde edilişi ve işlenmesine başka bir örnek ise aşağıdaki gibidir. [code language=”java”] // exporting structure for(int j=0; j<fileStrctr.getMetaData().getFieldCount(); j++) { String fieldName = fileStrctr.getMetaData().getName(j); if(fieldName.equals("ES_CONTRACTNO")) { String fieldValue = fileStrctr.getString(j); } if(fieldName.equals("ES_XSTRING")) { byte[] fileByteArr = fileStrctr.getByteArray(j); String fileBase64 = Base64.encodeBase64String(fileByteArr); tmpCustomer.setAttachedFile(fileByteArr); } if(fieldName.equals("ES_DATE")) { Date fileAttachedFileDate = fileStrctr.getDate(j); String tmpDateStr = MyDateUtils.getStrFromDate(fileAttachedFileDate, MyDateUtils.timeStampddDotMMDotyyyy); //tmpCustomer.setDateStr(tmpDateStr); } } [/code] EXPORTING TABLES, TABLES bölümü için gönderdiğimiz değerlere karşı alma işleminde aşağıdaki gibi işlem yapabilirsiniz. [code language=”java”] // exporting tables myExportTbl = function.getTableParameterList().getTable("ET-RFC-TABLES-PARAMETER_NAME"); myExportTbl.firstRow(); for (int i=0; i<myExportTbl.getNumRows(); i++) { int stepId = i+1; String tmpName = executionTbl.getString("NAME"); // CHAR tmpPerson = new Person(); tmpPerson.setId(stepId); tmpPerson.setName(tmpName); personList.add(tmpPerson); myExportTbl.nextRow(); } [/code] Buradaki örneği de inceleyebilirsiniz. Örneğin kaynağına ise buradan bakabilirsiniz. [code language=”java”] /** * Example2.java * Property of SAP AG, Walldorf * (c) Copyright SAP AG, Walldorf, 2000. * All rights reserved. */ import com.sap.mw.jco.*; /** * @version 1.0 * @author SAP AG, Walldorf */ public class Example2 { // The MySAP.com system we gonna be using static final String SID = "R3"; // The repository we will be using IRepository repository; public Example2() { try { // Add a connection pool to the specified system JCO.addClientPool(SID, // Alias for this pool 10, // Max. number of connections "000", // SAP client "johndoe", // userid "*****", // password "EN", // language "appserver",// host name "00"); // Create a new repository repository = JCO.createRepository("MYRepository", SID); } catch (JCO.Exception ex) { System.out.println("Caught an exception: \n" + ex); } } // Retrieves and prints information about remote system public void systemInfo() { try { // Get a function template from the repository IFunctionTemplate ftemplate = repository.getFunctionTemplate("RFC_SYSTEM_INFO"); // Create a function from the template JCO.Function function = new JCO.Function(ftemplate); // Get a client from the pool JCO.Client client = JCO.getClient(SID); // We can call ‘RFC_SYSTEM_INFO’ directly since it does not need any input parameters client.execute(function); // The export parameter ‘RFCSI_EXPORT’ contains a structure of type ‘RFCSI’ JCO.Structure s = function.getExportParameterList().getStructure("RFCSI_EXPORT"); // Use enumeration to loop over all fields of the structure System.out.println("System info for " + SID + ":\n" + "——————–"); for (JCO.FieldIterator e = s.fields(); e.hasMoreElements(); ) { JCO.Field field = e.nextField(); System.out.println(field.getName() + ":\t" + field.getString()); }//for System.out.println("\n\n"); // Release the client into the pool JCO.releaseClient(client); } catch (Exception ex) { System.out.println("Caught an exception: \n" + ex); } } // Retrieves and sales order list public void salesOrders() { try { // Get a function template from the repository IFunctionTemplate ftemplate = repository.getFunctionTemplate("BAPI_SALESORDER_GETLIST"); // Create a function from the template JCO.Function function = new JCO.Function(ftemplate); // Get a client from the pool JCO.Client client = JCO.getClient(SID); // Fill in input parameters JCO.ParameterList input = function.getImportParameterList(); input.setValue("0000001200", "CUSTOMER_NUMBER" ); input.setValue( "1000", "SALES_ORGANIZATION"); input.setValue( "0", "TRANSACTION_GROUP" ); // Call the remote system client.execute(function); // Print return message JCO.Structure ret = function.getExportParameterList().getStructure("RETURN"); System.out.println("BAPI_SALES_ORDER_GETLIST RETURN: " + ret.getString("MESSAGE")); // Get table containing the orders JCO.Table sales_orders = function.getTableParameterList().getTable("SALES_ORDERS"); // Print results if (sales_orders.getNumRows() > 0) { // Loop over all rows do { System.out.println("—————————————–"); // Loop over all columns in the current row for (JCO.FieldIterator e = sales_orders.fields(); e.hasMoreElements(); ) { JCO.Field field = e.nextField(); System.out.println(field.getName() + ":\t" + field.getString()); }//for } while(sales_orders.nextRow()); } else { System.out.println("No results found"); }//if // Release the client into the pool JCO.releaseClient(client); } catch (Exception ex) { System.out.println("Caught an exception: \n" + ex); } } public static void main(String[] argv) { Example2 e = new Example2(); e.systemInfo(); e.salesOrders(); } } [/code] Daha detaylı ABAP ve Java örneklerine ise buradan bakabilirsiniz.]]>

Leave a Reply

Your email address will not be published. Required fields are marked *