The business scenario:

We use product hierarchy from ECC for our SAP Companies. Our non SAP Companies use a subset of the products for reporting purposes. We only want to provide a shorter hierarchy to them.

 

The goal:

To create a product hierarchy based on an attribute that is flagged and indicates if it is relevant for the shorter version of the new hierarchy or not.
We also exchanged some texts of the hierarchy along the way. This shall all be fully automated via process chain.

Approach:

We started off with the help of http://wiki.sdn.sap.com/wiki/display/BI/Hierarchy+Download+to+Flatfile and enhanced to meet our requirements.

The processchain loads the original hierarchy first; extracts the middle texts we exchange into DSO then we have the ABAP programm wit RC creating the shorter hierarchy and download it to a flat file. From there an infopackage is loading and activating the new shorter hierarchy.

Some exlainations to the code:

REPORT ZBW_BPS_PRODHIER_DOWNLOAD               .
TYPE-POOLS: rs, rsdm, rrh1.
CONSTANTS: c_DIR_ALIAS TYPE STRING VALUE ‘DIR_BWDATA’,
           c_repname TYPE STRING VALUE ‘ZBW_BPS_PRODHIER_DOWNLOAD’.

* File structure
TYPES:

* No dates/intervals

BEGIN OF y_s_hierfile_1,
  nodeid TYPE rshienodid,
  iobjnm TYPE rsiobjnm,
  nodename TYPE rsnodename,
  link TYPE rslink,
  parentid TYPE rsparent,
  langu TYPE langu,
  txtsh TYPE rstxtsh,
  txtmd TYPE rstxtmd,
  txtlg TYPE rstxtlg,
END OF y_s_hierfile_1,
y_t_hierfile_1 TYPE STANDARD TABLE OF y_s_hierfile_1.

DATA: lr_hiesel TYPE rsndi_s_hiesel,
      lr_hiedir TYPE rsndi_s_hiedir,
      lr_hiekey TYPE RSNDI_S_HIEKEY,
      lt_hiedirt TYPE TABLE OF rshiedirt,
      lt_hierstruc TYPE TABLE OF rssh_s_htab,
      lr_hierstruc TYPE rssh_s_htab,
      lt_thiernode TYPE TABLE OF rsthiernode
        WITH KEY langu hieid objvers nodename,
      lr_thiernode TYPE rsthiernode,
      lt_hierintvl TYPE TABLE OF rssh_s_jtab
        WITH KEY hieid objvers nodeid,
      lt_message TYPE TABLE OF rsndi_s_message,
      lr_message TYPE rsndi_s_message,
      lr_chavlinfo TYPE rsdm_s_chavlinfo,
      lt_chavlinfo TYPE rsdm_t_chavlinfo,
      l_subrc TYPE SYSUBRC,
      l_txtmd TYPE RSTXTMD,
      l_zmm_acm TYPE /BIC/OIZMM_ACM,

      lr_file TYPE y_s_hierfile_1,
      lt_file TYPE y_t_hierfile_1,
      lr_file2 TYPE y_s_hierfile_1,
      lt_file2 TYPE y_t_hierfile_1,

      lr_user_dir TYPE USER_DIR,
      l_filename TYPE STRING,
      t_ref type ref to CX_SY_FILE_AUTHORITY,
      err_txt type string,

      lr_report_status TYPE ZRSPC_REP_STATUS,
      l_hienm TYPE rshiedirhienm VALUE ‘M20 BW 2007’, “<-Name of the original hierarchy
      l_hieid TYPE RSHIEID.

* read hierarchy id for M20 BW 07 hierarchy
SELECT SINGLE HIEID FROM RSHIEDIR
  INTO l_hieid
  WHERE HIENM = l_hienm AND
        OBJVERS = ‘A’ AND
        IOBJNM = ‘0PROD_HIER’. <- Use your Hierarchy object

lr_hiekeyhieid = l_hieid.
lr_hiekeyobjvers = rs_c_objversactive.
lr_hieselobjvers = rs_c_objversactive.
lr_hieselhienm = l_hienm.
lr_hieseliobjnm = ‘0PROD_HIER’. <- Use your Hierarchy object
lr_hieseldateto = ‘99991231’.

CALL FUNCTION ‘RSNDI_SHIE_STRUCTURE_GET’
  EXPORTING
    I_S_HIEKEY        = lr_hiekey
    i_s_hiesel        = lr_hiesel
    i_no_nodenm_table = rs_c_true
  IMPORTING
    e_s_hiedir        = lr_hiedir
    e_subrc           = l_subrc
  TABLES
    e_t_hiedirt       = lt_hiedirt
    e_t_hierstruc     = lt_hierstruc
    e_t_thiernode     = lt_thiernode
    e_t_hierintvl     = lt_hierintvl
    e_t_message       = lt_message.

IF l_subrc <> 0.
  READ TABLE lt_message INTO lr_message INDEX 1.
  IF sysubrc = 0.
    MESSAGE ID lr_messagemsgid TYPE ‘I’ NUMBER lr_messagemsgno
      WITH lr_messagemsgv1 lr_messagemsgv2
           lr_messagemsgv3 lr_messagemsgv4.
  ELSE.
    MESSAGE ID ‘RSBO’ TYPE ‘I’ NUMBER 899
      WITH ‘Hierarchy read error’.
  ENDIF.
  EXIT.
ENDIF.

* Nodes
REFRESH lt_file.

LOOP AT lt_hierstruc INTO lr_hierstruc.
  CLEAR lr_file.
  MOVE-CORRESPONDING lr_hierstruc TO lr_file.

* Texts for nodes
  READ TABLE lt_thiernode INTO lr_thiernode WITH TABLE KEY
    langu = ‘E’ hieid = lr_hierstruchieid
    objvers = rs_c_objversactive nodename = lr_hierstrucnodename.
  IF sysubrc = 0.
    MOVE-CORRESPONDING lr_thiernode TO lr_file.
  ELSE.

* Texts for characteristic values
    REFRESH lt_chavlinfo.
    CLEAR lr_chavlinfo.
    lr_chavlinfoc_chavl = lr_hierstrucnodename.
    APPEND lr_chavlinfo TO lt_chavlinfo.

    CALL FUNCTION ‘RSD_CHAVL_READ_ALL’
      EXPORTING
        i_iobjnm                  = lr_hierstruciobjnm
        i_langu                   = ‘E’
        i_dateto                  = ‘99991231’
        i_check_value             = space
        i_sid_in                  = space
        i_hieid                   = lr_hiedirhieid
        i_objvers                 = lr_hiedirobjvers
      CHANGING
        c_t_chavlinfo             = lt_chavlinfo
      EXCEPTIONS
        info_object_not_found     = 1
        routines_generation_error = 2
        check_table_not_existing  = 3
        text_table_not_existing   = 4
        OTHERS                    = 5.
    IF sysubrc = 0.
      READ TABLE lt_chavlinfo INTO lr_chavlinfo INDEX 1.
      IF sysubrc = 0.
        MOVE-CORRESPONDING lr_chavlinfoe_chatexts TO lr_file.
      ENDIF.
    ENDIF.
  ENDIF.

* We add special text for some of the hierarchy nodes
  IF lr_fileiobjnm = ‘0HIER_NODE’.
    IF lr_fileNODENAME(31) = ‘0000000000000000000000000000000’.
      lr_fileNODENAME = lr_fileNODENAME+31(1).
    ENDIF.

    SELECT SINGLE TXTMD FROM /BIC/AZCOPAO0400 “<- read from dso
      INTO l_txtmd
      WHERE PROD_HIER = lr_filenodename.

    IF SYSUBRC = 0.
      lr_filetxtsh = l_txtmd(20).
      lr_filetxtmd = l_txtmd.
      lr_filetxtlg = l_txtmd.
    ENDIF.
  ENDIF.

  lr_filelangu = ‘E’.

*Attribute that determins if the node is in the shorter hierarchy
  IF lr_fileiobjnm <> ‘0HIER_NODE’.
    SELECT SINGLE /BIC/ZMM_ACM FROM /BI0/PPROD_HIER “<- use your IO /Hierarchy
      INTO l_zmm_acm
      WHERE PROD_HIER = lr_filenodename AND
            OBJVERS = ‘A’.

    IF l_zmm_acm <> ‘002’. “<- if the value in our case is 002 then keep it
      APPEND lr_file TO lt_file2.
    ENDIF.
  ENDIF.
  APPEND lr_file TO lt_file.
ENDLOOP.

LOOP AT lt_file2 INTO lr_file2.
* check wether the node is a parent. No removal possible if yes
  READ TABLE lt_file TRANSPORTING NO FIELDS
    WITH KEY PARENTID = lr_file2NODEID.

  IF SYSUBRC <> 0.
    READ TABLE lt_file TRANSPORTING NO FIELDS
      WITH KEY NODEID = lr_file2NODEID.
    DELETE lt_file INDEX SYTABIX.
  ENDIF.
ENDLOOP.

* remove unnecessary text nodes from the hierarchy (empty ones)
lt_file2 = lt_file.
LOOP AT lt_file2 INTO lr_file2
  WHERE IOBJNM = ‘0HIER_NODE’.
* check wether the node is a parent. No removal possible if yes
  READ TABLE lt_file TRANSPORTING NO FIELDS
    WITH KEY PARENTID = lr_file2NODEID.
  IF SYSUBRC <> 0.
    READ TABLE lt_file TRANSPORTING NO FIELDS
      WITH KEY NODEID = lr_file2NODEID.
    DELETE lt_file INDEX SYTABIX.
  ENDIF.
ENDLOOP.

* get user directory
SELECT SINGLE * FROM USER_DIR
  INTO lr_user_dir
  WHERE ALIASS = c_DIR_ALIAS.

* Back to downloading the hierarchy to a text file
IF SYSUBRC = 0.
CONCATENATE lr_user_dirdirname ‘\PROD_HIER_HIER_BPS.txt’
    INTO l_filename.
  try.
    OPEN DATASET l_filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
    catch CX_SY_FILE_AUTHORITY INTO t_ref.
      err_txt = t_ref->get_longtext( ).
  endtry.

  IF SYSUBRC = 0.
    LOOP AT lt_file INTO lr_file.
      TRANSFER lr_file TO l_filename.
    ENDLOOP.
  ELSE.
    l_subrc = 2.
  ENDIF.
  CLOSE DATASET l_filename.
ENDIF.

* update status table with return code for process chain type
* report with RC
lr_report_statusreport = c_repname.
lr_report_statusstatus = l_subrc.
INSERT INTO ZRSPC_REP_STATUS values lr_report_status.
IF SYSUBRC <> 0.
  UPDATE ZRSPC_REP_STATUS FROM lr_report_status.
ENDIF.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply