Friends
REPORT zrsr.
*TABLES: rsrvariant.
TYPE-POOLS : slis. "",truxs.
PARAMETERS:p_qid TYPE rszcompid OBLIGATORY, ""sysuuid_25 OBLIGATORY,
p_qvar TYPE rsrvari OBLIGATORY,
p_file TYPE localfile OBLIGATORY, ""rlgrap-filename,
p_delete AS CHECKBOX.
TYPES: BEGIN OF gty_file,
vari TYPE rsrvariant-vari,
vnam TYPE rsrvariant-vnam,
low TYPE rsrvariant-low,
high TYPE rsrvariant-high,
END OF gty_file,
gtt_file TYPE STANDARD TABLE OF gty_file.
TYPES: BEGIN OF gty_final,
vari TYPE rsrvariant-vari,
vnam TYPE rsrvariant-vnam,
low TYPE rsrvariant-low,
high TYPE rsrvariant-high,
status TYPE string,
END OF gty_final,
gtt_final TYPE STANDARD TABLE OF gty_final.
DATA: gt_file TYPE gtt_file, gs_file TYPE gty_file, gt_fieldcat TYPE slis_t_fieldcat_alv. ""gt_type TYPE truxs_t_text_data,.
DATA: gs_final TYPE gty_final, gt_final TYPE TABLE OF gty_final, gt_file1 TYPE gtt_file, gs_file1 TYPE gty_file.
DATA: gs_rsr TYPE rsrvariant, gt_rsr TYPE TABLE OF rsrvariant, gv_tot TYPE i, gv_del TYPE i, gv_chg TYPE i, gv_ins TYPE i,
gs_rsr1 TYPE rsrvariant, gv_compid TYPE sysuuid_25.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM f4_get_file CHANGING p_file.
START-OF-SELECTION.
**--Upload CSV
PERFORM upload_file ""USING gt_type
CHANGING gt_file.
**Get COMPUID from Query id (Assumption: there will be only one active record with below key)
SELECT SINGLE compuid FROM v_compdir_compic INTO gv_compid WHERE objvers = 'A' AND compid = p_qid.
if sy-subrc <> 0.
MESSAGE 'View entry not maintained in v_compdir_compic' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
endif.
IF gt_file IS NOT INITIAL.
gt_file1 = gt_file.
SORT: gt_file BY vari, gt_file1 BY vnam.
SELECT * FROM rsrvariant INTO TABLE gt_rsr FOR ALL ENTRIES IN gt_file WHERE compuid = gv_compid AND vari = gt_file-vari.
SELECT * FROM rsrvariant APPENDING TABLE gt_rsr WHERE compuid = gv_compid AND vari = p_qvar.
IF gt_rsr IS NOT INITIAL.
SORT gt_rsr BY compuid vari.
ELSE.
MESSAGE 'No data found' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
ELSE.
MESSAGE 'CSV not uploaded successfully' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
DESCRIBE TABLE gt_file LINES gv_tot.
**&&--IF DELETION FLAG ON
IF p_delete = 'X'.
LOOP AT gt_file INTO gs_file.
DELETE FROM rsrvariant WHERE compuid = gv_compid AND vari = gs_file-vari.
IF sy-subrc = 0.
COMMIT WORK AND WAIT.
gv_del = gv_del + 1.
gs_final-vari = gs_file-vari.
gs_final-vnam = gs_file-vnam.
gs_final-low = gs_file-low.
gs_final-high = gs_file-high.
gs_final-status = 'Deleted Successfully'.
APPEND gs_final TO gt_final.
ELSE.
ROLLBACK WORK.
gs_final-vari = gs_file-vari.
gs_final-vnam = gs_file-vnam.
gs_final-low = gs_file-low.
gs_final-high = gs_file-high.
gs_final-status = 'Could not been Deleted'.
APPEND gs_final TO gt_final.
ENDIF.
ENDLOOP.
ELSE.
LOOP AT gt_rsr INTO gs_rsr. """gt_file INTO gs_file.
READ TABLE gt_file INTO gs_file WITH KEY vari = gs_rsr-vari BINARY SEARCH.
"" gt_rsr INTO gs_rsr WITH KEY compuid = gv_compid vari = gs_file-vari BINARY SEARCH.
gs_final-vari = gs_file-vari.
gs_final-vnam = gs_file-vnam.
gs_final-low = gs_file-low.
gs_final-high = gs_file-high.
IF sy-subrc = 0. ""if record exist in RSR table then change it from CSV record
gs_rsr-vnam = gs_file-vnam.
gs_rsr-low = gs_file-low.
IF gs_file-high IS INITIAL.
CLEAR:gs_rsr-high.
gs_rsr-sign = 'I'.
gs_rsr-opt = 'EQ'.
ELSE.
gs_rsr-high = gs_file-high.
gs_rsr-sign = 'I'.
gs_rsr-opt = 'BT'.
ENDIF.
MODIFY rsrvariant FROM gs_rsr.
IF sy-subrc = 0.
COMMIT WORK AND WAIT.
gs_final-status = 'Record altered successfully'.
gv_chg = gv_chg + 1.
ELSE.
gs_final-status = 'Record could not been altered'.
ROLLBACK WORK.
ENDIF.
APPEND gs_final TO gt_final.
ELSE. ""if record does not exist then copy CSV record to RSR table
* READ TABLE gt_rsr INTO gs_rsr1 WITH KEY compuid = gv_compid vari = p_qvar BINARY SEARCH.
LOOP AT gt_file INTO gs_file1.
IF gs_rsr-vnam = gs_file1-vnam.
gs_rsr1 = gs_rsr.
gs_rsr1-vari = gs_file1-vari.
gs_rsr1-low = gs_file1-low.
gs_final-vari = gs_rsr1-vari.
gs_final-vnam = gs_rsr1-vnam.
gs_final-low = gs_rsr1-low.
gs_final-high = gs_rsr1-high.
IF gs_file1-high IS INITIAL.
CLEAR:gs_rsr1-high.
gs_rsr1-sign = 'I'.
gs_rsr1-opt = 'EQ'.
ELSE.
gs_rsr1-high = gs_file1-high.
gs_rsr-sign = 'I'.
gs_rsr-opt = 'BT'.
ENDIF.
ELSE.
gs_rsr1 = gs_rsr.
gs_rsr1-vari = gs_file1-vari.
gs_final-vari = gs_rsr1-vari.
gs_final-vnam = gs_rsr1-vnam.
gs_final-low = gs_rsr1-low.
gs_final-high = gs_rsr1-high.
ENDIF.
INSERT rsrvariant FROM gs_rsr1.
IF sy-subrc = 0.
COMMIT WORK AND WAIT.
gs_final-status = 'Record inserted successfully'.
gv_ins = gv_ins + 1.
ELSE.
gs_final-status = 'Record could not been inserted'.
ROLLBACK WORK.
ENDIF.
APPEND gs_final TO gt_final.
ENDLOOP.
ENDIF.
CLEAR: gs_rsr, gs_rsr1, gs_file, gs_file1.
ENDLOOP .
ENDIF.
END-OF-SELECTION.
IF gt_final IS NOT INITIAL.
* Build the fieldcatelog
PERFORM get_fieldcat CHANGING gt_fieldcat.
* Display the ALV
PERFORM display_alv USING gt_fieldcat
gt_final.
ENDIF.
*&---------------------------------------------------------------------*
*& Form upload_file
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GT_TYPE text
* <--P_GT_FILE text
*----------------------------------------------------------------------*
FORM upload_file ""USING p_gt_type TYPE truxs_t_text_data
CHANGING p_gt_file TYPE gtt_file.
DATA lv_fname TYPE string.
lv_fname = p_file.
TYPES: BEGIN OF t_data,
line(255) TYPE c,
END OF t_data.
DATA: i_datatab TYPE TABLE OF t_data.
DATA: x_datatab LIKE LINE OF i_datatab.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = lv_fname ""p_file
filetype = 'ASC'
TABLES
data_tab = i_datatab """p_gt_file
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
DELETE i_datatab INDEX 1.
LOOP AT i_datatab INTO x_datatab.
SPLIT x_datatab AT ',' INTO gs_file-vari gs_file-vnam gs_file-low gs_file-high.
APPEND gs_file TO p_gt_file.
CLEAR gs_file.
ENDLOOP.
ENDFORM. " UPLOAD_FILE
*&---------------------------------------------------------------------*
*& Form f4_get_file
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_P_FILE text
*----------------------------------------------------------------------*
FORM f4_get_file CHANGING p_p_file TYPE rlgrap-filename.
DATA: ifile TYPE filetable.
DATA: xfile LIKE LINE OF ifile.
DATA: rc TYPE i.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
initial_directory = 'C:'
* MULTISELECTION =
CHANGING
file_table = ifile
rc = rc
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
READ TABLE ifile INTO xfile INDEX 1.
IF sy-subrc = 0.
p_p_file = xfile-filename.
ENDIF.
ENDFORM. " F4_GET_FILE
*&---------------------------------------------------------------------*
*& Form get_fieldcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GT_FIELDCAT text
*----------------------------------------------------------------------*
FORM get_fieldcat CHANGING p_gt_fieldcat TYPE slis_t_fieldcat_alv.
PERFORM fill_fieldcat USING:
1 'VARI' 'Variant Id' CHANGING p_gt_fieldcat,
2 'VNAM' 'Variant Name' CHANGING p_gt_fieldcat,
3 'LOW' 'Low value' CHANGING p_gt_fieldcat,
4 'HIGH' 'High Value' CHANGING p_gt_fieldcat,
5 'STATUS' 'Message' CHANGING p_gt_fieldcat.
ENDFORM. "get_fieldcat
*&---------------------------------------------------------------------*
*& Form fill_fieldcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->VALUE(P_SNO) text
* -->VALUE(P_FIELD) text
* -->VALUE(P_TEXT) text
* <--P1_GT_FIELDCAT text
*----------------------------------------------------------------------*
FORM fill_fieldcat USING value(p_sno)
value(p_field)
value(p_text)
CHANGING p1_gt_fieldcat TYPE slis_t_fieldcat_alv.
DATA:lwa_fieldcat TYPE slis_fieldcat_alv.
CLEAR lwa_fieldcat.
lwa_fieldcat-col_pos = p_sno.
lwa_fieldcat-fieldname = p_field.
lwa_fieldcat-tabname = 'GT_FINAL'.
lwa_fieldcat-seltext_l = p_text.
APPEND lwa_fieldcat TO p1_gt_fieldcat.
ENDFORM. " FILL_FIELDCAT
*&---------------------------------------------------------------------*
*& Form display_alv
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GT_FIELDCAT text
* -->P_GT_FINAL text
*----------------------------------------------------------------------*
FORM display_alv USING p_gt_fieldcat TYPE slis_t_fieldcat_alv
p_gt_final TYPE gtt_final.
DATA: lv_repid TYPE sy-repid,
lwa_layout TYPE slis_layout_alv.
lv_repid = sy-repid.
lwa_layout-colwidth_optimize = 'X'.
* ALV Grid Display
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = lv_repid
i_callback_user_command = 'USER_COMMAND'
i_callback_html_top_of_page = 'HTML_TOP_OF_PAGE'
is_layout = lwa_layout
it_fieldcat = p_gt_fieldcat
i_save = 'A'
TABLES
t_outtab = p_gt_final
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. "DISPLAY_ALV
*&---------------------------------------------------------------------*
*& Form html_top_of_page
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->TOP text
*----------------------------------------------------------------------*
FORM html_top_of_page USING top TYPE REF TO cl_dd_document. "#EC CALLED
DATA: lv_text(255) TYPE c,
lv_cnt(4) TYPE c, lv_dat(10) TYPE c, lv_tim(8) TYPE c.
CALL METHOD top->add_text
EXPORTING
text = 'Change Variant ID'(001)
sap_style = 'heading'.
CALL METHOD top->add_gap
EXPORTING
width = 100.
CALL METHOD top->new_line( ).
MOVE gv_tot TO lv_cnt.
CONCATENATE 'Total Number of records (in CSV) :'(002) lv_cnt INTO lv_text.
CALL METHOD top->add_text
EXPORTING
text = lv_text.
CLEAR: lv_text.
CALL METHOD top->new_line( ).
MOVE gv_del TO lv_cnt.
CONCATENATE 'No of records deleted:'(003) lv_cnt INTO lv_text.
CALL METHOD top->add_text
EXPORTING
text = lv_text.
CALL METHOD top->add_gap
EXPORTING
width = 100.
CALL METHOD top->new_line( ).
MOVE gv_chg TO lv_cnt.
CONCATENATE 'No of records altered:'(004) lv_cnt INTO lv_text.
CALL METHOD top->add_text
EXPORTING
text = lv_text.
CALL METHOD top->add_gap
EXPORTING
width = 100.
CALL METHOD top->new_line( ).
MOVE gv_ins TO lv_cnt.
CONCATENATE 'No of records newly inserted:'(004) lv_cnt INTO lv_text.
CALL METHOD top->add_text
EXPORTING
text = lv_text.
ENDFORM. "html_top_of_page