Skip to Content
Technical Articles

Adding annotations to SEGW based OData service using Annotation Helper Class

Hi,

I started using smart controls a year and a half back and was surprised to experience that the mundane tasks of UI are handled so smoothly. Although I don’t say that smart controls are the perfect go to choice for all scenarios but if you are ready to challenge yourself a little, it would add another arsenal in your kitty.

If you are using CDS views, it’s fairly straight forward to add the required annotations but it’s quite a  journey to add the similar annotations in SEGW based OData service. To change the label of property itself is a quite a bit of work, not to mention the annotation terms that you must remember. So I and my colleague Cristian Babei started creating a class which helped us a lot in our projects.

We added methods for all the frequent use cases specially linked to smart filter bar and smart table in our class. Here is the link to the github repo for the class and sample code to use this class. You can copy the class code in source code based view of SE24.

It helps to serve the below cases:

  • Set a single or multiple properties as
    • filterable – true/false
    • updatable – true/false
    • sortable – true/false
    • creatable – true/false
  • Set display format for properties
    • date – to render datepicker control
    • time – to render timepicker control
  • Set as dropdown
    • to show a dropdown for a smartfield
  • Set as text
    • to set the description property for a ID field
  • Set an entity as media
  • Set a property as email
  • Set the unit of a property
  • Set label for a property
    • using hardcoded text – quick way
    • using SAP text
  • Set a filter as mandatory in filter bar
  • Set filter value restrictions
    • single value
    • multi value
    • interval like date range

Here are some samples on how we used it. Feel free to add more methods in this and send a PR.

    DATA : lo_odata TYPE REF TO zcl_odata_v2_annotations.

    super->define( ).

    CREATE OBJECT lo_odata
      EXPORTING
        im_model            = model
        im_vocab_anno_model = vocab_anno_model
        im_service_name     = 'Z_MY_SERVICE_srv'.


    lo_odata->set_filterable(
      EXPORTING
        im_entity                   = 'MyEntity'            " Entity Type
        im_properties               = 'FilterProperty1,FilterProperty2'  " List of properties of the entity separated by ','
        im_value                    = abap_false            " Value for Annotation (Boolean)
    ).

    lo_odata->set_updateable(
      EXPORTING
        im_entity                   = 'MyEntity'            " Entity Type
        im_properties               = 'CreatedOn,ChangedOn,SentOn,CreatedBy,ChangedBy'  " List of properties of the entity separated by ','
        im_value                    = abap_false            " Value for Annotation (Boolean)
    ).

    lo_odata->set_display_format(
      EXPORTING
        im_entity                   = 'MyEntity'            " Entity Type
        im_properties               = 'ChangedOn,SentOn,OpenedOn,ClosedOn,CreatedOn'  " List of properties of the entity separated by ','
        im_value                    = 'Date'                " Value for display-format
    ).

    lo_odata->set_as_text(
      EXPORTING
        im_entity                   = 'MyEntity'            " Entity Type
        im_property_key             = 'Vendor'            " Property Key
        im_property_desc            = 'VendorName'          " Property Description
    ).

    lo_odata->set_as_text(
      EXPORTING
        im_entity                   = 'MyEntityItem'        " Entity Type
        im_property_key             = 'Material'            " Property Key
        im_property_desc            = 'MaterialDesc'        " Property Description
    ).

    lo_odata->set_as_dropdown(
      EXPORTING
        im_entity_set               = 'SendMethodSet'       " Entity Set
        im_entity                   = 'SendMethod'          " ( Optional ) Entity Type
        im_property_key             = 'DomvalueL'           " ( Optional ) Property Key
        im_property_desc            = 'Ddtext'              " ( Optional ) Property Description
    ).

    lo_odata->set_as_media(
      EXPORTING
        im_entity                   = 'ExcelFile'           " Entity Type
        im_property_mimetype        = 'MimeType'            " MimeType Property Key
    ).

    lo_odata->set_label(
      EXPORTING
        im_entity                   = 'MyEntityItem'    " Entity Type
        im_property                 = 'Order'    " Property Key
        im_label                    = 'Ordered Quantity'    " Label Text
    ).

 

Regards,

Ekansh

 

3 Comments
You must be Logged on to comment or reply to a post.
  • Hi Ekansh Saxena Cristian Babei ,

    Great Content.

    I have a question on the text element annotation ( Method set_as_text ).

    Can we UI Smartfield bound to the ID property display the description/description( ID ) in display mode similar to when we build FIORI templates on CDS views.

    From my experience now, we need to bind the UI control to the description property and not the ID.

    Regards,

    Mounika

    • Hi Mounika,

      If I understood your query well, you want to use smartfield to display "description (id)" when in display mode. Yes, this is possible with combination of 'set_as_text' and 'value list' (in SEGW annotations for commons library). For example, you have two entities, SalesOrder and User. For SalesOrder-CreatedBy property, you need to define value list annotation to User entity and then in DEFINE method of MPC_EXT, use set_as_text for User entity to dictate that 'Name' is text of 'Username' field. Now on UI, if you bind SalesOrder-CreatedBy property to a smartfield and set custom data like below, if should work.

      <smartField:SmartField id="idCreatedBy" value="{CreatedBy}">
          <smartField:configuration>
              <smartField:Configuration displayBehaviour="descriptionAndId"></smartField:Configuration>
          </smartField:configuration>
      </smartField:SmartField>

      Regards,

      Ekansh