This is a part of H2G – “How To Implement Lightening Fast OData Services with Exchange Table“.
You should have done the following steps:
– Extending Query operation
1. As written in the Prerequisites section, you’re supposed to have done with the basic Query operation. You should already have the code as written here.
Note: If you have done the “#2 – How To Implement $skiptoken”, your Query operation should have this code. I suggest to comment them out and use the simple Query operation in this H2G – but we’ll come back and make use of it in the later H2G.
2. We are enhancing the existing Query operation logic. Go to the “…_GET_ENTITYSET” code via the transaction SEGW. You should already have the code as written here – in between the line #03 and #04, add this variable declaration:
... 03 DATA: lt_entityset TYPE TABLE OF stravelag, 04 lv_delta_token TYPE timestamp. ...
The lv_delta_token will hold the timestamp value, which will work as the “token”.
And in between the line #05 and #06, add lines of code to set the delta token value in OData.
... 05 SELECT * FROM stravelag INTO TABLE lt_entityset. 06 * delta token based on timestamp 07 GET TIME STAMP FIELD lv_delta_token. 08 * export the delta token 09 es_response_context-deltatoken = lv_delta_token. 10 et_entityset = lt_entityset. ...
#09 sets the local token value “lv_delta_token” to “es_response_context-deltatoken”, which will be rendered in OData format.
Now activate the code.
– Testing Query operation
1. Let’s try to run the Query, as you have done before. Scroll down the OData payload to the bottom – now you’ll see an interesting new token you have just added.
This is the delta token we’re talking about. When we do the Delta Query, this value needs to be passed to the Delta Query, which will be implemented in the next step.
– Implementing Delta Query
1. As you confirmed during the testing, you’ll have a delta token value and we’ll use it in the OData URL (ex. ../Z_TRAVELAGENCY_SRV/TravelAgencySet?!deltatoken=’20150417053404%20′). If the OData query string contains a delta token value, SAP Gateway triggers “..~GET_ENTITYSET_DELTA” method of the OData service. We’ll implement that method.
2. 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_DELTA” and press the Redefine icon.
2. You have an empty method.
3. Time to code. So essentially what the Delta Query logic is supposed to do is:
- Pick up the incoming delta token value
- Obtain the delta record out of the Exchange Table by using the token
- Set the actual entities by making use of the delta record
- Create a new delta token
- Return them in OData
01 method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITYSET_DELTA. 02 03 DATA: lv_delta_token TYPE timestamp, 04 lo_delta_context TYPE REF TO /IWBEP/IF_MGW_REQ_ENTITYSET, 05 lr_ref_exch_data TYPE REF TO data, 06 lt_entityset TYPE TABLE OF stravelag, 07 ls_entityset TYPE stravelag, 08 lt_deleted_entityset TYPE TABLE OF stravelag, 09 ls_deleted_entityset TYPE stravelag. 10 11 FIELD-SYMBOLS: <lt_delta_tab> TYPE STANDARD TABLE, 12 <ls_delta_tab> TYPE zex_travelagency. 13 14 * incoming delta token 15 lo_delta_context ?= io_tech_request_context. 16 lv_delta_token = lo_delta_context->get_deltatoken( ). 17 18 IF lv_delta_token IS NOT INITIAL. 19 * ask exchange table to obtain the delta data 20 CALL METHOD /smfnd/cl_core_exobj_tools=>determine_delta_objkeys 21 EXPORTING 22 iv_mobile_app = 'ZAPPCONFIG' 23 iv_exchobj = 'ZEX_OBJ_TRAVELAGENCY' 24 iv_time_token = lv_delta_token 25 iv_time_zone = 'UTC' 26 IMPORTING 27 eref_exch_data = lr_ref_exch_data. 28 29 IF lr_ref_exch_data IS NOT INITIAL. 30 * examine the delta table 31 ASSIGN lr_ref_exch_data->* TO <lt_delta_tab>. 32 * Create/Update OR Delete? 33 LOOP AT <lt_delta_tab> ASSIGNING <ls_delta_tab>. 34 IF <ls_delta_tab>-action = 'I' OR <ls_delta_tab>-action = 'U'. 35 * load the entity 36 SELECT SINGLE * FROM stravelag INTO ls_entityset WHERE agencynum = <ls_delta_tab>-objkey. 37 APPEND ls_entityset TO lt_entityset. 38 ELSEIF <ls_delta_tab>-action = 'D'. 39 ls_deleted_entityset-AGENCYNUM = <ls_delta_tab>-objkey. 40 APPEND ls_deleted_entityset TO lt_deleted_entityset. 41 ENDIF. 42 ENDLOOP. 43 ENDIF. 44 45 * created/updated entity set 46 copy_data_to_ref( EXPORTING 47 is_data = lt_entityset 48 CHANGING 49 cr_data = er_entityset ). 50 * deleted entity set 51 copy_data_to_ref( EXPORTING 52 is_data = lt_deleted_entityset 53 CHANGING 54 cr_data = er_deleted_entityset ). 55 * new delta token 56 GET TIME STAMP FIELD lv_delta_token. 57 es_response_context-deltatoken = lv_delta_token. 58 59 ENDIF. 60 61 endmethod.
Code explained –
#03 – #04: declares the variables for obtaining the delta token value out of incoming OData URL.
#05 will hold the delta data out of the Exchange Table – the method is called in the line #20.
#06 – #09: declares the variables for picking up the actual entity values by making use of the delta record in #05.
#11 – #12: those variables are for ABAP’s coding technique called field-symbols, which is intended for dynamically assigning the value so that the line of code can be minimized cleanly (if used correctly). They will be used when calculating the actual delta entityset data by making use of the delta data of the Exchange Table during the lines of #31 – #42.
#15 – #16: fetches the delta token value.
#20 calls the method which returns the delta record of the Exchange Table by giving the delta token. You might want to go back to Syclo Config Panel again to look up your Mobile Application and Exchange Object name values. The lr_ref_exch_data should contain the delta tracked record since you obtained that delta token value.
#29 – #43: calculates the actual entities out of the delta record. As the delta record contains all the CUD operations (but minimum record), you need to sort out which is Create/Update/Delete so that the Delta Query can return those real delta info in OData.
#45 – #54: converts the data type. If you click on the Signature button, you’ll see the “…~GET_ENTITYSET_DELTA” method is expecting to return “ER_ENTITYSET” and “ER_DELETED_ENTITYSET” and both have “TYPE REF TO DATA” declaration. This means we need to convert the table data structure into a reference – the “copy_data_to_ref()” method does the job.
#55 – 57: creates a new delta token, which the OData client (an offline store) needs to know to fetch further delta the next time.
Got it? I believe the logic is fairly re-usable in most situations.
Now you can activate and run it.
– Testing Delta Query with Exchange Table
1. Let’s obtain the delta token. Simply run the Query operation and keep the token value.
2. Run any CUD operation – those changes should be recorded in the Exchange Table as you have done.
3. Now the time to run your Delta Query. put your delta token in the Query URL (ex. …/Z_TRAVELAGENCY_SRV/TravelAgencySet?!deltatoken=’20150417053404%20′)
It is always a good idea to set External Breakpoints to see how your “…~GET_ENTITYSET_DELTA” is called.
4. You should confirm the delta data in OData payload – showing what are the new entities for Created & Updated AND what are the Deleted ones since you obtained the last delta token.
The offline store will understand these info and update its local content accordingly.
Congratulations, now your super efficient Delta Query based on Exchange Table is running happily!
– Further Reading
How To Enable Delta Queries using Syclo Exchange Framework and SAP NetWeaver Gateway – very detailed document explaining some of the main benefits using Syclo xChange Framework: standardized change detection pattern, field level precision change detection, condition filtering, configurability, and reusability for multiple mobile scenarios.