메뉴 건너뛰기

SAP 한국 커뮤니티

REPORT z18_028

sapjoy 2007.04.10 08:06 조회 수 : 4281

REPORT  z18_028      MESSAGE-ID  0K.
              .


DATA : con1_ref TYPE REF TO cl_gui_custom_container.


DATA : grid1  TYPE REF TO cl_gui_alv_grid,
       gs_variant     LIKE disvariant,
       gs_cs_variant  LIKE disvariant,
       gs_layout     TYPE lvc_s_layo,
       gs_toolbar    TYPE ui_functions,
       gt_sort       TYPE lvc_t_sort,
       gt_fieldcat   TYPE lvc_t_fcat.


DATA: BEGIN OF gt_sflight OCCURS 0.     "with header line
        INCLUDE STRUCTURE sflight.
DATA: END OF gt_sflight.



DATA: g_carrid LIKE sflight-carrid,
      g_connid LIKE sflight-connid.


TYPE-POOLS: icon.


*---------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.


*.............


  PUBLIC SECTION.


    TYPES: BEGIN OF sflight_key.
    TYPES:   carrid TYPE s_carr_id.
    TYPES:   connid TYPE s_conn_id.
    TYPES:   fldate TYPE s_date.
    TYPES: END OF sflight_key.


    TYPES: sflight_keys TYPE STANDARD TABLE OF sflight_key,
           sflight_table TYPE STANDARD TABLE OF sflight.


    METHODS:
      handle_data_changed
         FOR EVENT data_changed OF cl_gui_alv_grid
             IMPORTING er_data_changed.


    METHODS:
      get_inserted_rows
           EXPORTING
              inserted_rows TYPE sflight_keys.


    METHODS:
      get_deleted_rows
          EXPORTING
              deleted_rows TYPE sflight_table.


    METHODS:
      get_modified_rows
          EXPORTING
              modified_rows TYPE sflight_table.


    METHODS:
       refresh_delta_tables.


    METHODS: set_table_is_initial.


    METHODS: set_table_is_not_initial.


    METHODS: table_is_initial
                RETURNING value(initial) TYPE char01.


*..............


  PRIVATE SECTION.


* §4.Define internal tables to remember inserted and deleted lines,
*    thus the delta between input made after the last saving.


    DATA: inserted_rows TYPE sflight_keys,
          deleted_rows TYPE STANDARD TABLE OF sflight,
          modified_rows TYPE STANDARD TABLE OF sflight.



* This flag is set if any error occured in one of the
* following methods:
    DATA: error_in_data TYPE c.


* This flag signals that no records were read for the flight
* table initially:
    DATA: initial_table TYPE c.
** Methods to modularize event handler method HANDLE_DATA_CHANGED:
*
    METHODS:
      check_double_entries
         IMPORTING
            pr_data_changed TYPE REF TO cl_alv_changed_data_protocol.


    METHODS:
      update_delta_tables
         IMPORTING
            pr_data_changed TYPE REF TO cl_alv_changed_data_protocol.


    METHODS:
      perform_semantic_checks
         IMPORTING
            pr_data_changed TYPE REF TO cl_alv_changed_data_protocol.


    METHODS:
      get_cell_values
           IMPORTING
             row_id          TYPE int4
             pr_data_changed TYPE REF TO cl_alv_changed_data_protocol
           EXPORTING
             key             TYPE sflight_key.


 


ENDCLASS.                    "lcl_event_receiver DEFINITION
**---------------------------------------------------------
CLASS lcl_event_receiver IMPLEMENTATION.
  METHOD handle_data_changed.
*
    DATA: ls_good TYPE lvc_s_modi,
          l_price TYPE s_price,
          ls_new TYPE lvc_s_moce.


    error_in_data = space.


* check if there exist double entries
    CALL METHOD check_double_entries( er_data_changed ).


* remember new or deleted lines for saving
    CALL METHOD update_delta_tables( er_data_changed ).


* check mt_good_cells semantically
    CALL METHOD perform_semantic_checks( er_data_changed ).


    IF error_in_data = 'X'.
      CALL METHOD er_data_changed->display_protocol.
    ENDIF.


  ENDMETHOD.                    "handle_data_changed


*-----------------------------------------------------------------------
  METHOD check_double_entries.
    DATA: lt_good_cells TYPE lvc_t_modi,
          ls_good TYPE lvc_s_modi,
          ls_key TYPE sflight_key,
          ls_sflight TYPE sflight,
          l_flightdate LIKE gt_sflight-fldate,
          l_del_row TYPE lvc_s_moce,
          ls_outtab LIKE LINE OF gt_sflight,
          l_reentered TYPE c.


*
    LOOP AT pr_data_changed->mt_good_cells INTO ls_good.
      CASE ls_good-fieldname.
        WHEN 'FLDATE'.


          CALL METHOD pr_data_changed->get_cell_value
            EXPORTING
              i_row_id    = ls_good-row_id
              i_fieldname = ls_good-fieldname
            IMPORTING
              e_value     = l_flightdate.


          READ TABLE lt_good_cells WITH KEY
                            value = l_flightdate
                            TRANSPORTING NO FIELDS.
          IF sy-subrc = 0.


            CALL METHOD pr_data_changed->add_protocol_entry
              EXPORTING
                i_msgid     = '0K'
                i_msgno     = '000'
                i_msgty     = 'E'
                i_msgv1     = text-m01
                i_fieldname = ls_good-fieldname
                i_row_id    = ls_good-row_id.


            error_in_data = 'X'.
          ELSE.
            ls_good-value = l_flightdate.
            APPEND ls_good TO lt_good_cells.
          ENDIF.
      ENDCASE.


    ENDLOOP.


    LOOP AT lt_good_cells INTO ls_good.
      l_flightdate = ls_good-value.      "flightdate, see above
      READ TABLE gt_sflight WITH KEY
                    carrid = g_carrid
                    connid = g_connid
                    fldate = l_flightdate
                    TRANSPORTING NO FIELDS.


      IF sy-subrc = 0.
        l_reentered = space.
        LOOP AT pr_data_changed->mt_deleted_rows INTO l_del_row.
          READ TABLE gt_sflight INTO ls_outtab INDEX l_del_row-row_id.
          IF sy-subrc NE 0.
            MESSAGE i000(0k) WITH text-e01."Fehler beim Loschen
          ELSEIF
                    ls_outtab-carrid EQ g_carrid
                AND ls_outtab-connid EQ g_connid
                AND ls_outtab-fldate EQ ls_good-value.
            l_reentered = 'X'.
          ENDIF.
        ENDLOOP.


        IF l_reentered NE 'X'.
          CALL METHOD pr_data_changed->add_protocol_entry
            EXPORTING
              i_msgid     = '0K'
              i_msgno     = '000'
              i_msgty     = 'E'
              i_msgv1     = text-m01
              i_fieldname = ls_good-fieldname
              i_row_id    = ls_good-row_id.


          error_in_data = 'X'.
        ENDIF.
      ENDIF.
    ENDLOOP.



    IF me->table_is_initial( ) EQ 'X'.
      CALL METHOD get_cell_values
        EXPORTING
          row_id          = 1
          pr_data_changed = pr_data_changed
        IMPORTING
          key             = ls_key.


      SELECT SINGLE * FROM sflight INTO ls_sflight
                WHERE carrid = ls_key-carrid
                  AND connid = ls_key-connid
                  AND fldate = ls_key-fldate.


      IF sy-subrc = 0.
        CALL METHOD pr_data_changed->add_protocol_entry
          EXPORTING
            i_msgid     = '0K'
            i_msgno     = '000'
            i_msgty     = 'E'
            i_msgv1     = text-m01
            i_fieldname = 'FLDATE'
            i_row_id    = 1.


        error_in_data = 'X'.
      ENDIF.


    ENDIF.


  ENDMETHOD.                    "check_double_entries
*-------------------------------------------------------
  METHOD update_delta_tables.
    DATA: l_ins_row TYPE lvc_s_moce,
          l_del_row TYPE lvc_s_moce,
          l_mod_row TYPE lvc_s_modi,
          ls_mod_row TYPE REF TO lvc_s_modi,
          ls_key TYPE sflight_key,
          ls_sflight type sflight,
          ls_outtab like line of gt_sflight.
          .


*..........
* deleted rows
*.............
    LOOP AT pr_data_changed->mt_deleted_rows INTO l_del_row.
      READ TABLE gt_sflight INTO ls_outtab INDEX l_del_row-row_id.
      IF sy-subrc NE 0.
        MESSAGE i000(0k) WITH text-e01."Fehler beim Loschen
      ELSE.
        MOVE-CORRESPONDING ls_outtab TO ls_sflight.


        APPEND ls_sflight TO deleted_rows.


        DELETE me->inserted_rows
             WHERE carrid = ls_outtab-carrid
             AND   connid = ls_outtab-connid
             AND   fldate = ls_outtab-fldate.
      ENDIF.
    ENDLOOP.


FIELD-SYMBOLS: <fs> TYPE table.


ASSIGN pr_data_changed->mp_mod_rows->* TO <fs>.
loop at <fs> into ls_outtab.
     MOVE-CORRESPONDING ls_outtab TO ls_sflight.
     APPEND ls_sflight TO modified_rows.
endloop.



* inserted rows
      IF me->table_is_initial( ) EQ 'X'.
        CALL METHOD get_cell_values
          EXPORTING
            row_id          = 1
            pr_data_changed = pr_data_changed
          IMPORTING
            key             = ls_key.


        APPEND ls_key TO inserted_rows.
        CALL METHOD me->set_table_is_not_initial.
      ENDIF.


      LOOP AT pr_data_changed->mt_inserted_rows INTO l_ins_row.
        CALL METHOD get_cell_values
          EXPORTING
            row_id          = l_ins_row-row_id
            pr_data_changed = pr_data_changed
          IMPORTING
            key             = ls_key.


        APPEND ls_key TO inserted_rows.
      ENDLOOP.


    ENDMETHOD.                    "update_delta_tables
*---------------------------------------------------------
    METHOD get_cell_values.
* get values of key cells of row ROW_ID


* CARRIER
      CALL METHOD pr_data_changed->get_cell_value
        EXPORTING
          i_row_id    = row_id
          i_fieldname = 'CARRID'
        IMPORTING
          e_value     = key-carrid.


      IF sy-subrc NE 0.
        MESSAGE i000(0k) WITH text-e02.
      ENDIF.
* CONNID
      CALL METHOD pr_data_changed->get_cell_value
        EXPORTING
          i_row_id    = row_id
          i_fieldname = 'CONNID'
        IMPORTING
          e_value     = key-connid.


      IF sy-subrc NE 0.
        MESSAGE i000(0k) WITH text-e02.
      ENDIF.


* FLDATE
      CALL METHOD pr_data_changed->get_cell_value
        EXPORTING
          i_row_id    = row_id
          i_fieldname = 'FLDATE'
        IMPORTING
          e_value     = key-fldate.


      IF sy-subrc NE 0.
        MESSAGE i000(0k) WITH text-e02.
      ENDIF.


 


    ENDMETHOD.                    "get_cell_values


*---------------------------------------------------------
    METHOD perform_semantic_checks.
      DATA: ls_good TYPE lvc_s_modi,
            l_planetype TYPE s_planetye,
            l_seatsmax TYPE s_seatsmax.


      LOOP AT pr_data_changed->mt_good_cells INTO ls_good.
        CASE ls_good-fieldname.
          WHEN 'PLANETYPE'.
            CALL METHOD pr_data_changed->get_cell_value
              EXPORTING
                i_row_id    = ls_good-row_id
                i_fieldname = ls_good-fieldname
              IMPORTING
                e_value     = l_planetype.


            SELECT SINGLE seatsmax FROM saplane INTO l_seatsmax
                          WHERE planetype = l_planetype.


            IF sy-subrc NE 0.
              CALL METHOD pr_data_changed->add_protocol_entry
                EXPORTING
                  i_msgid     = '0K'
                  i_msgno     = '000'
                  i_msgty     = 'E'
                  i_msgv1     = text-m02
                  i_fieldname = ls_good-fieldname
                  i_row_id    = ls_good-row_id.


              error_in_data = 'X'.
            ELSE.



              CALL METHOD pr_data_changed->modify_cell
                EXPORTING
                  i_row_id    = ls_good-row_id
                  i_fieldname = 'SEATSMAX'
                  i_value     = l_seatsmax.
            ENDIF.
        ENDCASE.
      ENDLOOP.
    ENDMETHOD.                    "perform_semantic_checks


*------------------------------------------------------


    METHOD get_inserted_rows.
      inserted_rows = me->inserted_rows.
    ENDMETHOD.                    "get_inserted_rows
*------------------------------------------------------


    METHOD get_deleted_rows.
      deleted_rows = me->deleted_rows.
    ENDMETHOD.                    "get_deleted_rows
*------------------------------------------------------
    METHOD get_modified_rows.
      modified_rows = me->modified_rows.
    ENDMETHOD.                    "get_modified_rows


*------------------------------------------------------
    METHOD refresh_delta_tables.
      CLEAR me->inserted_rows[].
      CLEAR me->deleted_rows[].
    ENDMETHOD.                    "refresh_delta_tables
*------------------------------------------------------
    METHOD set_table_is_initial.
      initial_table = 'X'.
    ENDMETHOD.                    "set_table_is_initial
*------------------------------------------------------
    METHOD set_table_is_not_initial.
      initial_table = space.
    ENDMETHOD.                    "set_table_is_not_initial
*------------------------------------------------------
    METHOD table_is_initial.
      IF initial_table = 'X'.
        initial = 'X'.
      ELSE.
        initial = space.
      ENDIF.
    ENDMETHOD.                    "table_is_initial



  ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION



  DATA : event_receiver TYPE REF TO lcl_event_receiver.
  DATA: g_verifier TYPE REF TO lcl_event_receiver.


START-OF-SELECTION.
  SELECT * FROM sflight
  INTO CORRESPONDING FIELDS OF TABLE gt_sflight.


  CALL SCREEN 100.


 


 


*---------------------------------------------------------------------*
*  MODULE init_con OUTPUT
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
MODULE init_con OUTPUT.


  IF con1_ref IS INITIAL.
    CREATE OBJECT con1_ref
    EXPORTING container_name = 'CON1'.



    CREATE OBJECT grid1
           EXPORTING
                i_parent = con1_ref.


    CREATE OBJECT g_verifier.
    SET HANDLER g_verifier->handle_data_changed FOR grid1.



    PERFORM setting_layout.
    PERFORM setting_catalog.
    PERFORM setting_toolbar.
    PERFORM setting_sort.
    PERFORM setting_event.


    CALL METHOD grid1->set_table_for_first_display
      EXPORTING
        i_structure_name     = 'SFLIGHT'
        i_save               = 'A'
        is_variant           = gs_variant  " variant display
        i_default            = ' '
        is_layout            = gs_layout
        it_toolbar_excluding = gs_toolbar
      CHANGING
        it_outtab            = gt_sflight[]
        it_fieldcatalog      = gt_fieldcat
        it_sort              = gt_sort.


*CALL METHOD GRID1->SET_READY_FOR_INPUT
*   EXPORTING I_READY_FOR_INPUT = 0.


 


  ENDIF.
ENDMODULE.                 " init_con  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'G100'.
*  SET TITLEBAR 'xxx'.


ENDMODULE.                 " STATUS_0100  OUTPUT


*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.


  CASE sy-ucomm.
    WHEN 'BACK' OR 'EXIT' OR 'CANC'.
      LEAVE TO SCREEN 0.
    WHEN 'SAVE'.
      DATA: l_valid TYPE c.
      CALL METHOD grid1->check_changed_data
        IMPORTING
          e_valid = l_valid.


      IF l_valid IS NOT INITIAL.


        PERFORM update_database.
        MESSAGE s000(0k) WITH text-s01.
      ENDIF.


  ENDCASE.


ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Form  setting_layout
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GS_LAYOCAT  text
*----------------------------------------------------------------------*
FORM setting_layout .
*- General display options
  gs_layout-cwidth_opt = 'X'.
* TITLE BAR
  gs_layout-grid_title = 'ALV GRID EDIT TEST'.
*  gs_layout-edit = 'X'.
* Selection modes for SEL_MODE
  gs_layout-sel_mode = 'D'.
* Grid pattern
*  gs_layout-zebra      = 'X'.
ENDFORM.                    " setting_layout
*&---------------------------------------------------------------------*
*&      Form  setting_toolbar
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GS_TOOLBAR  text
*----------------------------------------------------------------------*
FORM setting_toolbar.
  DATA: l_exclude TYPE ui_func.


  l_exclude = cl_gui_alv_grid=>mc_fc_save_variant.
  APPEND l_exclude TO gs_toolbar.


  l_exclude = cl_gui_alv_grid=>mc_fc_maintain_variant.
  APPEND l_exclude TO gs_toolbar.


ENDFORM.                    " setting_toolbar
*&---------------------------------------------------------------------*
*&      Form  setting_sort
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM setting_sort .


  DATA : ls_sort TYPE lvc_s_sort.


  ls_sort-spos = '1'.
  ls_sort-fieldname = 'CARRID'.
  ls_sort-up = 'X'.
  ls_sort-subtot = 'X'.
  APPEND ls_sort TO gt_sort.


ENDFORM.                    " setting_sort
*&---------------------------------------------------------------------*
*&      Form  setting_event
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM setting_event .
  CREATE OBJECT event_receiver.



ENDFORM.                    " setting_event
*&---------------------------------------------------------------------*
*&      Form  setting_catalog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM setting_catalog .
  DATA ls_fieldcat  TYPE lvc_s_fcat.


  ls_fieldcat-fieldname = 'CARRID'.
  ls_fieldcat-coltext = 'Carrid ID'.
  ls_fieldcat-just = 'L'.
  ls_fieldcat-key = 'X'.
  ls_fieldcat-edit = ''.
  ls_fieldcat-outputlen = '2'.


  APPEND ls_fieldcat TO gt_fieldcat.



  ls_fieldcat-fieldname = 'CONNID'.
  ls_fieldcat-coltext = 'Flight Number'.
  ls_fieldcat-just = 'C'.
  ls_fieldcat-key = 'X'.
  ls_fieldcat-edit = 'X'.
  ls_fieldcat-outputlen = '4'.


  APPEND ls_fieldcat TO gt_fieldcat.


  ls_fieldcat-fieldname = 'PRICE'.
  ls_fieldcat-coltext = 'Airfare'.
  ls_fieldcat-just = 'R'.
  ls_fieldcat-key = ' '.
  ls_fieldcat-edit = 'X'.
  ls_fieldcat-outputlen = '15'.


  APPEND ls_fieldcat TO gt_fieldcat.



ENDFORM.                    " setting_catalog
*&---------------------------------------------------------------------*
*&      Form  update_database
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM update_database .
  DATA: lt_del_rows TYPE TABLE OF sflight,
        lt_mod_rows TYPE TABLE OF sflight,
        ls_sflight TYPE sflight,
        lt_ins_keys TYPE g_verifier->sflight_keys,
        l_ins_key TYPE g_verifier->sflight_key,
        ls_outtab LIKE LINE OF gt_sflight,
        lt_instab TYPE TABLE OF sflight.


*
** 1.Delete Lines:
  CALL METHOD g_verifier->get_deleted_rows
    IMPORTING
      deleted_rows = lt_del_rows.
*
*  delete sflight from table lt_del_rows.
**
** 2.Insert Lines:
  CALL METHOD g_verifier->get_inserted_rows
    IMPORTING
      inserted_rows = lt_ins_keys.
*
  LOOP AT lt_ins_keys INTO l_ins_key.
    READ TABLE gt_sflight INTO ls_outtab
     WITH KEY carrid = l_ins_key-carrid
              connid = l_ins_key-connid
              fldate = l_ins_key-fldate.
    IF sy-subrc EQ 0.
      MOVE-CORRESPONDING ls_outtab TO ls_sflight.
      APPEND ls_sflight TO lt_instab.
    ENDIF.
  ENDLOOP.


 insert sflight from table  lt_instab.


** 3.modified Lines:
  CALL METHOD g_verifier->get_modified_rows
    IMPORTING
      modified_rows = lt_mod_rows.


  CALL METHOD g_verifier->refresh_delta_tables.


 modify sflight from table lt_mod_rows.
 commit work.


ENDFORM.                    " update_database

번호 제목 글쓴이 날짜 조회 수
» REPORT z18_028 sapjoy 2007.04.10 4281
230 REPORT Z03_021 [5] sapjoy 2007.05.15 2737
229 REPORT z18_002_d sapjoy 2008.04.16 2088
228 REPORT Z18_000 [1] sapjoy 2008.04.16 2602
227 REPORT Z17_019 [1] sapjoy 2007.02.15 2748
226 REPORT Z17_018. sapjoy 2008.04.15 1860
225 REPORT Z17_014. [1] sapjoy 2007.02.13 2504
224 Report Z15_047 [3] sapjoy 2007.12.06 2798
223 REPORT Z15_044 [1] sapjoy 2008.04.15 2474
222 REPORT Z15_039 sapjoy 2007.02.06 2315
221 REPORT Z15_037. sapjoy 2007.02.06 2472
220 REPORT Z15_033 sapjoy 2007.02.06 2987
219 REPORT z15_032 sapjoy 2007.02.06 2578
218 REPORT Z15_028 sapjoy 2007.02.01 2509
217 REPORT Z15_026 [1] sapjoy 2007.09.13 2205
216 REPORT Z15_026 sapjoy 2007.02.01 2758
215 REPORT Z15_025 sapjoy 2008.04.15 1975
214 REPORT z15_023 sapjoy 2007.01.31 2484
213 REPORT Z15_022. sapjoy 2007.01.30 2495
212 REPORT Z15_020 sapjoy 2007.01.30 2524