SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Upload formatted long text for Task List



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sun Oct 14, 2007 9:15 pm    Post subject: Upload formatted long text for Task List Reply with quote

Requirement:
Upload formatted long text for already created Task List from a tab delimited file.

Processing:
This program uploads formatted long text for Task List using FM 'SAVE_TEXT'.

Code:

REPORT Z_TASK_LIST_LTEXT_UPLOAD.
*----------------------------------------------------------------------*
*       Written By: Ram Manohar Tiwari                
*----------------------------------------------------------------------*
*       Presented By: http://www.rmtiwari.com
*----------------------------------------------------------------------*

********************************************************************
* This program is used to upload formatted long text for already
* created task lists.
********************************************************************

TABLES : tapl, plpo, plas.

CONSTANTS : lc_record_operation(3) TYPE c VALUE 'OPR'.

TYPES : BEGIN OF lv_text,
          rec(300) TYPE c,
        END OF lv_text.

DATA : BEGIN OF gt_tasklist_data OCCURS 0,
          record_id(3)  TYPE c,
          record_no(4)  TYPE n,
          tplnr      LIKE rc27e-tplnr,
          PLNNR      TYPE tapl-plnnr,
          PLNAL      TYPE tapl-plnal,
          VORNR      TYPE plpo-vornr,
          long_text_line(300) TYPE C,
          long_text  TYPE TABLE OF lv_text,
          STATUS(7)  TYPE C,
          MSG_TEXT(100) TYPE C,
       END OF gt_tasklist_data.

DATA : gt_tasklist_data_final like gt_tasklist_data occurs 0.

DATA  : BEGIN OF gt_upload OCCURS 0,
          rec(3000) TYPE c,
        END OF gt_upload.

DATA: BEGIN OF gt_header_fields OCCURS 0,
        rec(3000) TYPE c,
      END OF gt_header_fields.


DATA : BEGIN OF gt_tasklist_totalerr OCCURS 0,
          record_id(3)  TYPE c,
          record_no(4)  TYPE n,
          tplnr      LIKE rc27e-tplnr,
          PLNNR      TYPE tapl-plnnr,
          PLNAL      TYPE tapl-plnal,
          VORNR      TYPE plpo-vornr,
          longtext(6000)  TYPE C,
       END OF gt_tasklist_totalerr.

DATA : BEGIN OF gt_tasklist_report OCCURS 0,
         record_id(3)  TYPE c,
         record_no     TYPE i,
         tplnr        LIKE rc27e-tplnr,
         PLNNR        TYPE tapl-plnnr,
         PLNAL        TYPE tapl-plnal,
         VORNR      TYPE plpo-vornr,
         status     TYPE C,
         msg_text(100) TYPE C,

       END OF gt_tasklist_report.

TYPE-POOLS: slis.
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
      gs_layout   TYPE slis_layout_alv,
      gt_sort     TYPE slis_t_sortinfo_alv.

DATA : gv_file_name LIKE rlgrap-filename,
     : gv_extension(3) TYPE c,
     : wa_tasklist_data LIKE LINE OF gt_tasklist_data,
     : wa_longtext TYPE lv_text,
     : lc_newline TYPE x VALUE '0A',
     : lc_error(7) TYPE c VALUE 'ERROR',
     : lc_success(7) TYPE c VALUE 'SUCCESS',
     : gv_check_failed type C.
*------------------------------------------------------------*

* Start of Selection Screen to get file name.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-010.
PARAMETER p_infile LIKE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.


* At Selection Screen for F4 help on Filename.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_infile.
  PERFORM authorization_check.
  PERFORM get_file_name USING    'P_INFILE'
                        CHANGING gv_file_name.

*------------------------------------------------------------*
START-OF-SELECTION.

* Upload the data from file to internal table
  PERFORM read_inputfile_from_pc  TABLES gt_upload
                                         gt_tasklist_data
                                   USING p_infile.

* Check File data.
  PERFORM check_file_data.
   if gv_check_failed eq 'Y'.
    Message i090(z1).
    stop.
   endif.

* Prepare final internal table with text compressed in one itab field
* for same combination of key fields.
  PERFORM compress_text_data TABLES gt_tasklist_data
                                    gt_tasklist_data_final.


* Start Processing
  PERFORM process_file.

END-OF-SELECTION.

* To download the error file to the PC.
  PERFORM download_error_file.

* To display the error report.
  PERFORM alv_display.


*&---------------------------------------------------------------------*
*&      Form  read_inputfile_from_pc
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_UPLOAD  text
*----------------------------------------------------------------------*
FORM read_inputfile_from_pc
                      TABLES yt_upload STRUCTURE gt_upload
                             yt_tasklist_data STRUCTURE gt_tasklist_data
                       USING y_infile.

  CONSTANTS :
    lc_tab TYPE x VALUE '09'.
  DATA :
    lv_length TYPE i,
    lv_counter TYPE i,
    lv_operation_index TYPE i,
    lv_longtext(300) TYPE c,
    lv_data(300) TYPE c,
    lv_descrip LIKE plpod-ltxa1,
    lv_index LIKE sy-index,
    lt_long_text TYPE TABLE OF lv_text.

* Uploading the file.
  CALL FUNCTION 'WS_UPLOAD'
       EXPORTING
            filename = y_infile
            filetype = 'ASC'
       TABLES
            data_tab = yt_upload.

  LOOP AT yt_upload.

    lv_counter = lv_counter + 1.
    IF lv_counter <> 1.

      IF yt_upload+0(3) EQ lc_record_operation.

*       Spliting the record into respective fields
        SPLIT yt_upload AT lc_tab INTO
          yt_tasklist_data-record_id
          yt_tasklist_data-record_no
          yt_tasklist_data-tplnr
          yt_tasklist_data-PLNNR
          yt_tasklist_data-PLNAL
          yt_tasklist_data-VORNR
          yt_tasklist_data-long_text_line.

        APPEND yt_tasklist_data.

        IF yt_tasklist_data-record_id = lc_record_operation.
          DESCRIBE TABLE yt_tasklist_data LINES lv_operation_index.
          lv_longtext = yt_tasklist_data-long_text_line.
          APPEND lv_longtext TO lt_long_text.
        ENDIF.
      ELSE.

*       Saving Long text for each operation.
        lv_longtext = yt_upload.
        APPEND lv_longtext TO lt_long_text.
      ENDIF.
*       This is to check if the next record is a Operation Record
*       If yes then save long text to that Operation.
        lv_index = lv_counter + 1.
        READ TABLE yt_upload INDEX lv_index INTO lv_data.
        IF lv_data+0(3) EQ lc_record_operation.
          CLEAR yt_tasklist_data.
          READ TABLE yt_tasklist_data INDEX lv_operation_index.
          yt_tasklist_data-long_text[] = lt_long_text[].
          MODIFY yt_tasklist_data INDEX lv_operation_index.
          CLEAR yt_tasklist_data.
          CLEAR lv_operation_index.
          CLEAR lt_long_text[].
        ENDIF.

    ELSE.

*     Spliting the field labels into respective fields
      SPLIT yt_upload AT lc_tab INTO TABLE gt_header_fields.

    ENDIF.

    AT LAST.

*     If the Last record is an Operation Record then save the long text.
      IF yt_tasklist_data-record_id EQ lc_record_operation.
        CLEAR yt_tasklist_data.
        READ TABLE yt_tasklist_data INDEX lv_operation_index.
        yt_tasklist_data-long_text[] = lt_long_text[].
        MODIFY yt_tasklist_data INDEX lv_operation_index.
        CLEAR yt_tasklist_data.
        CLEAR lv_operation_index.
        CLEAR lt_long_text[].
      ENDIF.

    ENDAT.

  ENDLOOP.


ENDFORM.                    " read_inputfile_from_pc
*&---------------------------------------------------------------------*
*&      Form  alv_display
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM alv_display.

  IF gt_fieldcat[] IS INITIAL.
    PERFORM fieldcat_init.
    PERFORM layout_init.
    PERFORM sort_init.
  ENDIF.

* Display an ALV report of the error records.
  PERFORM grid_display.

ENDFORM.                    " alv_display
*&---------------------------------------------------------------------*
*&      Form  fieldcat_init
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fieldcat_init.

  DATA: ls_fieldcat TYPE slis_fieldcat_alv.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname    = 'RECORD_NO'.
  ls_fieldcat-reptext_ddic = 'Record No'.
  ls_fieldcat-outputlen    = 10.
  APPEND ls_fieldcat TO gt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname    = 'TPLNR'.
  ls_fieldcat-key          = 'X'.
  ls_fieldcat-reptext_ddic = 'Functional Location'.
  ls_fieldcat-outputlen    = 45.
  ls_fieldcat-inttype = 'N'.
  APPEND ls_fieldcat TO gt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname    = 'PLNNR'.
  ls_fieldcat-reptext_ddic = 'Group'.
  ls_fieldcat-outputlen    = 12.
  APPEND ls_fieldcat TO gt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname    = 'PLNAL'.
  ls_fieldcat-reptext_ddic = 'Group Counter'.
  ls_fieldcat-outputlen    = 10.
  APPEND ls_fieldcat TO gt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname    = 'VORNR'.
  ls_fieldcat-reptext_ddic = 'Operation Number'.
  ls_fieldcat-outputlen    = 10.
  APPEND ls_fieldcat TO gt_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname    = 'STATUS'.
  ls_fieldcat-reptext_ddic = 'Status'.
  ls_fieldcat-outputlen    = 6.
  APPEND ls_fieldcat TO gt_fieldcat.


  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname    = 'MSG_TEXT'.
  ls_fieldcat-reptext_ddic = 'Message'.
  ls_fieldcat-outputlen    = 100.
  APPEND ls_fieldcat TO gt_fieldcat.

ENDFORM.                    " fieldcat_init
*&---------------------------------------------------------------------*
*&      Form  layout_init
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM layout_init.

  gs_layout-zebra               = 'X'.
  gs_layout-cell_merge          = 'X'.
  gs_layout-colwidth_optimize   = 'X'.
  gs_layout-no_vline            = ' '.
  gs_layout-totals_before_items = ' '.

ENDFORM.                    " layout_init
*&---------------------------------------------------------------------*
*&      Form  sort_init
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM sort_init.

  DATA: ls_sort TYPE slis_sortinfo_alv.

  CLEAR ls_sort.
  ls_sort-fieldname = 'RECORD_NO'.
  ls_sort-spos      = 1.
  ls_sort-up        = 'X'.
  APPEND ls_sort TO gt_sort.

  CLEAR ls_sort.
  ls_sort-fieldname = 'TPLNR'.
  ls_sort-spos      = 2.
  ls_sort-up        = 'X'.
  APPEND ls_sort TO gt_sort.

  CLEAR ls_sort.
  ls_sort-fieldname = 'PLNNR'.
  ls_sort-spos      = 3.
  ls_sort-up        = 'X'.
  APPEND ls_sort TO gt_sort.

  CLEAR ls_sort.
  ls_sort-fieldname = 'PLNAL'.
  ls_sort-spos      = 4.
  ls_sort-up        = 'X'.
  APPEND ls_sort TO gt_sort.

  CLEAR ls_sort.
  ls_sort-fieldname = 'STATUS'.
  ls_sort-spos      = 5.
  ls_sort-up        = 'X'.
  APPEND ls_sort TO gt_sort.



ENDFORM.                    " sort_init
*&---------------------------------------------------------------------*
*&      Form  grid_display
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM grid_display.

  DELETE gt_tasklist_report WHERE msg_text = ' '.
  SORT gt_tasklist_report BY record_no tplnr .

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
            is_layout     = gs_layout
            it_fieldcat   = gt_fieldcat
            it_sort       = gt_sort
            i_default     = ' '
            i_save        = 'X'
       TABLES
            t_outtab      = gt_tasklist_report
       EXCEPTIONS
            program_error = 1
            OTHERS        = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
         WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.                    " grid_display
*&---------------------------------------------------------------------*
*&      Form  save_long_text
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_TASKLIST_PROCESS  text
*----------------------------------------------------------------------*
FORM save_long_text
      USING   yt_tasklist_process LIKE LINE OF gt_tasklist_data.

  DATA : lv_name      LIKE stxh-tdname,
         lv_object    LIKE tca09-objopr,
         lv_id        LIKE opr_class_data-ident,
         lv_err_text  TYPE bad_patch-descript,
         lv_suc_text  TYPE bad_patch-descript,
         lv_ecn_s     LIKE tsk_class_data-aennr VALUE ' ',
         lv_keydate   LIKE sy-datum,
         lc_operation(3) TYPE c    VALUE 'OPR',
         tca01     TYPE tca01,
         tca09     TYPE tca09,
         lv_longtext(300) TYPE c.

  DATA : lv_plpo TYPE STANDARD TABLE OF plpo WITH HEADER LINE.

  DATA : BEGIN OF lv_tapl OCCURS 0,
          tplnr LIKE tapl-tplnr,
          plnty LIKE tapl-plnty,
          plnnr LIKE tapl-plnnr,
          plnal LIKE tapl-plnal,
          zkriz LIKE tapl-zkriz,
          zaehl LIKE tapl-zaehl,
         END OF lv_tapl.

  DATA : BEGIN OF lv_header.
          INCLUDE STRUCTURE thead.
  DATA : END OF lv_header.

  DATA : lt_long_text  TYPE TABLE OF tline WITH HEADER LINE,
         lv_text_length     TYPE i,
         lv_loop_times      TYPE i,
         lv_char_pos_last   TYPE i,
         lv_char_pos_curr   TYPE i,
         lv_no_lines        TYPE i.

  DATA :  lv_PLNNR      TYPE tapl-plnnr,
          lv_PLNAL      TYPE tapl-plnal,
          lv_VORNR      TYPE plpo-vornr.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input         = yt_tasklist_process-PLNNR
    IMPORTING
      OUTPUT        = lv_PLNNR
            .
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input         = yt_tasklist_process-PLNAL
    IMPORTING
      OUTPUT        = lv_PLNAL
            .
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input         = yt_tasklist_process-VORNR
    IMPORTING
      OUTPUT        = lv_VORNR
            .
*----------------------------------------------------------*

* Get the corresponding data from database.
  SELECT tplnr plnty plnnr plnal FROM tapl
       INTO CORRESPONDING FIELDS OF TABLE lv_tapl
       WHERE tplnr = yt_tasklist_process-tplnr
         and PLNNR = lv_PLNNR
         and PLNAL = lv_PLNAL
       ORDER BY tplnr plnty plnnr plnal DESCENDING.

  READ TABLE lv_tapl INDEX 1.
   if sy-subrc ne 0.
      lv_err_text =  text-050.
      yt_tasklist_process-msg_text = lv_err_text.
      yt_tasklist_process-status   = lc_error.
      EXIT.
   endif.

* Select the appropriate records from Table PLPO.
  SELECT *
    APPENDING CORRESPONDING FIELDS OF TABLE lv_plpo
    FROM plpo AS a
    JOIN plas AS b
     ON ( a~plnty = b~plnty
         AND a~plnnr = b~plnnr
         AND a~plnkn = b~plnkn
         AND a~zaehl = b~zaehl )
     WHERE b~plnty = lv_tapl-plnty
       AND b~plnnr = lv_tapl-plnnr
       AND b~plnal = lv_tapl-plnal
       AND a~VORNR = lv_VORNR.

   if sy-subrc ne 0.
      lv_err_text =  text-050.
      yt_tasklist_process-msg_text = lv_err_text.
      yt_tasklist_process-status   = lc_error.
      EXIT.
   endif.
    READ TABLE lv_plpo INDEX 1.

*   Prepare the key for saving the text. Used in table STXH.
    CONCATENATE lv_plpo-mandt
                lv_plpo-plnty
                lv_plpo-plnnr
                lv_plpo-plnkn
                lv_plpo-zaehl INTO lv_name.

    CALL FUNCTION 'TCA01_READ'
         EXPORTING
              plnty  = lv_plpo-plnty
         IMPORTING
              struct = tca01.

*   Determine text-id and object-id
    CALL FUNCTION 'TCA09_READ'
         EXPORTING
              plnaw  = tca01-plnaw
         IMPORTING
              struct = tca09.

*   Initialse the Header For Text
    CALL FUNCTION 'INIT_TEXT'
         EXPORTING
              id       = tca09-idopr
              language = sy-langu
              name     = lv_name
              object   = tca09-objopr
         IMPORTING
              header   = lv_header
         TABLES
              lines    = lt_long_text
         EXCEPTIONS
              id       = 1
              language = 2
              name     = 3
              object   = 4
              OTHERS   = 5.

    IF sy-subrc <> 0.
      lv_err_text = text-040.
      yt_tasklist_process-msg_text = lv_err_text.
      yt_tasklist_process-status = lc_error.
      EXIT.

    ENDIF.

    LOOP AT yt_tasklist_process-long_text INTO lv_longtext.
*     Split long text into table.
      lv_text_length = strlen( lv_longtext ).
      lv_char_pos_curr = 0.
      lv_char_pos_last = 0.

      lv_loop_times = lv_text_length DIV 80.
      DO lv_loop_times TIMES.
        lv_char_pos_curr = lv_char_pos_curr + 80.

        lt_long_text-tdline =
          lv_longtext+lv_char_pos_last(80).
        lt_long_text-tdformat = '* '.
        APPEND lt_long_text.
        lv_char_pos_last = lv_char_pos_curr - 1.
      ENDDO.

*     Write the left-over.
      lt_long_text-tdline =
        lv_longtext+lv_char_pos_last.
      lt_long_text-tdformat = '* '.
      APPEND lt_long_text.

    ENDLOOP.

*   To get the no. of lines of Long Text.
    DESCRIBE TABLE lt_long_text LINES lv_no_lines.
    lv_header-tdtxtlines = lv_no_lines.

*   Saving the Long Text.
    CALL FUNCTION 'SAVE_TEXT'
         EXPORTING
              header          = lv_header
              savemode_direct = 'X'
         TABLES
              lines           = lt_long_text
         EXCEPTIONS
              id              = 1
              language        = 2
              name            = 3
              object          = 4
              OTHERS          = 5.

    IF sy-subrc <> 0.
      lv_err_text = text-040.
      yt_tasklist_process-msg_text = lv_err_text.
      yt_tasklist_process-status = lc_error.
      EXIT.

    ELSE.

*     Set text flag in operation data
      lv_plpo-txtsp = sy-langu.

*     Field LTXA1 hold first line of longtext
      READ TABLE lt_long_text INDEX 1.
      MOVE lt_long_text-tdline TO lv_plpo-ltxa1.

      MODIFY lv_plpo TRANSPORTING ltxa1 txtsp
              WHERE plnty = lv_plpo-plnty
                AND plnnr = lv_plpo-plnnr
                AND plnkn = lv_plpo-plnkn
                AND zaehl = lv_plpo-zaehl.

      IF sy-subrc <> 0.
        lv_err_text = text-040.
        yt_tasklist_process-msg_text = lv_err_text.
        yt_tasklist_process-status   = lc_error.
        EXIT.

      ELSE.
        lv_suc_text = text-030.

        yt_tasklist_process-msg_text = lv_suc_text.
        yt_tasklist_process-status   = lc_success.
        COMMIT WORK.

      ENDIF.

    ENDIF.

* Modifing the Database Table PLPO to reflect the changes
* Done to Long Text.
  MODIFY plpo FROM TABLE lv_plpo.

ENDFORM.                    " save_long_text
*&---------------------------------------------------------------------*
*&      Form  authorization_check
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM authorization_check.
 
ENDFORM.                    " authorization_check
*&---------------------------------------------------------------------*
*&      Form  get_file_name
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0565   text
*      <--P_GV_FILE_NAME  text
*----------------------------------------------------------------------*
FORM get_file_name USING    x_file_field_name
                   CHANGING y_file_name_with_path.

  DATA : lv_file_path TYPE rlgrap-filename,
         lv_file_name TYPE rlgrap-filename.

  PERFORM read_screen_values USING    x_file_field_name
                             CHANGING y_file_name_with_path.

  PERFORM split_path USING    y_file_name_with_path
                     CHANGING lv_file_path
                              lv_file_name.

  CALL FUNCTION 'WS_FILENAME_GET'
       EXPORTING
            def_filename = lv_file_name
            def_path     = lv_file_path
            mask         = ',*.*,*.*.'
            mode         = 'O'
            title        = 'Directory'
       IMPORTING
            filename     = p_infile
       EXCEPTIONS
            OTHERS       = 1.

ENDFORM.                    " get_file_name
*&---------------------------------------------------------------------*
*&      Form  read_screen_values
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_X_FILE_FIELD_NAME  text
*      <--P_Y_FILE_NAME_WITH_PATH  text
*----------------------------------------------------------------------*
FORM read_screen_values USING    x_field
                        CHANGING y_value.

  DATA: lv_dynpname LIKE d020s-prog,
        lv_dynpnumb LIKE d020s-dnum.

  DATA: BEGIN OF lt_dynpvaluetab OCCURS 1.
          INCLUDE STRUCTURE dynpread.
  DATA: END   OF lt_dynpvaluetab.

  lv_dynpname = sy-repid.
  lv_dynpnumb = sy-dynnr.

  REFRESH lt_dynpvaluetab.
  lt_dynpvaluetab-fieldname = x_field.
  APPEND lt_dynpvaluetab.

  CALL FUNCTION 'DYNP_VALUES_READ'
       EXPORTING
            dyname               = lv_dynpname
            dynumb               = lv_dynpnumb
       TABLES
            dynpfields           = lt_dynpvaluetab
       EXCEPTIONS
            invalid_abapworkarea = 1
            invalid_dynprofield  = 2
            invalid_dynproname   = 3
            invalid_dynpronummer = 4
            invalid_request      = 5
            no_fielddescription  = 6
            invalid_parameter    = 7
            undefind_error       = 8
            double_conversion    = 9
            OTHERS               = 10.
  IF sy-subrc = 0.

    READ TABLE lt_dynpvaluetab INDEX 1.
    MOVE: lt_dynpvaluetab-fieldvalue TO y_value.

  ENDIF.

ENDFORM.                    " read_screen_values
*&---------------------------------------------------------------------*
*&      Form  split_path
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_Y_FILE_NAME_WITH_PATH  text
*      <--P_LV_FILE_PATH  text
*      <--P_LV_FILE_NAME  text
*----------------------------------------------------------------------*
FORM split_path USING    x_file_name_with_path
                CHANGING y_file_path
                         y_file_name.

  DATA: lv_length TYPE i,
        lv_cpos    TYPE i.

  CHECK NOT x_file_name_with_path IS INITIAL.
  lv_length = strlen( x_file_name_with_path ).
  lv_cpos = lv_length.
  DO lv_length TIMES.
    lv_cpos = lv_cpos - 1.
    CHECK x_file_name_with_path+lv_cpos(1) EQ '\'.

    EXIT.

  ENDDO.
  y_file_path = x_file_name_with_path(lv_cpos).
  lv_cpos = lv_cpos + 1.
  y_file_name = x_file_name_with_path+lv_cpos(lv_length).

ENDFORM.                    " split_path
*&---------------------------------------------------------------------*
*&      Form  process_file
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM process_file.

  LOOP AT gt_tasklist_data_final INTO wa_tasklist_data.

*     Save Long Text only when saving the input file.
      PERFORM save_long_text USING wa_tasklist_data.

        MOVE-CORRESPONDING wa_tasklist_data TO gt_tasklist_report.

*       To download only Error Messages.
        IF NOT wa_tasklist_data-status = 'SUCCESS'.
         MOVE-CORRESPONDING wa_tasklist_data TO gt_tasklist_totalerr.
*         To save Long text.
          IF wa_tasklist_data-record_id EQ lc_record_operation.
            LOOP AT wa_tasklist_data-long_text INTO wa_longtext.
              CONCATENATE gt_tasklist_totalerr-longtext
                          wa_longtext-rec lc_newline
                     INTO gt_tasklist_totalerr-longtext.
              AT LAST.
                CONCATENATE '"' gt_tasklist_totalerr-longtext
                           INTO gt_tasklist_totalerr-longtext.
              ENDAT.
            ENDLOOP.
          ENDIF.
          APPEND gt_tasklist_totalerr.
        ENDIF.

        APPEND gt_tasklist_report.
        CLEAR gt_tasklist_totalerr.
        CLEAR gt_tasklist_report.


      CLEAR : wa_longtext.

  ENDLOOP.

ENDFORM.                    " process_file
*&---------------------------------------------------------------------*
*&      Form  download_error_file
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM download_error_file.
 data : gt_tasklist_err_final like gt_tasklist_totalerr
       occurs 0 with header line.

  IF NOT gt_tasklist_totalerr[] IS INITIAL.
* To add 'error' to the file name.
    SPLIT p_infile AT '.' INTO gv_file_name gv_extension.
   CONCATENATE gv_file_name '_error' '.' gv_extension INTO gv_file_name.

   LOOP AT gt_tasklist_totalerr.

     loop at gt_tasklist_data
             where record_id = gt_tasklist_totalerr-record_id
               and tplnr     = gt_tasklist_totalerr-tplnr
               and PLNNR     = gt_tasklist_totalerr-plnnr
               and PLNAL     = gt_tasklist_totalerr-plnal
               and VORNR     = gt_tasklist_totalerr-vornr.

      move-corresponding gt_tasklist_data
           to gt_tasklist_err_final.
      loop at gt_tasklist_data-long_text into wa_longtext.
        gt_tasklist_err_final-longtext = wa_longtext.
        append gt_tasklist_err_final.
      endloop.
     endloop.
   ENDLOOP.


* Download the error file in the same directory as that of upload file.
    CALL FUNCTION 'DOWNLOAD'
         EXPORTING
              filename                = gv_file_name
              filetype                = 'DAT'
              item                    = 'Error Records'
         TABLES
              data_tab                = gt_tasklist_err_final
              fieldnames              = gt_header_fields
         EXCEPTIONS
              invalid_filesize        = 1
              invalid_table_width     = 2
              invalid_type            = 3
              no_batch                = 4
              unknown_error           = 5
              gui_refuse_filetransfer = 6
              OTHERS                  = 7.

  ENDIF.

ENDFORM.                    " download_error_file
*&---------------------------------------------------------------------*
*&      Form  compress_text_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form compress_text_data TABLES xt_tasklist_data
                               STRUCTURE gt_tasklist_data
                               yt_tasklist_data_final
                               STRUCTURE gt_tasklist_data.

data : lv_long_text_tab TYPE TABLE OF lv_text.

  LOOP AT xt_tasklist_data.

  ON CHANGE OF xt_tasklist_data-TPLNR OR
               xt_tasklist_data-PLNNR OR
               xt_tasklist_data-PLNAL OR
               xt_tasklist_data-VORNR.

    CLEAR : lv_long_text_tab, lv_long_text_tab[].

    LOOP AT xt_tasklist_data
            where TPLNR = xt_tasklist_data-TPLNR and
                  PLNNR = xt_tasklist_data-PLNNR and
                  PLNAL = xt_tasklist_data-PLNAL and
                  VORNR = xt_tasklist_data-VORNR.

     APPEND LINES OF xt_tasklist_data-long_text[] to lv_long_text_tab.

    ENDLOOP.

     xt_tasklist_data-long_text[] = lv_long_text_tab.
     APPEND xt_tasklist_data to yt_tasklist_data_final.

  ENDON.

  ENDLOOP.
endform.                    " compress_text_data

*&---------------------------------------------------------------------*
*&      Form  check_file_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM check_file_data.

  LOOP AT gt_tasklist_data INTO wa_tasklist_data.

*     Check data before processing the file.
      PERFORM check_data USING wa_tasklist_data.

*       To download only Error Messages.
        IF wa_tasklist_data-status = lc_error.
          MOVE-CORRESPONDING wa_tasklist_data TO gt_tasklist_report.
          gv_check_failed = 'Y'.
        ENDIF.

        APPEND gt_tasklist_report.
        CLEAR gt_tasklist_report.

  ENDLOOP.

ENDFORM.                    " check_data

*&---------------------------------------------------------------------*
*&      Form  check_file_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM check_data USING xt_tasklist_data like line of gt_tasklist_data.

*   Check file data
    IF xt_tasklist_data-record_id IS INITIAL.
      xt_tasklist_data-msg_text =  'Record Id is missing.'.
      xt_tasklist_data-status   = lc_error.
      EXIT.
    ENDIF.
    IF xt_tasklist_data-tplnr IS INITIAL.
      xt_tasklist_data-msg_text = 'Functional Location is missing'.
      xt_tasklist_data-status   = lc_error.
      EXIT.
    ENDIF.
    IF xt_tasklist_data-plnnr IS INITIAL.
      xt_tasklist_data-msg_text = 'Group is missing'.
      xt_tasklist_data-status   = lc_error.
      EXIT.
    ENDIF.
    IF xt_tasklist_data-plnal IS INITIAL.
      xt_tasklist_data-msg_text = 'Group counter is missing'.
      xt_tasklist_data-status   = lc_error.
      EXIT.
    ENDIF.
    IF xt_tasklist_data-VORNR IS INITIAL.
      xt_tasklist_data-msg_text = 'Operation is missing'.
      xt_tasklist_data-status   = lc_error.
      EXIT.
    ENDIF.
    IF xt_tasklist_data-long_text[] IS INITIAL.
      xt_tasklist_data-msg_text = 'Long Text is missing'.
      xt_tasklist_data-status   = lc_error.
      EXIT.
    ENDIF.

ENDFORM.                    " check_file_data
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.