Thursday, July 26, 2007

File Upload Program

REPORT ZHARZTEST NO STANDARD PAGE HEADING
LINE-SIZE 132
LINE-COUNT 65.
*----------------------------------------------------------------------*
* 1. Read input file *
* 2. Check the mandatory fields and if any one of them is missing *
* move the record to error file. *
* 3. If any errors format the message and send it for reporting *
* 4. If any errors, it downloads the error file *
* 5. Display the Audit report *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* FLAGS *
*----------------------------------------------------------------------*
DATA: FG_STOP TYPE C, " Flag used to stop processing
FG_COLOR TYPE C, " Flag to set Background color
FG_OPENSESSION TYPE C. " Flag for Open Sesssion

*----------------------------------------------------------------------*
* INTERNAL TABLE DECLARATIONS *
*----------------------------------------------------------------------*
*--- Internal table for Dimension Details
DATA: BEGIN OF IT_DATA OCCURS 0,
MANDT(3),
ZDIMID(10),
ZDIMDESC(120),
END OF IT_DATA.

*--- Internal table for input file for ascii data load
DATA: BEGIN OF I_INFILE_ASC OCCURS 0,
STRING(1000) TYPE C,
END OF I_INFILE_ASC.

*--- Internal table to store messages from Call Transaction.
DATA : I_BDCMSGCOLL TYPE STANDARD TABLE OF BDCMSGCOLL WITH HEADER LINE,

*--- Internal table for BDC data
IT_BDCTAB TYPE BDCDATA OCCURS 0 WITH HEADER LINE.

*--- Internal table to hold the error records.
DATA : BEGIN OF IT_ERROR OCCURS 0.
INCLUDE STRUCTURE IT_DATA.

DATA : MESSAGE(80) TYPE C,
END OF IT_ERROR.

*--- Internal table for session names
DATA : BEGIN OF IT_SESSION OCCURS 0,
NAME(12),
END OF IT_SESSION.

*--- Internal table to hold Collecting messages in the SAP System
DATA : IT_MSG LIKE BDCMSGCOLL OCCURS 10 WITH HEADER LINE.

*--- Internal table to hold error records to be downloaded
DATA : BEGIN OF I_FINAL OCCURS 0,
STRING(1000),
END OF I_FINAL.

*--- Internal table for session names
DATA : BEGIN OF I_SESSION OCCURS 0,
NAME(12),
END OF I_SESSION.

*--- Internal table to hold data from Excel
DATA : IT_INTERN TYPE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.

*----------------------------------------------------------------------*
* CONSTANTS *
*----------------------------------------------------------------------*
CONSTANTS : C_FILETYPE LIKE RLGRAP-FILETYPE VALUE 'DAT'. " Filetype
*----------------------------------------------------------------------*
* DATA DECLARATIONS *
*----------------------------------------------------------------------*
DATA : G_TABIX LIKE SY-TABIX, " Table Index
G_MESG(80) TYPE C, " Message,
G_ERROR TYPE I, " No.of Errors
G_SUCCESS TYPE I, " No.of successes
G_BDC_SESS TYPE APQI-GROUPID VALUE 'UPLOAD', " Queue info defn
G_MAX TYPE I VALUE '900', " Max records
G_LINES TYPE I, " No.of records
G_INSERT TYPE I, " Insert
G_COUNTER(2) TYPE N, " No.of Sessions
G_EMPTY TYPE I, " No. of empty records
G_TOTAL TYPE I, " Total no. of records with data
G_SESSION TYPE APQI-GROUPID, " Session name
G_DEL TYPE X VALUE '09', " To seperate fields in case of Dat
G_SFILE(255), " File path
G_FILEEXT(3). " File Extension

*----------------------------------------------------------------------*
* SELECTION SCREEN. *
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK SCR1 WITH FRAME TITLE TEXT-001.
PARAMETER : P_FILE TYPE RLGRAP-FILENAME OBLIGATORY, " Input File
P_FTYPE LIKE RLGRAP-FILETYPE DEFAULT 'XLS'. " File Type
PARAMETERS: P_HEADER TYPE I DEFAULT 1. " Header Lines

SELECTION-SCREEN END OF BLOCK SCR1.
PARAMETERS: P_BEGCOL TYPE I DEFAULT 1 NO-DISPLAY, " Start Column
P_BEGROW TYPE I DEFAULT 1 NO-DISPLAY, " Start Row
P_ENDCOL TYPE I DEFAULT 100 NO-DISPLAY, " End Column
P_ENDROW TYPE I DEFAULT 32000 NO-DISPLAY. " End Row

*---------------------------------------------------------------------*
* AT SELECTION-SCREEN *
*---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
*--- Validating file
PERFORM VALIDATE_FILE USING P_FILE.
*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
*--- Perform to store the data to an internal table
PERFORM READ_INPUT_FILE_DATA.
*--- Process data in non test mode only
IF NOT IT_DATA[] IS INITIAL.
*--- Perform to process data
PERFORM PROCESS_DATA.
*--- Perform to Update the Table with the Input data
PERFORM UPDATE_TABLE.
ENDIF.

*----------------------------------------------------------------------*
* END OF SELECTION. *
*----------------------------------------------------------------------*
END-OF-SELECTION.
*----------------------------------------------------------------------*
* Form : validate_input_file
*----------------------------------------------------------------------*
* Description : To provide F4 help for file if read from PC
*----------------------------------------------------------------------*
FORM VALIDATE_FILE USING F_FILE TYPE RLGRAP-FILENAME.
*--- Call function to get the File (user specified)
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
CHANGING
FILE_NAME = F_FILE
EXCEPTIONS
MASK_TOO_LONG = 1
OTHERS = 2.

*----------------------------------------------------------------------*
* Form : Read_inputfile
*----------------------------------------------------------------------*
* Description : To upload the data into internal table
*----------------------------------------------------------------------*
FORM READ_INPUTFILE TABLES F_INFILE
USING F_FILE TYPE RLGRAP-FILENAME
F_FILTYP TYPE RLGRAP-FILETYPE.
*--- Local variable declarations
DATA : LV_DEL TYPE CHAR01,
LV_FILE TYPE STRING.
*--- Clear and initialize variables
CLEAR F_INFILE.
REFRESH F_INFILE.
LV_FILE = F_FILE.
*--- Function module to upload the input file
IF F_FILTYP = 'DAT'.
LV_DEL = ''.
ENDIF.
*--- Call function GUI_UPLOAD to upload data from flat file to internal table
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = LV_FILE
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = LV_DEL
TABLES
DATA_TAB = F_INFILE
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.
*--- Display message if error occurs in uploading data from file
* G_MESG = 'Error in uploading the file'(003).
* MESSAGE I001(Z_MSGCLASS) WITH G_MESG.
* CLEAR G_MESG.
* FG_STOP = 'X'.
* STOP.
ENDIF.
ENDFORM. "Read_inputfile

*&---------------------------------------------------------------------*
*& Form FORMAT_ASC_DATA
*&---------------------------------------------------------------------*
* To format the ASCII data
*----------------------------------------------------------------------*
FORM FORMAT_ASC_DATA.
*--- Loop to format ASCII data
LOOP AT I_INFILE_ASC.
IT_DATA-ZDIMID = I_INFILE_ASC-STRING+0(10).
IT_DATA-ZDIMDESC = I_INFILE_ASC-STRING+10(120).
ENDLOOP.
ENDFORM. " FORMAT_ASC_DATA

*&---------------------------------------------------------------------*
*& Form error_processing
*&---------------------------------------------------------------------*
* To process the error records
*----------------------------------------------------------------------*
FORM ERROR_PROCESSING USING F_IT_DATA
F_MESG.
G_ERROR = G_ERROR + 1.
IT_ERROR = F_IT_DATA.
IT_ERROR-MESSAGE = F_MESG.
APPEND IT_ERROR.
CLEAR IT_ERROR.
ENDFORM. " ERROR_PROCESSING
*&---------------------------------------------------------------------*
*& Form READ_INPUT_DATA
*&---------------------------------------------------------------------*
* To Read the input file data
*----------------------------------------------------------------------*
FORM READ_INPUT_FILE_DATA.

*--- Splitting the filename and its extension
PERFORM SPLIT_FILENAME_EXT.
CASE G_FILEEXT.
WHEN 'XLS'.

*--- Perform to convert the Excel data into an internal table
PERFORM CONVERT_XLS_ITAB.
WHEN 'TXT'.
IF P_FTYPE = 'ASC'.

*--- Uploading the data from server
PERFORM READ_INPUTFILE TABLES I_INFILE_ASC
USING P_FILE P_FTYPE.
*--- Perform Formating ASC type data
PERFORM FORMAT_ASC_DATA.
ELSEIF P_FTYPE = 'DAT'.

*--- Uploading the data from server
PERFORM READ_INPUTFILE TABLES IT_DATA
USING P_FILE P_FTYPE.
ENDIF.
WHEN OTHERS.
G_MESG = 'Invalid Operation'(008).
MESSAGE I001(Z_MSGCLASS) WITH G_MESG.
CLEAR G_MESG.
FG_STOP = 'X'.
STOP.
ENDCASE.
IF NOT IT_DATA[] IS INITIAL.

*--- Perform to delete Header lines
PERFORM DELETE_HEADER_EMPTY_RECS.
ENDIF.
ENDFORM. " READ_INPUT_FILE_DATA
*&---------------------------------------------------------------------*
*& Form SPLIT_FILENAME_EXT
*&---------------------------------------------------------------------*
* To split the filename and the extension
*----------------------------------------------------------------------*
FORM SPLIT_FILENAME_EXT.
MOVE P_FILE TO G_SFILE.
*--- To get the extensions of the file
CALL FUNCTION 'CV120_SPLIT_FILE'
EXPORTING
PF_FILE = G_SFILE
IMPORTING
* PFX_FILE =
PFX_EXTENSION = G_FILEEXT
* PFX_DOTEXTENSION =
.
*--- Checking the case of file extension
IF G_FILEEXT NA SY-ABCDE.
TRANSLATE G_FILEEXT TO UPPER CASE.
ENDIF.
ENDFORM. " SPLIT_FILENAME_EXT
*&---------------------------------------------------------------------*
*& Form MOVE_DATA
*&---------------------------------------------------------------------*
* To move the input file data to an internal table
*----------------------------------------------------------------------*
FORM MOVE_DATA.
DATA : LV_INDEX TYPE I.
FIELD-SYMBOLS .
*--- Sorting the internal table
SORT IT_INTERN BY ROW COL.
CLEAR IT_INTERN.
LOOP AT IT_INTERN.
MOVE IT_INTERN-COL TO LV_INDEX.
*--- Assigning the each record to an internal table row
ASSIGN COMPONENT LV_INDEX OF STRUCTURE IT_DATA TO .
IF SY-SUBRC = 0.
*--- Asigning the field value to a field symbol
MOVE IT_INTERN-VALUE TO .
ENDIF.
AT END OF ROW.
APPEND IT_DATA.
CLEAR IT_DATA.
ENDAT.
ENDLOOP.
ENDFORM. " MOVE_DATA
*&---------------------------------------------------------------------*
*& Form CONVER_XLS_ITAB
*&---------------------------------------------------------------------*
* To convert the excel data into an internal table
*----------------------------------------------------------------------*
FORM CONVERT_XLS_ITAB.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
FILENAME = P_FILE
I_BEGIN_COL = P_BEGCOL
I_BEGIN_ROW = P_BEGROW
I_END_COL = P_ENDCOL
I_END_ROW = P_ENDROW
TABLES
INTERN = IT_INTERN
* EXCEPTIONS
* INCONSISTENT_PARAMETERS = 1
* UPLOAD_OLE = 2
* OTHERS = 3
.
IF SY-SUBRC <> 0. "#EC *
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
IF NOT IT_INTERN[] IS INITIAL.
*--- Perform to move the data into an internal data
PERFORM MOVE_DATA.
ELSE.
G_MESG = 'No values found'(009).
MESSAGE I001(Z_MSGCLASS) WITH G_MESG.
CLEAR G_MESG.
FG_STOP = 'X'.
STOP.
ENDIF.
ENDFORM. " CONVERT_XLS_ITAB

*&---------------------------------------------------------------------*
*& Form DELETE_HEADER_EMPTY_RECS
*&---------------------------------------------------------------------*
* To delete the Header and empty records
*----------------------------------------------------------------------*
FORM DELETE_HEADER_EMPTY_RECS.

DATA: LV_TABIX LIKE SY-TABIX. " Table Index
CLEAR IT_DATA.

*--- To delete the empty lines from internal table
LOOP AT IT_DATA.
LV_TABIX = SY-TABIX.
IF IT_DATA IS INITIAL.
G_EMPTY = G_EMPTY + 1.
DELETE IT_DATA INDEX LV_TABIX..
ENDIF.
ENDLOOP.
CLEAR IT_DATA.

*--- Total no of recs in file
DESCRIBE TABLE IT_DATA LINES G_TOTAL.
IF G_TOTAL = 0.
G_MESG = 'No records in the file'(010).
MESSAGE I001(Z_MSGCLASS) WITH G_MESG.
CLEAR G_MESG.
FG_STOP = 'X'.
STOP.
ENDIF.
ENDFORM. " DELETE_HEADER_EMPTY_RECS

*&---------------------------------------------------------------------*
*& Form DISPLAY_SUCCESS_ERROR_DATA
*&---------------------------------------------------------------------*
* To display the no.of records,success and error records
*----------------------------------------------------------------------*
FORM DISPLAY_SUMMARY_REPORT.
SKIP 3.
FORMAT COLOR COL_HEADING INTENSIFIED ON.

WRITE :/1(132) SY-ULINE.
WRITE :/1(1) SY-VLINE,
65 'SUMMARY REPORT'(019) CENTERED,
132(1) SY-VLINE.
FORMAT INTENSIFIED OFF.
PERFORM FILL_COLOR.
WRITE :/1(132) SY-ULINE.
WRITE :/1(1) SY-VLINE,
2(24) 'Processed Records'(020),
27(2) ' : ',
30 G_TOTAL,
132(1) SY-VLINE.
PERFORM FILL_COLOR.
WRITE :/1(1) SY-VLINE,
2(24) 'Successfully Uploaded'(021),
27(2) ' : ',
30 G_SUCCESS,
132(1) SY-VLINE.
PERFORM FILL_COLOR.
WRITE :/1(1) SY-VLINE,
2(24) 'Error Records'(022),
27(2) ' : ',
30 G_ERROR,
132(1) SY-VLINE.
PERFORM FILL_COLOR.
WRITE :/1(1) SY-VLINE,
2(24) 'Empty Records'(023),
27(2) ' : ',
30 G_EMPTY,
132(1) SY-VLINE.
WRITE :/1(132) SY-ULINE.
ENDFORM. " DISPLAY_SUMMARY_REPORT

*----------------------------------------------------------------------*
* Form : session_report
*----------------------------------------------------------------------*
* To display the list of sessions created
*----------------------------------------------------------------------*
FORM SESSION_REPORT .
IF NOT I_SESSION[] IS INITIAL.
FORMAT COLOR COL_HEADING.

WRITE :/01(132) SY-ULINE.
WRITE :/01(01) SY-VLINE,
65 'SESSIONS CREATED'(024) CENTERED,
132(01) SY-VLINE.
WRITE :/01(132) SY-ULINE.

FORMAT INTENSIFIED OFF.
FORMAT COLOR COL_NORMAL INTENSIFIED OFF.

WRITE :/01(132) SY-ULINE.
LOOP AT I_SESSION.
WRITE : /1 SY-VLINE.

AT FIRST.
WRITE : 5 'Session names'(O25).
ENDAT.

WRITE : 27 ':', 30 I_SESSION-NAME,
132 SY-VLINE.
ENDLOOP.
WRITE : /01(132) SY-ULINE.
ENDIF.
ENDFORM. " session_report

*&---------------------------------------------------------------------*
*& Form FILL_COLOR
*&---------------------------------------------------------------------*
* To display the background color
*----------------------------------------------------------------------*
FORM FILL_COLOR.
IF FG_COLOR = SPACE.
FORMAT COLOR 2.
FG_COLOR = 'X'.
ELSE.
FORMAT COLOR 4.
CLEAR FG_COLOR.
ENDIF.
ENDFORM. " FILL_COLOR

*&---------------------------------------------------------------------*
*& Form UPDATE_TABLE
*&---------------------------------------------------------------------*
* To Update the Table with the Input data
*----------------------------------------------------------------------*
FORM UPDATE_TABLE.
IF NOT IT_DATA IS INITIAL.

LOOP AT IT_DATA.
*--- Modify table with the values from table IT_ZEMPLOYEES
MODIFY ZROIDIM FROM IT_DATA.
IF SY-SUBRC <> 0.
ENDIF.
ENDLOOP.
ENDIF.
ENDFORM. " UPDATE_TABLE

*&---------------------------------------------------------------------*
*& Form PROCESS_DATA
*&---------------------------------------------------------------------*
* To Process the Internal Table to remove empty records
*----------------------------------------------------------------------*
FORM PROCESS_DATA .
DATA: LV_TABIX LIKE SY-TABIX. " Table Index
IF NOT P_HEADER IS INITIAL.
LOOP AT IT_DATA.
IF P_HEADER > 0 AND NOT IT_DATA IS INITIAL.
DELETE IT_DATA FROM 1 TO P_HEADER.
EXIT.
ENDIF.
ENDLOOP.
ENDIF.
CLEAR IT_DATA.

*--- To delete the empty lines from internal table
LOOP AT IT_DATA.
LV_TABIX = SY-TABIX.
IF IT_DATA IS INITIAL.
G_EMPTY = G_EMPTY + 1.
DELETE IT_DATA INDEX LV_TABIX..
ENDIF.
ENDLOOP.

*--- Sort table IT_ZEMPLOYEES by EID and NAME
SORT IT_DATA ASCENDING BY ZDIMID.

*--- Get the Total Number of Records in the internal table
DESCRIBE TABLE IT_DATA LINES G_TOTAL.
IF G_TOTAL = 0.
FG_STOP = 'X'.
STOP.
ENDIF.
IF SY-SUBRC <> 0.

**--- Display message if error occurs in getting file
* G_MESG = 'Error in getting the Filename'(002).
* MESSAGE I001(Z_MSGCLASS) WITH G_MESG.
ENDIF.
ENDFORM. " validate_input_file

No comments: