By Guest Blogger Kenny Sutherland, SAP Technical Intern, DataXstream
In three-tier architecture, SAP Gateways make up the middle or application tier. This middle layer is essential for communication between the frontend and the backend. The use of multi-tier architecture to implement a data processing system adds a valuable level of modularity and flexibility to the system by being able to develop and maintain each tier individually. The purpose of this tutorial is to create a Gateway that will extract data from the flight carrier database table, which will be accessed from the Gateway Client. The Gateway will be able to retrieve a list of carriers as well as individual carriers. Let’s get started…
Creating a New Project
- First, navigate to the Gateway Service Builder with the T-Code “SEGW”
- Click the “Create Project” button
- Give the Gateway project a name and a description
- Save the project and assign it to the appropriate package. For the purpose of this demo the project will be saved as a local object ($TMP or click the button “Local Object). The Gateway project has now been successfully created
Creating an Entity & Entity Set
- What is an Entity and Entity Set?
- An Entity is a structure that can be defined by the user or defined as an ABAP Data Dictionary structure. An Entity Set is simply a collection or table of Entities.
- Right click Data Model and select “Import” -> “DDIC Structure” to use an ABAP Data Dictionary structure
- Type “SCARR” for ABAP Structure and press enter. A list of properties should appear
- Make “Carrier” the object name for the entity
- Change the “Usage” of the property “MANDT” to “Ignore” and click the check mark at the bottom right
- Double click the folder “Entity Sets”
- Click “Insert Row” and name the Entity Set
- The naming convention is to either make the Entity Set name the plural form of the name of the entity or append “_Set” to the Entity name. For training purposes, name the entity set “Carriers” or “Carrier_Set”. “Carriers” will be used for the remainder of this tutorial
- Use the Entity name, “Carrier”, for “Entity Type Name”. Make sure to save and the Entity and corresponding Entity Set have successfully created
How to Generate ABAP Classes
- Click on the “Generate Runtime Objects” button towards the top left of the IDE
How to Activate Gateway Service
- Navigate to the “Activate and Maintain Services” page, “/iwfnd/maint_service”, and click “Add Service”
- Set System Alias to “LOCAL” and Technical Service Name to the name of the Gateway
- Click “Local Object” and then the check button to save
- Go back to the “Activate and Maintain Services” page, click on the service name, and click on “Gateway Client”
- To test the service, verify the “HTTP Method” is set to “GET” and then click “Execute”. There should now be some auto-generated XML
- In order to view the entity and its properties add a URI option to the end of the URI. Click “Add URI Option” and use “$metadata” and “sap-ds-debug=true”
- Now we can see the Entity Type as well as its properties
Congratulations! You have made a usable Gateway Service. Now the backend functionality of the Gateway must be coded in order to make it useful.
- Navigate back to the gateway service builder, expand the “Service Implementation” folder, and expand the entity set. There will be a few auto-generated methods
- Right click “GetEntitySet”, click “Go to ABAP Workbench”, and ignore the popup that follows. This will take bring up the Class Builder
- In the left menu, expand the “Methods” folder, right click on the “GET_ENTITYSET” method, and select “Redefine”
- Under “Signature”, what the method is exporting to the frontend service can be seen, “ET_ENTITYSET”. This exporting table needs to be populated with data from the backend database
- It is generally bad practice to select all the records from a database table because it can be extremely inefficient and redundant so instead of using “SELECT *”, only select the first 100 records from the database using the following statement…
“SELECT * FROM scarr INTO CORRESPONDING FIELDS OF TABLE et_entityset UP TO 100 ROWS.”
- Activate the method
- To debug this code, set an external breakpoint. Session breakpoints will not work using the Gateway Client. Now the method needs to be tested
- Reenter the “Activate and Maintain Services” or if it is already in a window click “Refresh Catalog”
- Open the service again using the Gateway Client
- Append the name of the Entity Set to the end of the URI, verify “HTTP Method” is set to “GET”, and execute. There should now be multiple Carrier entries
- To get an individual Carrier, the Get_Entity method must be implemented
- In the Class Builder, right click CARRIERS_GET_ENTITY and select “Redefine”
- Add the following code,
“DATA: ls_key_tab LIKE LINE OF it_key_tab.
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'Carrid'.
SELECT SINGLE carrid carrname currcode url FROM scarr
INTO CORRESPONDING FIELDS OF er_entity
WHERE carrid = ls_key_tab-value.”
- The above code will select a Carrier using the Carrid that will be passed into the URI
- Activate this method and open the Gateway Client one more time
- Make sure HTTP Method is “GET”, type “/sap/opu/odata/sap/Z_GATEWAY_DEMO_SRV/Carriers(‘AF’)” for the URI, and press execute
- There should now be an individual Carrier using the Carrid that was just passed in the URI
Congratulations! You have made your first Gateway Service.
Kenny Sutherland is a current Christopher Newport University student working towards a degree in Information Systems.
We welcome prospective interns to contact us about starting your journey at DataXstream!
Visit our Jobs page or email our Director of Human Resources email@example.com for more information.