msc mobile emerging technologies blog

How to add Custom Annotations to Entity Properties in a SAP Gateway Model

Posted by Sandeep TP on Feb 22, 2016 2:00:00 PM

Within an implementation of the SAP SRM UI AddOn we recently had to add a Custom Annotation to an existing Gateway Service. In this blog we want to share how this is done. 


At times when building SAP OData Models there might be a need to add custom attributes to the properties of an Entity. Few Properties like filterable, sortable are available where the values can be set as true or false.

Annotations can be used to enrich the meta data of an attribute – setting the data type, adding default or min/max values.

Below is the metadata of the Entity PurchaseOrder which has three properties.

GW_II.png

The Code below shows how to add Custom Annotation to Property PO_SUPPLIER_ID.

In the class Builder (se24) in the DEFINE method of the model provider class add the following code (the custom annotation is highlighted yellow).

DATA: lo_entity_type          TYPE REF TO /iwbep/if_mgw_odata_entity_typ,
      lo_property             TYPE REF TO /iwbep/if_mgw_odata_property,
      lr_med_exception        TYPE REF TO /iwbep/cx_mgw_med_exception,
      lo_private_annotation   TYPE REF TO /iwbep/if_mgw_odata_annotation. "#EC NEEDED

  "  Create Entity
  lo_entity_type = model->create_entity_type( 'PurchaseOrder' ).

*********************** Object ID*********************************
  lo_property = lo_entity_type->create_property( iv_property_name   = 'OBJECT_ID'
                                                 iv_abap_fieldname  = 'OBJECT_ID' ).
  lo_property->set_is_key( ).
  lo_property->set_conversion_exit( 'ALPHA' ).
  lo_property->set_sortable( abap_false ).

*********************** Description*********************************
  lo_property = lo_entity_type->create_property( iv_property_name   = 'DESCRIPTION'
                                                 iv_abap_fieldname  = 'DESCRIPTION' ).
  lo_property->set_type_edm_string( ).
  lo_property->set_filterable( abap_false ).
  lo_property->set_sortable( abap_true ).

*********************** Supplier ID*********************************
 lo_property = lo_entity_type->create_property( iv_property_name   = 'PO_SUPPLIER_ID'
                                                iv_abap_fieldname  = 'PO_SUPPLIER_ID' ).

  lo_property->set_conversion_exit( 'ALPHA' ).
  lo_property->set_filterable( abap_true ).
  lo_property->set_sortable( abap_false ).

  lo_private_annotation = lo_property->/iwbep/if_mgw_odata_annotatabl~create_annotation ( /iwbep/if_mgw_med_odata_types=>gc_sap_namespace ).
  lo_private_annotation->add( iv_key = 'CUST_PROP_1' iv_value = 'true' ).
  lo_private_annotation->add( iv_key = 'CUST_PROP_2' iv_value = 'false' ).
  lo_private_annotation->add( iv_key = 'CUST_PROP_3' iv_value = 'CUSTOM_STRING' ).

  TRY.
      lo_entity_type->bind_structure(
        EXPORTING
          iv_structure_name   = gcs_purchase_order-po_header_struc_name
      ).
    CATCH /iwbep/cx_mgw_med_exception INTO lr_med_exception.    " Meta data exception
      RAISE EXCEPTION lr_med_exception.
  ENDTRY.

After clearing the Gateway Model Cache, execute the metadata again and you will see the Custom Annotations added.

GW_II.png


 

This can of course not only be applied to the SRM UI AddOn, but also to any other use case, in which Gateway is used.


 

Topics: SRM, Gateway, Odata