      * ILE RPG example of a SOAP provider that returns a list of data
      * using IBM's Integrated Web Services (IWS) server.
      *
      * Given a customer number & date range, this will return invoices
      * for that customer. All input/output is passed through parameters.
      *
      * To compile:
      *> CRTSQLRPGI SOAPINV SRCFILE(QRPGLESRC) DBGVIEW(*SOURCE)
      *

     H OPTION(*SRCSTMT: *NODEBUGIO) PGMINFO(*PCML:*MODULE)
     H DFTACTGRP(*NO) ACTGRP('KLEMENT') BNDDIR('QC2LE')

      *  To see the PCML, remove the PGMINFO above
      *   and do this:
      *   CRTSQLRPGI SOAPINV SRCFILE(QRPGLESRC) DBGVIEW(*SOURCE) -
      *           COMPILEOPT('PGMINFO(*PCML) INFOSTMF(''soapinv.pcml'')')

     D row             ds                  qualified inz
     D   inv                          5a
     D   date                         8s 0
     D   name                        25a
     D   amount                       9p 2
     D   weight                       9p 1

     D SOAPINV         PR                  ExtPgm('SOAPINV')
     D   CustNo                       4p 0 const
     D   fromDate                     8p 0 const
     D   toDate                       8p 0 const
     D   rtnCount                    10i 0
     D   rtnList                           likeds(row) dim(999)
     D SOAPINV         PI
     D   CustNo                       4p 0 const
     D   fromDate                     8p 0 const
     D   toDate                       8p 0 const
     D   rtnCount                    10i 0
     D   rtnList                           likeds(row) dim(999)

     D QMHSNDPM        PR                  ExtPgm('QMHSNDPM')
     D   MessageID                    7A   Const
     D   QualMsgF                    20A   Const
     D   MsgData                  32767A   Const options(*varsize)
     D   MsgDtaLen                   10I 0 Const
     D   MsgType                     10A   Const
     D   CallStkEnt                  10A   Const
     D   CallStkCnt                  10I 0 Const
     D   MessageKey                   4A
     D   ErrorCode                 8192A   options(*varsize)

     D err             ds                  qualified
     D   bytesProv                   10i 0 inz(0)
     D   bytesAvail                  10i 0 inz(0)

     D MsgDta          s           1000a   varying
     d MsgKey          s              4a

      /free
          *inlr = *on;
          exec SQL set option naming=*SYS;
          rtnCount = 0;

          exec SQL declare C1 cursor for
              select aiOrdn, aiIDat, aiSNme, aiDamt, aiLbs
                from ARSHIST
               where aiCust = :CustNo
                 and aiIDat between :fromDate
                                and :toDate;

          exec SQL open C1;
          exec SQL fetch next from C1 into :row;

          if sqlstt<>'00000'
             and %subst(sqlstt:1:2) <> '01'
             and %subst(sqlstt:1:2) <> '02';
             msgdta = 'Failed with SqlState=' + sqlstt;
             QMHSNDPM( 'CPF9897'
                     : 'QCPFMSG   *LIBL'
                     : msgdta
                     : %len(msgdta)
                     : '*ESCAPE'
                     : '*PGMBDY'
                     : 1
                     : MsgKey
                     : err );
             return;
          endif;

          dow sqlstt='00000' or %subst(sqlstt:1:2)='01';
             if rtnCount < %elem(rtnList);
                rtnCount = rtnCount + 1;
                rtnList(rtnCount) = row;
             endif;
             exec SQL fetch next from C1 into :row;
          enddo;

          exec SQL close C1;
          return;
      /end-free
