Skip to end of metadata
Go to start of metadata

Author: Yajuvendrasinh Raulji & Jeet Bhatt
Submitted: 06/05/2007

Adding WBS Element in Proper Hierarchy from a Text File.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
*&---------------------------------------------------------------------*
*& Include ZBAPIWBS
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Report Z_BAPI_CJ20N
*&---------------------------------------------------------------------*

REPORT z_bapi_cj20n.

INCLUDE zbapicj20ntop.

INCLUDE zbapicj20nfileup.

INCLUDE zbapicj20nlogic.

*&---------------------------------------------------------------------*
*& Include ZBAPICJ20NTOP
*&---------------------------------------------------------------------*

DATA: BEGIN OF itmain OCCURS 0,
  project_definition TYPE ps_pspid,
  description TYPE ps_post1,
  project_profile TYPE profidproj,
  bus_area TYPE gsber,
  wbs_element TYPE ps_posid,
  wbs_description TYPE ps_post1,
END OF itmain,
wa_main LIKE LINE OF itmain,
itwbs LIKE STANDARD TABLE OF itmain WITH HEADER LINE,
itmain2 LIKE STANDARD TABLE OF itmain.

DATA: itprjdef TYPE STANDARD TABLE OF bapi_project_definition WITH HEADER LINE,
      wa_prjdef LIKE LINE OF itprjdef.

DATA: itprjdefup TYPE STANDARD TABLE OF bapi_project_definition_up WITH HEADER LINE,
      wa_prjdefup LIKE LINE OF itprjdefup.

DATA: itmetprj TYPE STANDARD TABLE OF bapi_method_project,
      wa_metprj LIKE LINE OF itmetprj.

DATA: return TYPE bapireturn1,msg TYPE STANDARD TABLE OF
        bapi_meth_message WITH HEADER LINE.

DATA: lines TYPE i,refno TYPE i VALUE 0.
DESCRIBE TABLE itmain LINES lines.
DATA: mettype TYPE string VALUE 'Create'.

DATA: wa_dupli LIKE LINE OF itmain.
wa_dupli-project_definition = 'JB'.

DATA: split1(50) TYPE c.
DATA: prevwbs TYPE ps_posid VALUE ' ',
      leftwbs TYPE ps_posid VALUE ' '.
DATA: pos TYPE i VALUE 1.
DATA: off TYPE i VALUE 0,mode TYPE i,index1 TYPE i VALUE 0,
      index2 TYPE i VALUE 0,index3 TYPE i VALUE 0,
      index4 TYPE i VALUE 0,index5 TYPE i VALUE 0,
      pos2 TYPE i VALUE 0,flag TYPE i VALUE 0.

DATA: itbus TYPE STANDARD TABLE OF bapi_bus2054_new,
      wa_bus LIKE LINE OF itbus, ret TYPE STANDARD TABLE OF bapiret2,
      wa_ret LIKE LINE OF ret, itbus2 LIKE STANDARD TABLE OF bapi_bus2054_new,
      wa_bus2 LIKE LINE OF itbus, wa_bus3 LIKE LINE OF itbus.

DATA: bapiret TYPE bapireturn1,
      projex TYPE bapi_project_definition_ex.

*&---------------------------------------------------------------------*
*& Include ZBAPICJ20NFILEUP
*&---------------------------------------------------------------------*

CALL FUNCTION 'UPLOAD'
  EXPORTING
    filename = ' '
    filetype = 'DAT'
  TABLES
    data_tab = itmain.

*&---------------------------------------------------------------------*
*& Include ZBAPICJ20NLOGIC
*&---------------------------------------------------------------------*

SORT itmain BY project_definition wbs_element.

LOOP AT itmain INTO wa_main.

  IF wa_dupli-project_definition <> wa_main-project_definition.

    wa_prjdef-project_definition = wa_main-project_definition.
    wa_prjdef-description = wa_main-description.
    wa_prjdef-project_profile = wa_main-project_profile.
    wa_prjdef-bus_area = wa_main-bus_area.

    wa_prjdefup-project_definition = 'X'.
    wa_prjdefup-description = 'X'.
    wa_prjdefup-project_profile = 'X'.
    wa_prjdefup-bus_area = 'X'.

    wa_metprj-refnumber = refno + 1 .
    wa_metprj-objecttype = 'ProjectDefinition'.
    wa_metprj-method = mettype.
    wa_metprj-objectkey = wa_main-project_definition.
    APPEND wa_metprj TO itmetprj.

    wa_metprj-refnumber = ''.
    wa_metprj-objecttype = ''.
    wa_metprj-method = 'Save'.
    wa_metprj-objectkey = ''.
    APPEND wa_metprj TO itmetprj.

    CALL FUNCTION 'BAPI_PROJECT_MAINTAIN'
      EXPORTING
        i_project_definition     = wa_prjdef
        i_project_definition_upd = wa_prjdefup
      IMPORTING
        return                   = return
      TABLES
        i_method_project         = itmetprj
        e_message_table          = msg.

    IF sy-subrc = 0.
      flag = 1.
    ENDIF.
    wa_dupli-project_definition = wa_main-project_definition.

    COMMIT WORK.
    DELETE itmetprj FROM 1 TO 2.

    INCLUDE zbapiwbs_all.

  ENDIF.
ENDLOOP.

*&---------------------------------------------------------------------*
*& Include ZBAPIWBS_ALL
*&---------------------------------------------------------------------*
LOOP AT itmain INTO wa_main WHERE project_definition = wa_dupli-project_definition.
  APPEND wa_main TO itwbs.
ENDLOOP.

*Sort the table
LOOP AT itwbs INTO wa_main.
  split1 = wa_main-wbs_element.
  pos = 0.
  LOOP AT itwbs WHERE wbs_element <> split1.

    IF strlen( itwbs-wbs_element ) = strlen( split1 ).
      IF pos = 0.
        APPEND wa_main TO itmain2.
      ENDIF.
      APPEND itwbs TO itmain2.
      DELETE itwbs WHERE wbs_element EQ itwbs-wbs_element.
      pos = 1.
    ELSE.
      IF pos = 0.
        APPEND wa_main TO itmain2.
        DELETE itwbs WHERE wbs_element EQ wa_main.
      ENDIF.
      pos = 1.
    ENDIF.

  ENDLOOP.
  index1 = index1 + 1.
ENDLOOP.

*Define UP element
LOOP AT itmain2 INTO wa_main.

  wa_bus-wbs_element = wa_main-wbs_element.
  wa_bus-description = wa_main-wbs_description.
  wa_bus-wbs_up = ''.
  wa_bus-wbs_left = ''.
  split1 = wa_main-wbs_element.
  LOOP AT itmain2 INTO wa_main WHERE wbs_element <> split1.
    IF strlen( wa_main-wbs_element ) < strlen( split1 ) .
      FIND wa_main-wbs_element IN split1.
      IF sy-subrc = 0.
        wa_bus-wbs_up = wa_main-wbs_element.
        prevwbs = wa_main-wbs_element.
      ENDIF.
    ENDIF.
  ENDLOOP.
  APPEND wa_bus TO itbus.
  index2 = index2 + 1.
ENDLOOP.

prevwbs = ''.
leftwbs = ''.

* Define LEFT ELEMENT.
LOOP AT itmain2 INTO wa_main.

  pos = 0.
  split1 = wa_main-wbs_element.
  READ TABLE itbus INTO wa_bus2 WITH KEY wbs_element = wa_main-wbs_element.
  wa_bus-wbs_element = split1.
  wa_bus-description = wa_main-wbs_description.
  wa_bus-wbs_up = wa_bus2-wbs_up.
  off = sy-tabix.

  LOOP AT itmain2 INTO wa_main WHERE wbs_element <> split1.

    READ TABLE itbus INTO wa_bus3 WITH KEY wbs_element = wa_main-wbs_element.

    IF strlen( wa_bus2-wbs_element ) = strlen( wa_bus3-wbs_element ) AND
    wa_bus2-wbs_up = wa_bus3-wbs_up AND pos = 0.

      IF strlen( leftwbs ) <> strlen( split1 ).
        leftwbs = ''.
      ENDIF.

      IF strlen( leftwbs ) = strlen( split1 ).
        FIND wa_bus2-wbs_up IN leftwbs.
        IF sy-subrc <> 0.
          leftwbs = ''.
        ENDIF.
      ENDIF.

      wa_bus-wbs_left = leftwbs.
      leftwbs = split1.
      pos = 1.
      MODIFY itbus INDEX off FROM wa_bus.
    ENDIF.
  ENDLOOP.

ENDLOOP.

wa_bus-wbs_element = ''.
APPEND wa_bus TO itbus.

* Store WBS ELEMENTS.
LOOP AT itbus INTO wa_bus.
  split1 = wa_bus-wbs_element.
  APPEND wa_bus TO itbus2.
  index3 = 1.
  pos = 0.

  LOOP AT itbus INTO wa_bus WHERE wbs_element <> split1.

    IF strlen( wa_bus-wbs_element ) <> strlen( split1 ) AND pos = 0.
      pos = 1.
      index4 = index3.

      CALL FUNCTION 'BAPI_PS_INITIALIZATION' .

      CALL FUNCTION 'BAPI_BUS2054_CREATE_MULTI'
        EXPORTING
          i_project_definition = wa_dupli-project_definition
        TABLES
          it_wbs_element       = itbus2
          et_return            = ret.

      CALL FUNCTION 'BAPI_PS_PRECOMMIT'
        TABLES
          et_return = ret.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait   = 'X'
        IMPORTING
          return = wa_ret.

    ENDIF.
    APPEND wa_bus TO itbus2.
    index3 = index3 + 1.
  ENDLOOP.

  DELETE itbus2 FROM 1 TO index3.
  DELETE itbus FROM 1 TO index4.

ENDLOOP.

DELETE itwbs FROM 1 TO index1.
CLEAR itwbs.
DELETE itmain2 FROM 1 TO index2.

*At the end of all projects' processing
IF flag = 1.

  CALL FUNCTION 'POPUP_FOR_INTERACTION'
    EXPORTING
      headline = 'PS Project & WBS Creation'
      text1    = 'All the Projects have been created.Please check......'
      ticon    = 'I'
      button_1 = 'OK'.

ELSE.

  CALL FUNCTION 'POPUP_FOR_INTERACTION'
    EXPORTING
      headline = 'Jeet Program'
      text1    = 'No Projects were created...'
      ticon    = 'I'
      button_1 = 'CANCEL'.
ENDIF.