Experiences with SAP Gateway

"My foots always in my mouth i just can't stomach defeet" – Hilltop Hoods

Addressing with key

with one comment

This one is a pretty big gotcha and it is hard to explain so bear with me.

KeyPredicate: A predicate that identifies the value(s) of the key Properties of an Entry. If the Entry has a single key Property the predicate may include only the value of the key Property. If the key is made up of two or more Properties, then its value must be stated using name/value pairs. More precisely, the syntax for a KeyPredicate is shown by the following figure.


[Source: OData: URI Conventions: 3.1. Addressing Entries]

Examples of using key and value pairs
http://services.odata.org/Northwind/Northwind.svc/Suppliers(1)
http://services.odata.org/Northwind/Northwind.svc/Suppliers(SupplierID=1)

How the call is structured is very much in the hands of the client API and in my experience they tend to use the KEY when its available.

When you address entries using the KEY in the SAP Netweaver Gateway 2.0 Trial Version it is important to note that the table IT_KEY_TAB leaves out the NAME.

This means we have to do something like the following to make IT_KEY_TAB usable in a SQL statement

lt_key_tab = it_key_tab.
lv_name = 'SUPPLIERID'.
LOOP AT lt_key_tab ASSIGNING <fs_key_tab>.
  CHECK <fs_key_tab>-name EQ space.
  <fs_key_tab>-name = lv_name.
ENDLOOP.

Which allows us to make the following call

SELECT * FROM SUPPLIER WHERE SUPPLIERID EQ 1

The trouble is the NAME is not populated when calling from an association either

eg.
http://services.odata.org/Northwind/Northwind.svc/Suppliers(1)/Products

In this case the SUPPLIER entity DAO is not called, the PRODUCT Entity GET_ENTITYSET method is and the IT_KEY_TAB has a missing NAME value

We have to look at the IV_SOURCE_NAME to derive the source entity key for our SQL call

CASE iv_source_name.
 WHEN 'Category'.
   lv_name = 'CATEGORYID'.
 WHEN 'Product'.
  lv_name = 'PRODUCTID'.
 WHEN 'Supplier'.
  lv_name = 'SUPPLIERID'.
ENDCASE.
LOOP AT lt_key_tab ASSIGNING <fs_key_tab>.
 CHECK <fs_key_tab>-name EQ space.
 <fs_key_tab>-name = lv_name.
ENDLOOP.

Which will hopefully end with

SELECT * FROM PRODUCT WHERE SUPPLIERID EQ 1

The following happens when you call associated KEYS

eg.

http://services.odata.org/Northwind/Northwind.svc/Suppliers(1)/Products(2)

In the GET_ENTITY method of the PRODUCT data provider, the IT_KEY_TAB has a single entry again with a missing NAME which relates to the the SOURCE entity, which in the scenario above is SUPPLIER.

In this case we can get the PRODUCT KEY from IT_NAVIGATION_PATH-KEY

or IT_NAVIGATION_PATH-KEY_TAB (or by calling the source entity)

 
IF iv_source_name EQ 'Product'.
    READ TABLE it_key_tab INTO ls_key_tab INDEX 1.
 ELSE.
    READ TABLE it_navigation_path ASSIGNING <fs_navigation_path> INDEX 1.
    IF <fs_navigation_path>-key IS NOT INITIAL. "1:1
       ls_key_tab-value =  <fs_navigation_path>-key.
     ELSE.
       IF iv_source_name = '<SourceEntity>'. "N:1
          DATA: lo_SourceEntity TYPE REF TO zcl_SourceEntity,
          ls_SourceEntity TYPE zcl_service_model=>ts_SourceEntity.
          " go to the source and get the key
          CREATE OBJECT lo_SourceEntity.
          lo_SourceEntity->get_entity( EXPORTING iv_entity_set_name = iv_entity_set_name
                                                 iv_source_name = iv_source_name
                                                 it_key_tab = it_key_tab
                                                 it_navigation_path = it_navigation_path
                                       IMPORTING es_SourceEntity = ls_SourceEntity ).
          ls_key_tab-value = ls_SourceEntity-productid.
        ENDIF.
     ENDIF.
ENDIF
* UPDATED code for N:1 where i call the source entity to get key

I have 3 completely different working solutions for this catering for different scenarios. <Still work needed to re-factor to one generic>
SELECT SINGLE * FROM PRODUCT WHERE PRODUCTID EQ 2

And you end up with

Advertisements

Written by rsol1

January 17, 2012 at 2:08 pm

Posted in Uncategorized

Tagged with , , ,

One Response

Subscribe to comments with RSS.

  1. Just worked out why I had three solutions for the last scenario, my simple example broke N:1 association, will update blog when i find the better approach.

    UPDATE: added pseudo code for N:1, still think there are a few more iterations

    rsol1

    January 18, 2012 at 11:19 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: