SACASCR1 (Totally/Fully Free RPG)

1) CRTSQLRPGI OBJ(SACASCR1) OBJTYPE(*MODULE) RPGPPOPT(*LVL2) DBGVIEW(*SOURCE)

2) CRTPGM PGM(SACASCR1) MODULE(SACASCR1 JSON00R) BNDSRVPGM((YAJL) (YAJLR4) (QHTTPSVR/QZHBCGI)) ACTGRP(REST)  

Updated 2020/02/06 - Removed four needless lines of code 
Updated 2020/02/24 - Numeric zero key not working 

        //=======================================================================
      //=  Author...........: Esteban Cabero                                  =
      //=  Date.............: 2020/01/13                                      =
      //=  Description......: File SACASCP REST API - GET                     =
      //=  Program summary..: REST API                                        =
      //=  Before Compile...: CRTSQLRPGI OBJ(SACASCR1) OBJTYPE(*MODULE)       =
      //=                                RPGPPOPT(*LVL2) DBGVIEW(*SOURCE)     =
      //=                                                                     =
      //=                     CRTPGM PGM(SACASCR1) MODULE(SACASCR1 JSON00R)   =
      //=                     BNDSRVPGM((YAJL) (YAJLR4) (QHTTPSVR/QZHBCGI))   =
      //=                     ACTGRP(REST)                                    =
      //=  Amendments:                                                        =
      //=  DD.MM.YYYY  Name             Comment with shortcode                =
      //=                                                                     =
      //=======================================================================
        CTL-OPT
        DATEDIT(*DMY)
        DATFMT(*EUR.)
        TIMFMT(*HMS:)
        OPTIMIZE(*NONE)
        TEXT(*SRCMBRTXT)
        OPTION(*SRCSTMT)
        BNDDIR('QC2LE')
        BNDDIR('@SYSFUNC')
        PGMINFO(*PCML: *MODULE)
        BNDDIR('YAJL');
      //=======================================================================
      //   D E C L A R E   I N P U T   P A R A M E T E R S                    =
      //=======================================================================
        DCL-PR SACASCR1    EXTPGM('SACASCR1');
        END-PR;
        DCL-PI SACASCR1;
        END-PI;
        DCL-PR JSON00R     EXTPGM;
               ERRMSG      VARCHAR(80);
               METHOD      VARCHAR(10);
               API         VARCHAR(10);
               PARAMETER   VARCHAR(1000);
        END-PR;
        DCL-PR EnvError;
        END-PR;
        DCL-PR GETRec;
               FIELD       LIKE(FILE_T.SACASC) CONST;
               DATA        LIKEDS(DATA_T);
        END-PR;
        DCL-PR CreateJSON;
               DATA        LIKEDS(DATA_T) CONST;
               JSONDATA    LIKE(JSONBUF_T);
               SUCCESS     IND CONST;
        END-PR;
        DCL-PR PUTRec;
        END-PR;
        DCL-PR POSTRec;
        END-PR;
        DCL-PR DELETERec;
        END-PR;
        //===================================================================
        /COPY YAJL_H
        //===================================================================
        //   D E C L A R E   D A T A S T R U C T U R E   A R R A Y S        =
        //===================================================================
        DCL-DS FILE_T           QUALIFIED TEMPLATE;
               SACASC           CHAR(2);
               SACASD           CHAR(40);
               SACASF           CHAR(200);
               SACASA           CHAR(3);
               SACASP           CHAR(3);
               SACASE           CHAR(3);
               SACASR           CHAR(3);
               SACASM           CHAR(3);
               SACASB           CHAR(10);
               ISODAT           TIMESTAMP;
               UID              CHAR(10);
        END-DS;
        DCL-DS DATA_T           QUALIFIED TEMPLATE;
               SUCCESS          IND;
               ERRMSG           CHAR(80);
               COUNT            INT(10);
               FILE1            LIKEDS(FILE_T) DIM(999);
               FILE2            LIKEDS(FILE_T) DIM(999);
        END-DS;
        DCL-DS DATA             LIKEDS(DATA_T);
        DCL-DS FILE1            LIKEDS(FILE_T);
        DCL-DS FILE2            LIKEDS(FILE_T);
      //=====================================================================
      //   D E C L A R E   V A R I A B L E S                                =
      //=====================================================================
        DCL-S  ERRMSG        VARCHAR(80)                        INZ(*BLANKS);
        DCL-S  METHOD        VARCHAR(10)                        INZ(*BLANKS);
        DCL-S  API           VARCHAR(10)                        INZ('SACASCR1');
        DCL-S  PARAMETER     VARCHAR(1000)                      INZ(*BLANKS);
        DCL-S  FIELD         LIKE(FILE_T.SACASC);
        DCL-S  JSONBUF_T     VARCHAR(200000)                    TEMPLATE;
        DCL-S  X             INT(10);
        DCL-S  JSONBUF       LIKE(JSONBUF_T);
        DCL-S  HEADER        VARCHAR(500);
      //=====================================================================
      //   M A I N   F L O W   O F   P R O G R A M                          =
      //=====================================================================
        JSON00R(ERRMSG:METHOD:API:PARAMETER);
           IF ERRMSG = *BLANKS;
              // Environment error
              MONITOR;
                 FIELD = PARAMETER;
              ON-ERROR;
                 FIELD = *BLANKS;
              ENDMON;
              SELECT;
                 WHEN METHOD = 'GET';
                    // Code to retrieve record/s
                    GETRec(FIELD:DATA);
                 WHEN METHOD = 'PUT';
                    // Code to update a record (idempotent)
                    PUTRec();
                 WHEN METHOD = 'POST';
                    // Code to write a new record (non-idempotent)
                    POSTRec();
                 WHEN METHOD = 'DELETE';
                    DELETERec();
                    // Code to delete a record
              ENDSL;
           ELSE;
              EnvError();
           ENDIF;
              CreateJSON(DATA:JSONBUF:DATA.SUCCESS);
        *INLR = *ON;
        RETURN;
        //=====================================================================
        //   P R O S E D U R E S                                              =
        //=====================================================================
        //---------------------------------------------------------------------
        //--   Environment error
        //---------------------------------------------------------------------
        DCL-PROC EnvError;
        DCL-PI   EnvError;
        END-PI;
                 DATA.SUCCESS = *OFF;
                 DATA.ERRMSG = ERRMSG;
                 DATA.COUNT = 0;
                 RETURN;
        END-PROC;
        //---------------------------------------------------------------------
        //--   ReadData(): Read data into the data struct
        //--
        //--      FIELD = (input) the the record to read, or *Blanks
        //--                 for all records
        //--
        //--       Data = (output) the 'data' data structure that contains
        //--                 the data to be output
        //---------------------------------------------------------------------
        DCL-PROC GETRec;
        DCL-PI   GETRec;
                 FIELD       LIKE(FILE_T.SACASC) CONST;
                 DATA        LIKEDS(DATA_T);
        END-PI;
           CLEAR DATA;
           IF PARAMETER = *BLANKS;
                 EXEC SQL DECLARE FILE1 CURSOR FOR
                      SELECT SACASC, SACASD, SACASF, SACASA, SACASP,
                      SACASE, SACASR, SACASM, SACASB, ISODAT, UID
                        FROM SACASCP
                      ORDER BY SACASC;
                 EXEC SQL OPEN FILE1;
                 EXEC SQL FETCH NEXT FROM FILE1 INTO :FILE1;
           ELSE;
                 EXEC SQL DECLARE FILE2 CURSOR FOR
                      SELECT SACASC, SACASD, SACASF, SACASA, SACASP,
                      SACASE, SACASR, SACASM, SACASB, ISODAT, UID
                        FROM SACASCP
                      WHERE SACASC = :FIELD;
                 EXEC SQL OPEN FILE2;
                 EXEC SQL FETCH NEXT FROM FILE2 INTO :FILE2;
           ENDIF;

                 IF %SUBST(SQLSTT:1:2)='00' or %SUBST(SQLSTT:1:2)='01';
                    DATA.COUNT = 0;
                    DATA.SUCCESS = *ON;
                 ELSEIF %SUBST(SQLSTT:1:2) = '02';
                    DATA.SUCCESS = *OFF;
                    DATA.ERRMSG = 'No records found in option file SACASCP';
                 ELSE;
                    DATA.SUCCESS = *OFF;
                    DATA.ERRMSG = 'SQL statement failed; see job log.';
                 ENDIF;

           IF PARAMETER = *BLANKS;
                 DOW %SUBST(SQLSTT:1:2)='00' or %SUBST(SQLSTT:1:2)='01';
                    DATA.COUNT += 1;
                    EVAL-CORR DATA.FILE1(DATA.COUNT) = FILE1;
                    EXEC SQL FETCH NEXT FROM FILE1 INTO :FILE1;
                 ENDDO;
                 EXEC SQL CLOSE FILE1;
           ELSE;
                 DOW %SUBST(SQLSTT:1:2)='00' or %SUBST(SQLSTT:1:2)='01';
                    DATA.COUNT += 1;
                    EVAL-CORR DATA.FILE1(DATA.COUNT) = FILE2;
                    EXEC SQL FETCH NEXT FROM FILE2 INTO :FILE2;
                 ENDDO;
                 EXEC SQL CLOSE FILE2;
           ENDIF;
                 RETURN;
        END-PROC;
        //---------------------------------------------------------------------
        //--   CreateJSON():  Transform the 'data' ds/array into JSON
        //--
        //--       data = (input) data struct with data to return
        //--   jsonData = (output) the same data in JSON format
        //--
        //--   returns YAJL generator status
        //---------------------------------------------------------------------
        DCL-PROC CreateJSON;
        DCL-PI   CreateJSON;
                 DATA        LIKEDS(DATA_T) CONST;
                 JSONDATA    LIKE(JSONBUF_T);
                 SUCCESS     IND CONST;
        END-PI;

        //--   Generated document is "pretty" for test (readable)
        //       YAJL_GENOPEN(*ON);
        //--   Generated document is not "pretty"  for live
        //--   More efficient for the computer to process
                 YAJL_GENOPEN(*OFF);

                 YAJL_BEGINOBJ();
                   YAJL_addBool('SUCCESS': DATA.SUCCESS );
                   YAJL_addChar('ERRMSG': %TRIMR(DATA.ERRMSG));
                   YAJL_addNum('COUNT': %CHAR(DATA.COUNT) );

                   YAJL_beginArray('FILE1');
                     FOR X = 1 TO DATA.COUNT;
                       YAJL_beginObj();
                         YAJL_addChar
                          ('StatusCode'        :%TRIMR(DATA.FILE1(X).SACASC));
                         YAJL_addChar
                          ('StatusDescription' :%TRIMR(DATA.FILE1(X).SACASD));
                         YAJL_addChar
                          ('StatusDefinition'  :%TRIMR(DATA.FILE1(X).SACASF));
                         YAJL_addChar
                          ('AdverseClosure'    :%TRIMR(DATA.FILE1(X).SACASA));
                         YAJL_addChar
                          ('PositiveClosure'   :%TRIMR(DATA.FILE1(X).SACASP));
                         YAJL_addChar
                          ('Closure'           :%TRIMR(DATA.FILE1(X).SACASE));
                         YAJL_addChar
                          ('RepeatEver'        :%TRIMR(DATA.FILE1(X).SACASR));
                         YAJL_addChar
                          ('RepeatMonthonMonth':%TRIMR(DATA.FILE1(X).SACASM));
                         YAJL_addChar
                          ('D/MorBoth'         :%TRIMR(DATA.FILE1(X).SACASB));
                         YAJL_addChar
                          ('DateCreated'       :%TRIMR(%CHAR(%TIMESTAMP
                                               (DATA.FILE1(X).ISODAT):*ISO)));
                         YAJL_addChar
                          ('CreatorUserID'     :%TRIMR(DATA.FILE1(X).UID));
                       YAJL_endObj();
                     ENDFOR;
                   YAJL_endArray();

                 YAJL_endObj();

                 HEADER = 'Content-type: application/json; charset=utf-8';
                 IF DATA.SUCCESS;
                    yajl_writeStdout(200: HEADER);
                 ELSE;
                    yajl_writeStdout(500: HEADER);
                 ENDIF;

                 YAJL_genClose();
                 RETURN;
        END-PROC;
        //---------------------------------------------------------------------
        DCL-PROC POSTRec;
        DCL-PI   POSTRec;
        END-PI;
                 DATA.SUCCESS = *OFF;
                 DATA.ERRMSG = 'POST not allowed';
                 DATA.COUNT = 0;
                 RETURN;
        END-PROC;
        //---------------------------------------------------------------------
        DCL-PROC PUTRec;
        DCL-PI   PUTRec;
        END-PI;
                 DATA.SUCCESS = *OFF;
                 DATA.ERRMSG = 'PUT not allowed';
                 DATA.COUNT = 0;
                 RETURN;
        END-PROC;
        //---------------------------------------------------------------------
        DCL-PROC DELETERec;
        DCL-PI   DELETERec;
        END-PI;
                 DATA.SUCCESS = *OFF;
                 DATA.ERRMSG = 'DELETE not allowed';
                 DATA.COUNT = 0;
                 RETURN;
        END-PROC;
        //---------------------------------------------------------------------

No comments:

Post a Comment