How-to Enforce DTP Semantic Group

Have you ever had a support ticket raised against a solution you delivered, only to find that it’s not broken but someone has changed it without realising the consequences?

Data Transfer Process (DTP)

The worst thing to happen is when that change doesn’t stop the nightly load window but turns up as poor quality data that has been made available for reporting.

When the DTP configuration uses Semantic Groups to drive business rules within a Transformation, it is usually done by the BW Developer intentionally and for a very good reason. In the end, it always comes back to a need to process simular records together as a group while processing a DataPacket.

For Example: A time-dependant DataSet is converted to time-independant.

For Example: Reducing a DataSet to a smaller group of nominated representative records.

For Example: Optimised business rule processing because all related records are processed together, resulting in fewer lookups into external resources.

In these circumstances, the Semantic Group configuration in the DTP is considered mandatory and should always be used as originally configured, no changes allowed.

Use this solution in a Transformation Start Routine to enforce a DTP Semantic group configuration. The DTP will always fail to load (Abort) until the DTP Semantic Group configuration is restored.

Method ZCL_TRFN_SEMANTIC_GROUP->CHECK

METHOD check.
*--------------------------------------------------------------------*
* Code Name:   zcl-trfn-semantic-group-check.                        *
* Code URI:    http://lnxbw.co/edsg-cod-1                            *
* Description: Check DTP Semantic Group configuration against        *
*              expectations in a SAP BW Transformation.              *
* Version:     1.0.0                                                 *
* Author:      Bob Sarmany and John Lang                             *
* Author URI:  https://www.learnbw.com/                              *
* License:     BSD-New                                               *
* License URI: http://opensource.org/licenses/BSD-3-Clause           *
*--------------------------------------------------------------------*
* Copyright (c) 2015, www.LearnBW.com. All rights reserved.          *
*                                                                    *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met:                                                           *
*                                                                    *
* 1. Redistributions of source code must retain the above copyright  *
* notice, this list of conditions and the following disclaimer.      *
*                                                                    *
* 2. Redistributions in binary form must reproduce the above         *
* copyright notice, this list of conditions and the following        *
* disclaimer in the documentation and/or other materials provided    *
* with the distribution.                                             *
*                                                                    *
* 3. Neither the name of the copyright holder nor the names of its   *
* contributors may be used to endorse or promote products derived    *
* from this software without specific prior written permission.      *
*                                                                    *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             *
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           *
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           *
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS *
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,           *
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED    *
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,      *
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON  *
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,    *
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT *
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *
* SUCH DAMAGE.                                                       *
*--------------------------------------------------------------------*
********************************************************************************
* Class : ZCL_TRFN_SEMANTIC_GROUP                                              *
* Method: CHECK                                                                *
* Date  : 2015                                                                 *
* Author: Bob Sarmany                                                          *
********************************************************************************
* Description:
* ------------
* Check the validity of the DTP Semantic Group definition being executed.
*
* Example Usage:
* ==============
*
*  DATA:
*    lt_fldlist TYPE zcl_trfn_semantic_group=>tty_field_list
*  , ls_fldname TYPE zcl_trfn_semantic_group=>ty_field_list
*  .
*
*  * Populate field names that are expected in the semantic group.
*    refresh lt_fldlist.
*    ls_fldlist-fieldname = 'MATERIAL'. insert ls_fldlist into table lt_fldlist.
*    ls_fldlist-fieldname = 'CALDAY'.   insert ls_fldlist into table lt_fldlist.
*  
*  * Ensure only the expected field names are in the semantic group.
*    CALL METHOD zlbw_trfn_semantic_group=>check
*      EXPORTING
*        i_p_r_request                = p_r_request
*        it_fieldlist                 = lt_fldlist
*      EXCEPTIONS
*        no_grouping_defined          = 1
*        grouping_incorrectly_defined = 2
*        field_not_found_in_group     = 3
*        others                       = 4.
*  
*  * Abort the Transformation.
*    IF sy-subrc <> 0.
*      clear monitor_rec.
*      monitor_rec-msgid = 'RSTRAN'.
*      monitor_rec-msgty = 'E'.
*      monitor_rec-msgno = '826'.
*      monitor_rec-msgv1 = 'Semantic grouping error. The following'.
*      monitor_rec-msgv2 = 'fields must be in the semantic group:'.
*      monitor_rec-msgv3 = 'MATERIAL,CALDAY.'.
*      monitor_rec-msgv4 = ''.   "<- More field names if needed.
*      APPEND monitor_rec TO monitor.
*      RAISE EXCEPTION TYPE cx_rsrout_abort.
*    ENDIF.
********************************************************************************

  DATA:
    lr_dtp         TYPE REF TO cl_rsbk_dtp
  , lv_dtp_name    TYPE rsbkdtpnm
  , lt_groupfields TYPE rsbk_tx_fields_keyfl
  , lv_fieldcount  TYPE i
  .

  FIELD-SYMBOLS:
    <lfs_groupfields> TYPE rsbk_sx_fields_keyfl
  , <lfs_fieldlist>   TYPE rsbk_t_fields_keyfl
  , <lfs_fieldname>   TYPE rsbk_s_fields_keyfl
  .

* Use the in-built class to get the current DTP name.
  lv_dtp_name = i_p_r_request->get_dtp( ).

  lr_dtp = cl_rsbk_dtp=>factory( lv_dtp_name ).
  lr_dtp->get_obj_ref_display( ).

* Validate that semantic grouping is correct.
* Get grouping field list.
  CALL METHOD lr_dtp->get_groupfields
    IMPORTING
      e_t_groupfields = lt_groupfields.

* Get number of expected fields in semantic group.
  lv_fieldcount = lines( it_fieldlist ).

* First check if any semantic grouping has been defined.
  READ TABLE lt_groupfields ASSIGNING <lfs_groupfields>
                                INDEX 1.
  IF sy-subrc <> 0.
    RAISE no_grouping_defined.

  ELSE.

    ASSIGN <lfs_groupfields>-t_fields TO <lfs_fieldlist>.

*   Check that we have the correct number of fields defined.
    IF lines( <lfs_fieldlist> ) <> lv_fieldcount.
      RAISE grouping_incorrectly_defined.

    ELSE.

*     Now ensure that every field matches with what is expected.
      LOOP AT <lfs_fieldlist> ASSIGNING <lfs_fieldname>.
        READ TABLE it_fieldlist 
             WITH TABLE KEY fieldname = <lfs_fieldname>-fieldname
             TRANSPORTING NO FIELDS.
        IF sy-subrc <> 0.
          RAISE field_not_found_in_group.
        ENDIF.
      ENDLOOP.

    ENDIF.
  ENDIF.

ENDMETHOD.

Implemented in 3 easy steps:

  1. Save the CHECK method code snippet;
  2. Create the ZCL_TRFN_SEMANTIC_GROUP ABAP Class;
  3. Enhance Transformation with call to DTP Semantic Group check.

How did it go? Leave a comment below.