Friday, August 28, 2020

Message Subfile

Message Subfile:

Message Subfiles (or Subfile Messages) are very useful for screens where it needs to display multiple messages to the user giving the flexibility to the user to view all the messages by taking PAGE DOWN/PAGE UP. 

These messages can be sent using SNDPGMMSG in CL Program. And, RMVMSG can be used to clear these messages from the Message Queue. 

Message Subfile would then load the messages from the Specified Program Message Queue and display them on the screen. 

However, When we use RPG, it require writing CL Program to execute SNDPGMMSG & RMVMSG to Send or Remove Messages to/from the queue. 

Below APIs become very useful to do the same from RPG program rather than having to create another program to do this. 

  • QMHSNDPM - Send Program Message API.
  • QMHRMVPM - Remove Program Message API.

We will see how to create Message Subfile before going on to using these APIs to send the messages to Queue.

When adding new Record Format in DSPF, Select the type as 'SFLMSG' (Subfile message record). This would prompt for 'Subfile control record' similar to Subfiles. 

There are few Important Keywords to remember when it comes to Message Subfiles. 

SFLMSGRCD - Specify the Line Number where the Message to be displayed on the screen.
SFLPGMQ - Type the name of the field that contains the name of the program message queue. Variable used here needs to be assigned with the Program Name. 

Other Subfile keywords (SFLDSP, SFLDSPCTL, SFLINZ, SFLPAG, SFLSIZ) to be defined similar to the usual subfiles. Click Here to see more about Subfile Keywords. 

Below is the Sample code for Subfile Message (DSPF)

     A* Subfile Message  

     A          R PPMSFL01                  SFL

     A                                      SFLMSGRCD(24)

     A            MSGKEY                    SFLMSGKEY

     A            PGMQ                      SFLPGMQ(10)

     A* Subfile Message Control Format

     A          R PPMCTL01                  SFLCTL(PPMSFL01)

     A                                      SFLDSP

     A                                      SFLDSPCTL

     A                                      SFLINZ

     A                                      SFLSIZ(0002)

     A                                      SFLPAG(0001)

     A            PGMQ                      SFLPGMQ(10)    


We will now see How to define the Prototypes for these APIs and how to call them to send the message and remove the message.

QMHSNDPM (Send Program Message):

QMHSNDPM API require 9 Mandatory parameters. see the Prototype declaration below.

  Dcl-PR SendProgramMessage ExtPgm('QMHSNDPM') ;

    MessageIdentifier Char(7)

    MessageFileName Char(20)

    MessageData Char(80) ;

    LengthOfMessageData Packed(10 : 0) ;

    MessageType Char(10) ;

    CallStackEntry Char(10) ;

    CallStackCounter Packed(10 : 0) ;

    MessageKey Char(4) ;

    ErrorData LikeDs(QUSEC) ;

  END-PR;     


Message Identifier - Predefined Message Identifier from Message File.

Message FileName - Message File along with Library Name (*LIBL can be used). First 10 digits for Message file and next 10 digits are for Library Name.

Message Data - If first two parameters are passed, This field would be used to pass the Data for the Variables used in the Message Identifier in Message File. If first two parameters are Blanks, Message passed in this parameter would be displayed. 

Length Of Message Data - Length of Message Data passed in the previous parameter. 

Message Type - Type of the message sent (*CMD - Command, *COMP - Completion, *DIAG - Diagnostic, *ESCAPE - Escape, *INFO - Informational, *NOTIFY - Notify, *RQS - Request and *STATUS - Status)

Call Stack Entry - Used to Identify the Program Message Queue. '*' to be used to use the current Call stack entry.

Call Stack Counter - Number to identify which Program Message Queue to send the message to. '0' indicates the last Program in the Call stack, '1' to indicate the previous program. Alternatively, any integer to identify the Program in the respective position. 

Message Key - This is Output parameter. 

Error Data - Error info. Error Data structure can be used from QUSEC in QSYSINC/QRPGLESRC. 

            MessageIdentifier = 'MSG0001' ;

            MessageFileName = 'PPTESTMSGF*LIBL     ' ;

            MessageData = *Blanks ;

            LengthOfMessageData = %Len(%Trim(MessageData)) ;

            MessageType = '*INFO' ;

            CallStackEntry = '*' ;

            CallStackCounter = 0 ;

            MessageKey = *Blanks ;


            SendProgramMessage(MessageIdentifier

                              :MessageFileName

                              :MessageData

                              :LengthOfMessageData

                              :MessageType

                              :CallStackEntry

                              :CallStackCounter

                              :MessageKey

                              :ErrorData) ; 


This can be called from RPG when ever there is an error and these messages would be displayed on EXFMT. Message Subfile control format to be written before EXFMT. 

      Write PPMCTL01 ; // Message Subfile Control Format

      Write PPFTR01 ; // Subfile Footer Record Format

      Exfmt PPCTL01 ; // Subfile Control Format


Messages need to be cleared whenever user does some action on the screen. So that these messages will not be shown again. 

QMHRMVPM (Remove Program Message):

QMHRMVPM requires 5 mandatory parameters. See the prototype declaration below. 

  Dcl-PR RemoveMessage ExtPgm('QMHRMVPM') ;

    CallStackEntry Char(10) ;

    CallStackCounter Packed(10 : 0) ;

    MesssgeKey Char(4) ;

    MessagestoRemove Char(10) ;

    ErrorData LikeDs(QUSEC) ;

  End-PR;    


Call Stack Entry - Used to Identify the Program Message Queue. '*' to be used to use the current Call stack entry.

Call Stack Counter - Number to identify which Program Message Queue to send the message to. '0' indicates the last Program in the Call stack, '1' to indicate the previous program. Alternatively, any integer to identify the Program in the respective position. 

Message Key - This is Output parameter. 

Messages to Remove - Used to define a message or group of messages to be removed. *ALL - to remove all the messages

Error Data - Error info. Error Data structure can be used from QUSEC in QSYSINC/QRPGLESRC. 

      CallStackEntry = '*' ;

      CallStackCounter = 0 ;

      MessageKey = *Blanks ;

      MessagestoRemove = '*ALL' ;


      RemoveMessage(CallStackEntry

                   :CallStackCounter

                   :MessageKey

                   :MessagestoRemove

                   :ErrorData) ;



Tuesday, August 25, 2020

How to run CL commands in RPGLE (Using QCMDEXC)?

QCMDEXC:

CL (Control Language) has always been (and continue to be) the best choice for the programs which require more interaction with the Operating System. 

However, We do see many cases where there is a need to run these commands from other HLL programs (like. RPGLE). And, it's not always best to write a CL Program to serve this purpose. 

QCMDEXC (Execute Command) API becomes very useful in this case. 

This API Requires two Input parameters.
  • Command String - Character field with Variable length (maximum length of 32,702 characters).
  • Length Of Command String - Decimal field with 15 digits (5 decimal places). 
Here is a sample code with a simple 'DSPMSG' command. 

**Free


  Dcl-PR ExecuteCommand ExtPgm('QCMDEXC') ;

    Command Char(128) ;

    Length Packed(15 : 5) ;

  End-PR;


  Dcl-S Command Char(128) ; //Maximum length allowed is 32,702

  Dcl-S Length Packed(15 : 5) ;


  Command = 'DSPMSG' ;

  Length = %Len(%Trim(Command)); // Length of the Command being executed


  ExecuteCommand(Command : Length) ;


  *InLr = *On ; 

Thursday, August 20, 2020

Expandable Subfile - Example Program

Subfile:

Subfiles are one of the most used concepts on IBM i. 

There are three different types of Subfiles. 
  • Load All Subfile (Click Here to see more about Load All Subfile with an example).
  • Expandable Subfile
  • Single Page Subfile

Message Subfile is used to display messages on the screen. Click Here to see more about Message Subfiles. 

We will see more about Expandable Subfile in this article with an example. Variable has been used to display message on the screen in this example. Message subfile can be used instead.

Expandable Subfile:

As the name suggests Subfile is expanded (loaded) as we press the 'PAGE DOWN' Button. 

Only one page (equal to the number of records mentioned against SFLPAG in DSPF) is loaded on the initial load and the next page is loaded when we press 'PAGE DOWN'. 

Logic for PAGE DOWN needs to be handled in the Program and PAGE UP is taken care by the system (using the records already loaded from Subfile buffer). 

However, Maximum number of records allowed are 9999 similar to Load All Subfile. 

SFLSIZ should be declared with 1 record greater than the number of records declared in SFLPAG Keyword in DDS (SFLSIZ = SFLPAG + 1).

Here is an example with a bit of Options processing using READC & SFLNXTCHG

Physical File:

DDS for Physical file (PPTESTFILE). Any key fields required are to be added as necessary. 

     A          R TESTFILER                          
     A            TFIELD1       10A                  
     A            TFIELD2       10A                  
     A            TFIELD3       10A                  
     A            TFIELD4       10A                  

We can create the Table using below SQL script alternatively.

CREATE TABLE QTEMP/PPTESTFILE 
    (TFIELD1 CHAR (10) NOT NULL WITH DEFAULT, 
     TFIELD2 CHAR (10) NOT NULL WITH DEFAULT, 
     TFIELD3 CHAR (10) NOT NULL WITH DEFAULT, 
     TFIELD4 CHAR (10) NOT NULL WITH DEFAULT)

Display File (DSPF):

DDS for Display file (PPEXPSFLD)

     A* Record Format for Subfile Data                              
     A                                      DSPSIZ(24 80 *DS3)      
     A          R PPSFL01                   SFL                     
     A  70                                  SFLNXTCHG               
     A            SOPT           1A  B 10  4                        
     A            SFIELD1       10A  O 10  9                        
     A            SFIELD2       10A  O 10 28                        
     A            SFIELD3       10A  O 10 48                        
     A            SFIELD4       10A  O 10 66                        
     A* Record Format for Subfile Control                           
     A          R PPCTL01                   SFLCTL(PPSFL01)         
     A                                      CA03(03 'Exit')         
     A                                      PAGEDOWN(41 'Page Down')
     A                                      OVERLAY                 
     A                                      SFLCSRRRN(&CSRRRN)      
     A  25                                  SFLDSP                  
     A  26                                  SFLDSPCTL               
     A  27                                  SFLCLR                  
     A  40                                  SFLEND(*MORE)           
     A                                      SFLSIZ(0009)            
     A                                      SFLPAG(0008)            
     A                                  1  2USER                           
     A                                  1 32'Expandable Subfile'           
     A                                      COLOR(WHT)                     
     A                                  1 73DATE                           
     A                                      EDTCDE(Y)                      
     A                                  2 73TIME                           
     A                                  2 37'Example'                      
     A                                      COLOR(WHT)                     
     A                                  4  3'Select Option, Press Enter...'
     A                                      COLOR(BLU)                     
     A                                  6  3'5=Display'                    
     A                                      COLOR(BLU)                     
     A                                  8  9'Field - 1'                    
     A                                      DSPATR(UL)                     
     A                                      COLOR(WHT)                     
     A                                  8 28'Field - 2'                    
     A                                      DSPATR(UL)                     
     A                                      COLOR(WHT)                     
     A                                  8 48'Field - 3'                    
     A                                      DSPATR(UL)                     
     A                                      COLOR(WHT)       
     A                                  8 66'Field - 4'      
     A                                      DSPATR(UL)       
     A                                      COLOR(WHT)       
     A                                  8  3'Opt'            
     A                                      DSPATR(UL)       
     A                                      COLOR(WHT)       
     A            SRRN           4S 0H      SFLRCDNBR(CURSOR)
     A            CSRRRN         5S 0H                       
     A* Record Format for Footer                             
     A          R PPFTR01                                    
     A                                      OVERLAY          
     A                                 23  2'F3=Exit'        
     A                                      COLOR(BLU)       
     A            MESSAGE       70   O 24  2                 

Program (RPGLE):

Below is the Sample RPGLE program (PPEXPSFLR) written in Full free format RPGLE. 

**Free

  // ------------------------------------------------------------------------

  // Expandable Subfile - Example Program

  // ------------------------------------------------------------------------


  // Input File

  Dcl-F PPTESTFILE Keyed ;


  // Expandable Subfile

  Dcl-F PPEXPSFLD WorkStn Sfile(PPSFL01 : wRRN) ;


  // Stand-Alone Variables Declaration

  Dcl-S wNoOfRecords Packed(1 : 0) ;

  Dcl-S wRrn Packed(4 : 0) ;

  Dcl-S wError Ind ;

  Dcl-S wNoOptions Packed(4 : 0) ;


  // ------------------------------------------------------------------------

  // Main Process

  // ------------------------------------------------------------------------

  ExSr SrInitializeSubfile ;

  ExSr SrLoadSubfile ;

  ExSr SrDisplaySubfile ;

  ExSr SrFinalRoutine ;


  // ------------------------------------------------------------------------

  // Initialize Subfile Routine

  // ------------------------------------------------------------------------

  BegSr SrInitializeSubfile ;


    *In25 = *Off ;

    *In26 = *Off ;

    *In27 = *On ;

    wRrn = 0 ;

    SRrn = 1 ;


    Write PPCTL01 ;


    *In26 = *On ;

    *In27 = *Off ;


    Setll *Start PPTESTFILE ;


  EndSr;


  // ------------------------------------------------------------------------

  // Load Subfile Routine

  // ------------------------------------------------------------------------

  BegSr SrLoadSubfile ;


    wNoOfRecords = 0 ;


    Read PPTESTFILE ;

    Dow Not %Eof(PPTESTFILE)

      And wNoOfRecords < 8

      And wRrn < 9999 ;


      SFIELD1    = TFIELD1    ;

      SFIELD2    = TFIELD2    ;

      SFIELD3    = TFIELD3    ;

      SFIELD4    = TFIELD4    ;


      wRrn += 1 ;

      wNoOfRecords += 1;


      Write PPSFL01 ;


      If wNoOfRecords < 8 ;

        Read PPTESTFILE ;

      EndIf ;


    EndDo;


    wNoOfRecords = 0 ;


    If %Eof(PPTESTFILE) or wRRN = 9999 ;

      *In40 = *On ;

    EndIf;


  EndSr;


  // ------------------------------------------------------------------------

  // Display Subfile Routine

  // ------------------------------------------------------------------------

  BegSr SrDisplaySubfile ;


    Dow *In03 = *Off ;


      If wRrn > 0 ;

        *In25 = *On ;

      EndIf;


      If CsrRrn > 0 ;

        SRrn = CsrRrn ;

      EndIf ;


      Write PPFTR01 ;

      Exfmt PPCTL01 ;


      Select ;

        When *In03 = *On ;

          Leave ;

        When *In41 = *On ;

          ExSr SrLoadSubfile ;

          CsrRrn = 0 ;

          SRrn += 8 ;

        Other ;

          ExSr SrValidateOptions ;

          If wError ;

            MESSAGE = 'Ivalid Options Selected.' ;

          Else ;

            ExSr SrProcessOptions ;

          EndIf ;

      EndSl;


    EndDo;


  EndSr;


  // ------------------------------------------------------------------------

  // Validate Subfile Options

  // ------------------------------------------------------------------------

  BegSr SrValidateOptions ;


    wError = *Off ;


    ReadC PPSFL01 ;

    Dow Not %Eof(PPEXPSFLD) ;


      If SOpt <> '5' And SOpt <> ' ' ;

        wError = *On ;

        *In60 = *On ;

      Else ;

        *In60 = *Off ;

      EndIf;


      *In70 = *On ;

      Update PPSFL01 ;


      ReadC PPSFL01 ;

    EndDo;


  EndSr;


  // ------------------------------------------------------------------------

  // Process Subfile Options

  // ------------------------------------------------------------------------

  BegSr SrProcessOptions ;


    wNoOptions = 0 ;

    ReadC PPSFL01 ;

    Dow Not %Eof(PPEXPSFLD) ;

      If SOpt = '5' ;

        wNoOptions += 1 ;

      EndIf ;

      ReadC PPSFL01 ;

    EndDo ;


    If wNoOptions > 0 ;

      MESSAGE = %Char(wNoOptions) + ' Records Selected.' ;

    EndIf ;


  EndSr;


  // ------------------------------------------------------------------------

  // Final Routine

  // ------------------------------------------------------------------------

  BegSr SrFinalRoutine ;


    *InLr = *On;


  EndSr; 


Different Ways of Sorting Data in a List - Python

Sorting Data in a List List is a collection of data (of different data types), much like an array. Like any data structure or data set, dat...