Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 

Introduction


 

This is a simple step-by-step guide on setting up a Gateway service and calling it from Kotlin using the Intellij IDE.

This Blog is organized in 2 sections:


  1. Build an SAP Gateway Service



  2. Call the Service from Kotlin



Software Versions:

  • 52 of SAP, SAPGUI Version 750.

  • Version 2018.1.3 (Ultimate Edition) of the Intellij IDE was used.


 

1. Build an SAP Gateway Service


Execute the SAP Transaction SEGW and click on the “Create Project” button:



Enter a Project name starting with “Z” and a description, and leave all other fields default. Enter $TMP in the “Package” field to make it a local object without a transport.



Click “Continue” to save the new project.

Next, create a new data model for our project. Right-click on the “Data Model” node, and select the menu path “Import > DDIC Structure”.



Enter the following:


  • Name: GLLineItemSum



  • DDIC Structure: FACV_RJET_WLI_06



  • Leave all other fields as Default





Click the “Next” button to continue.

Note: Notice this structure is actually an ABAP CDS View, but can be referenced just the same as a regular view in SE11. Might as well enjoy the HANA goodness!

The next screen shows a list of fields contained in this structure. Select all fields, except for MANDT.



Click the “Next” button to continue.

For the first 3 fields (BELNR, BUKRS and GJAHR), check the “Is Key” checkbox:



Click the “Finish” button to continue.

Save the Project by clicking the “Save” button or (Ctrl+S)

Click on the “Check Project Consistency” button to ensure there are no errors:





Click on the “Generate Runtime Objects” button:



Classes are auto-generated:



You may keep the default names, and press the “Continue” button.

Click on the “Local Object” button to save locally, without a transport:



This gives a list of the newly generated classes and objects:



Next, we will register and test the new Gateway service. Expand the “Service Maintenance” node:



If there is nothing under your “Service Maintenance” folder, this means that the SAP Gateway has not yet been setup for your SAP instance. You will need to set this up first. There are many good blogs out there that walk-through a setup.

Right-click on the Gateway defined for your system, in this case GW_HUB and select “Register”:



For the “System Alias” pop-up, type in the system alias for your Gateway system. This pop-up is a little odd in 750, as it does not allow you to select from the search help, but rather I had to type it in. You may have the same issue with your system. A typical test setup uses the “LOCAL” Alias, which we will use here:



Click the “Continue” button to accept the entry. This brings you to the following screen:



Click on the “Local Object” button in the center of the screen, to default in “$TMP” for the Package Assignment:



Leave all other values on this screen as the default, and click on the “Continue” button.

Back on the main project screen, double-click on the Gateway node GW_HUB (or whatever your Gateway is named):



Confirm that the status is now green for our new project:



Next, we want to test our new service. Right-click on the Gateway node GW_HUB and select “SAP Gateway Client”.



Once in the Gateway Client, click on the “Execute” button:



We have not yet written any code, but this will check the service and confirm that all is working properly:



Click on the “Entity Sets” button:



Double-click on the “GLLineItemSumSet” row:



The Request URI now becomes:

  • /sap/opu/odata/SAP/ZFINANCE_REPORT_SRV/GLLineItemSumSet




Again click on the “Execute” button and you will now get the following:



We have not yet implemented the code to fetch the GL data, so we get the above error. Let’s implement that method now. Close the SAP Gateway Client and go back to our project in SEGW.

Make sure your project is saved. Expand the node “Service Implementation > GLLineItemSumSet”:



Right-click on the “GetEntitySet (Query)” node and select “Go to ABAP Workbench”:



Click the “Continue” button for the following information screen:



We are now in the ABAP Class Builder.

Click to edit mode by clicking the “Display<->Change” button:



Scroll all the way to the bottom of the list of Methods to find the “GLLINEITEMSUMSET_GET_ENTITYSET” method:



Place your cursor in the row with the “GLLINEITEMSUMSET_GET_ENTITYSET” method.

Ensure that the right-hand checkbox titled “Filter” is unchecked:



In the toolbar, click on the second button from the right, entitled “Redefine Method”:



In the Code Editor, enter the following code:
    select * from FACVD_RJET_WLI_06 into CORRESPONDING FIELDS OF
table @et_entityset
UP TO 100 rows.



Check (Ctrl+F2), Save (Ctrl+S) and Activate (Ctrl+F3) your program.



When prompted, Click the “Enter” button to accept the following:



Click the Back Arrow, until you return to your project screen (SEGW):



We can now test our new logic and Gateway Service. Again, navigate to the SAP Gateway Client from your project (right-click from the Gateway node):



Click on the “Entity Sets” button:



Select the row “GLLineItemSumSet”, which adds the following URI:



Click on the “Execute” button to test:



We now have our OData results with our GL Report:



To switch to JSON, click the “Add URI Option” button:



Double-click on the “$format=json” row:



This appends the following to the URI:

  • /sap/opu/odata/SAP/ZFINANCE_REPORT_SRV/GLLineItemSumSet?$format=json


Click on the “Execute” button again to see the JSON results:



Now, we can copy the url from this JSON and get the following:

  • http://<your company server>.com:8000/sap/opu/odata/SAP/ZFINANCE_REPORT_SRV/GLLineItemSumSet?$format=json


Pasting this url into a browser will call the Gateway Service and get our JSON data:



The above is shown in a Chrome browser, with the JSONView extension installed:



We will use this URL to fetch the JSON in our Kotlin project.

 

2. Call the Service from Kotlin


 

In the Intellij IDE, create a new project:



Select the Kotlin/JVM Project and click “Next”:





Give it a project name and click the “Finish” button.

Expand your project source tree, right-click on the “src” folder and select the menu path “New > Kotlin File/Class”:



Create a Kotlin file and give it a name:



Leave this file empty, for now, as we will add the logic later.

Next, let’s use a tool in Intellij to get an encrypted sign-on for SAP.

Go to the menu path “Tools > HTTP Client > Test RESTful Web Service”:



In the lower-left, click on the button “Generate Authorization Header”:



When prompted, type in your SAP User Name and password and click “OK”:



This generates an authorization string in the lower-left corner:



Mine is blanked out above, for obvious reasons, but copy this string, starting with the “Basic…” part, all the way to the end of the string. When you click on the string, it will toggle into an edit box, where you may copy it:



Save this string for later use. In the Kotlin code, below, you will replace the string “<your auth code>” with this encrypted authorization string.

Add the Kotson Library:

The Kotson open source library allows you to utilize Google’s Gson library with Kotlin for parsing JSON.

Add the kotson library to your project. Go to “File > Project Structure”:



Go to “Libraries” and Insert a new project library:



Choose “From Maven”:



Paste in the following Maven coordinates and click “OK” to save:

  • com.github.salomonbrys.kotson:kotson:2.5.0




You may be prompted to update this kotson library to the most recent version.

Next, let’s return to our “GLReport.kt” Kotlin file and add the following code. Be sure to replace the following:


  • <your server name> - replace with your own SAP Gateway server.



  • <your auth code> - your encrypted auth code consisting of your SAP logon user name and password. This was generated from the Intellij tool, as described in the steps, above.



import java.net.HttpURLConnection
import java.net.URL
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.google.gson.JsonParser

// Kotson - Kotlin bindings for JSON manipulation via Gson
// https://github.com/SalomonBrys/Kotson#install
fun main(args: Array<String>) {
val response = sendGet()
//Format the json output...
val gson = GsonBuilder().setPrettyPrinting().create()
val jsonObject = JsonParser().parse(response) as JsonObject
val json = gson.toJson(jsonObject)
println(json)
}

private fun sendGet() : String{
//JSON...
val url = "http://<your server name>.com:8000/sap/opu/odata/SAP/ZFINANCE_REPORT_SRV/GLLineItemSumSet?\$format=json"
val connection = URL(url).openConnection() as HttpURLConnection
val auth = "<your auth code>"
connection.addRequestProperty("Authorization", "Basic $auth")
connection.connect();
val text = connection.inputStream.use { it.reader().use { reader -> reader.readText() } }
return text;
}

 

To test, you can simply click on the green arrow just to the left of your main method:





The call is made to the SAP Gateway service and the results of the JSON are formatted and printed to the screen:



 

References


 

Intellij IDE:

https://www.jetbrains.com/idea/

 

To read more about the Kotlin Language:

https://kotlinlang.org/

 

Kotlin bindings for JSON manipulation via Gson:

https://github.com/SalomonBrys/Kotson

 

Excellent Blog on creating a project on the SAP Gateway:

https://blogs.sap.com/2016/05/31/odata-service-development-with-sap-gateway-code-based-service-devel...

 

The raw DDL Source for the CDS View used in this example is (SAP version 752):
@AbapCatalog.sqlViewName: 'FACV_RJET_WLI_06'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@ClientHandling.algorithm: #SESSION_VARIABLE
@ObjectModel.usageType.sizeCategory: #XL
@ObjectModel.usageType.serviceQuality: #X
@ObjectModel.usageType.dataClass: #MIXED
@EndUserText.label: 'Creating sums of amounts from bseg'


define view FACVD_RJET_WLI_06

as select from bseg
{
key bseg.belnr,
key bseg.bukrs,
key bseg.gjahr,

//creating addition of the amount values
sum (bseg.wrbtr) as sum_of_bseg_wrbtr,
sum (bseg.dmbtr) as sum_of_bseg_dmbtr

} where bseg.shkzg = 'S'

group by
bseg.belnr,
bseg.bukrs,
bseg.gjahr

 
Labels in this area