Вы находитесь на странице: 1из 14

#1 - OData CRUD Crash Course Create

- Building Create operation


1. In SAP transaction SEGW, expand a project folder and open TravelAgencySet. Click on
Create and choose Go to ABAP Workbench. As the first implementation, you'll see the popup.

2. Click on the *_DPC_EXT folder. The right pane should show the Class Interface shown in
the next step.You'll see the list of the methods. Scroll down - click and focus on the
"..._CREATE_ENTITY" and press the Redefine icon.
Tip: Make sure if your editor is in edit mode with the

icon. Redefine icon is

3. Delete the commented out lines of code. You have an empty implementation. If you click
"Signature" text, you'll see the in & out parameters of this method. We'll implement so that
it returns "ER_ENTITY" parameter.

Here's implementation:
Namingconvention:

llocalscope
ttable
sstructure
vvariable

01methodTRAVELAGENCYSET_CREATE_ENTITY.
02
03DATA:ls_entitysetTYPEstravelag,
04ls_key_tabTYPE/iwbep/s_mgw_name_value_pair,
05lv_error_entityTYPEstring.
06
07io_data_provider>read_entry_data(IMPORTINGes_data=ls_entityset).
08
09ls_key_tabvalue=ls_entitysetagencynum.
10
11INSERTintostravelagvaluesls_entityset.
12
13IF(sysubrc=0).
14*entityinserted
15er_entity=ls_entityset.

16ELSE.
17*entityalredyexists
18CONCATENATEiv_entity_name
19'('''
20ls_key_tabvalue
21''')'
22INTOlv_error_entity.
23RAISEEXCEPTIONTYPE/iwbep/cx_mgw_busi_exception
24EXPORTING
25textid=/iwbep/cx_mgw_busi_exception=>resource_duplicate
26entity_type=lv_error_entity.
27ENDIF.
28
29endmethod.

#05 declares a variable which can have the error message if the Create operation failed.
#07 picks up the entity value which comes in from the OData client. #09 sets the key value
"agancynum" in a local key-value table (this will be helpful in other enhancement H2G later).
#11 does a simple insert command to store a new entity value in the TravelAgency table. If
the command goes successful, the sy-subrc value should be 0. #18 - #26 sets the userfriendly error message saying the key value already exists in the TravelAgency table.

Make sure you activate it by

- Testing Create operation

icon.

1. Do the Query operation. In the query step, set the two HTTP Header parameters - these
are required to do any data modification (either Create/Update/Delete) against OData
services in SAP Gateway.
X-CSRF-Token = Fetch
Content-Type = application/atom+xml; charset=UTF-8

2. In a response header, you'll find the token value in X-CSRF-Token. Copy it. (Note: the token
will keep valid until the browser gets closed)

3. Replace the copied value with "Fetch" string. Now your REST client is ready for Create
operation via HTTP POST.

4. Choose one of the entities in the returned Query operation - in <id> tag.

5. Run the Read operation.

6. You should obtain the entity value in a response body. Copy it.

7. And paste it in the Body payload.

8. By making use of this body content, we'll create a new entity. For this case just change
the <d:Agencynum> value to a new one which doesn't yet exist on the TravelAgency table.
You can change other values if you like - but make sure the XML tag is well formed by a set
of opening and closing tags.

9. Let's issue by HTTP POST. The URL is /TravelAgencySet without any key value.

10. You should receive HTTP 201 Created. The Create operation executed successfully!

#1 - OData CRUD Crash Course - Query

- Building Query operation

1. In SAP transaction SEGW, expand a project folder and open TravelAgencySet. Click on
GetEntitySet (Query) and choose Go to ABAP Workbench.

2. As the first implementation, you'll see the popup.

3. Click on the *_DPC_EXT folder. The right pane should show the Class Interface shown in
the next step.

4. You'll see the list of the methods. Scroll down - click and focus on the "..._GET_ENTITYSET"
and press the Redefine icon.
Tip: Make sure if your editor is in edit mode with the

icon. Redefine icon is

5. Here's the generated code from the superclass. Delete the commented out lines of code.

6. You have an empty implementation now.

7. If you click "Signature" text, you'll see the in & out parameters of this method. We'll
implement so that it returns "ET_ENTITYSET" parameter.

Here's implementation:
Namingconvention:
llocalvariable
ttable

01methodTRAVELAGENCYSET_GET_ENTITYSET.
02
03DATA:lt_entitysetTYPETABLEOFstravelag.
04
05SELECT*FROMstravelagINTOTABLElt_entityset.
06
07et_entityset=lt_entityset.
08
09endmethod.

#03 defines a variable which holds the TravelAgency table contents. #05 loads the database
table "stragelag" that contains the TravelAgency data into the local variable. #07 sets the
value in the predefined output parameter "et_entityset" - as you see in the Signature pane,
the data type is already generated as a TravelAgency structure
"..._MPC=>TT_TRAVELAGENCY".

Those tiny 3 lines of code does the database access and returns the TravelAgency collection
in OData - how simple & powerful is that? :-)

Make sure you activate it by

icon.

- Testing Query operation

Start your favorite REST Client tool - this one is using Firefox RESTClient. In this step, you
should have seen the actual URL of the OData service. Type it in the REST client URL with
HTTP GET.

The Query result should come in correctly!


- Creating a project in Gateway Service Builder

1. Lets create the project file. Open SAP transaction SEGW. Youll navigate to SAP Gateway
Service Builder. Click on the icon for Create Project.

2. Name your project (this example is Z_TravelAgency) and save it.

3. Youll see those folders generated.

4. Right click on Data Model and select Import > DDIC Structure.

5. Well generate an entity with stravelag structure. Name it TravelAgency and enter the
ABAP Structure name.

6. Next screen youll choose which columns in the structure you want to generate as OData
properties. Select all except MANDT field (In this case, it doesn't really matter if you keep it
or not)

7. And the next screen let you choose the Key. These keys will be used to
read/update/delete entity. In this example we just select AGENCYNUM.

8. Now that youll confirm those TravelAgency, TravelAgencySet, and CRUD + Query
Implementation are generated Note that these are still empty. Well implement them later
steps.

9. Right click on the project name and choose Generate Runtime. This will generate actual
OData runtime service code.

10. Accept all the default values and save it.

11. After you have done the step above, youll confirm the Method Name fields are filled
out the template methods are generated.

12. Find the folder node Service Maintenance. In order to run the current OData service we
just have generated, we need to configure here. Click it and youll see the config pane on
the right.

13. (This might not the same with your screen this depends on how your Gateway system
are deployed) In this screenshot, click on GW_HUB and click Register button.

14. Youll see a popup asking you the alias name. (Ex. LOCAL - but the value should vary
in your system) Once you enter the correct alias name, youll see this popup and select your
Package Assignment ($tmp).

15. You should be able to confirm green light

16. If you see the green light, press Gateway Client button.

17. Youll see the REST client starts. Just click Execute and the response should come back.

18. In the response payload, youll confirm the generated Collection name
TravelAgencySet and entity name TravelAgency. These are all generated out of the ABAP
structure.

19. Now lets experiment. Add the TravelAgencySet in the URL and see what happens.

20. Yes as expected, youll see the error message. The Query is not yet implemented. Well
do it in the other chapter.

Вам также может понравиться