     **
     **  Program . . : CBX3021
     **  Description : Work with Workload Capping Groups - CCP
     **  Author  . . : Carsten Flensburg
     **
     **
     **
     **  Compile options:
     **    CrtRpgMod   Module( CBX3021 )
     **                DbgView( *LIST )
     **
     **    CrtPgm      Pgm( CBX3021 )
     **                Module( CBX3021 )
     **                ActGrp( *NEW )
     **
     **-- Header specifications:  --------------------------------------------**
     H Option( *SrcStmt )

     **-- System information:
     D PgmSts         SDs                  Qualified  NoOpt
     D  PgmNam           *Proc
     D  Status                        5a   Overlay( PgmSts: 11 )
     D  StmNbr                        8a   Overlay( PgmSts: 21 )
     D  MsgId                         7a   Overlay( PgmSts: 40 )
     D  PgmLib                       10a   Overlay( PgmSts: 81 )
     D  Msg                          80a   Overlay( PgmSts: 91 )
     D  JobId                        26a   Overlay( PgmSts: 244 )
     D  CurJob                       10a   Overlay( PgmSts: 244 )
     D  UsrPrf                       10a   Overlay( PgmSts: 254 )
     D  JobNbr                        6a   Overlay( PgmSts: 264 )
     **-- API error data structure:
     D ERRC0100        Ds                  Qualified
     D  BytPrv                       10i 0 Inz( %Size( ERRC0100 ))
     D  BytAvl                       10i 0
     D  MsgId                         7a
     D                                1a
     D  MsgDta                      128a

     **-- Global constants:
     D EXT_ATR         c                   ' '
     D USP_SIZ         c                   65535
     D INZ_VAL         c                   x'00'
     D PUB_AUT         c                   '*CHANGE'
     D TXT_DSC         c                   ' '
     D USP_RPL         c                   '*YES'
     **
     D OFS_MSGDTA      c                   16
     D ADP_PRV_INVLVL  c                   1
     **-- UIM constants:
     D PNLGRP_Q        c                   'CBX3021P  *LIBL     '
     D PNLGRP          c                   'CBX3021P  '
     D SCP_AUT_RCL     c                   -1
     D RDS_OPT_INZ     c                   'N'
     D PRM_IFC_0       c                   0
     D CLO_NORM        c                   'M'
     D FNC_EXIT        c                   -4
     D FNC_CNL         c                   -8
     D KEY_F05         c                   5
     D KEY_F24         c                   24
     D RTN_ENTER       c                   500
     D HLP_WDW         c                   'N'
     D POS_TOP         c                   'TOP'
     D POS_BOT         c                   'BOT'
     D LIST_COMP       c                   'ALL'
     D LIST_SAME       c                   'SAME'
     D EXIT_SAME       c                   '*SAME'
     D TRIM_SAME       c                   'S'

     **-- Global variables:
     D Idx             s             10i 0
     D SysDts          s               z
     **
     D SpcAutLst       s             10a   Dim( 8 )
     D AutFlg          s              1a

     **-- UIM variables:
     D UIM             Ds                  Qualified
     D  AppHdl                        8a
     D  LstHdl                        4a
     D  EntHdl                        4a
     D  FncRqs                       10i 0
     D  EntLocOpt                     4a
     D  LstPos                        4a
     **-- List attributes structure:
     D LstAtr          Ds                  Qualified
     D  LstCon                        4a
     D  ExtPgmVar                    10a
     D  DspPos                        4a
     D  AlwTrim                       1a

     **-- UIM Panel exit program record:
     D ExpRcd          Ds                  Qualified
     D  ExitPg                       20a   Inz( 'CBX3021E  *LIBL' )
     **-- UIM Panel header record:
     D HdrRcd          Ds                  Qualified
     D  SysDat                        7a
     D  SysTim                        6a
     D  TimZon                       10a
     D  WlgRqs                       10a
     **-- UIM List entry:
     D LstEnt          Ds                  Qualified  Inz
     D  Option                        5i 0
     D  WlcGrp                       10a
     D  MaxPrcLmt                     5i 0
     D  NbrPrdEnt                     5i 0
     **
     D LstEntPos       Ds                  LikeDs( LstEnt )

     **-- API return information data structures:
     D WLCR0100        Ds         65535    Qualified
     D  BytRtn                       10i 0
     D  BytAvl                       10i 0
     D  WlcGrp                       10a
     D                                2a
     D  WlgPrcCap                    10i 0
     D  OfsWlgLst                    10i 0
     D  NbrWlgLstE                   10i 0
     D  LenWlgLstE                   10i 0
     **
     D WlgProd         Ds                  Qualified  Based( pWlgProd )
     D  PrdId                         7a
     D  LicTerm                       6a
     D  Feature                       4a
     **
     D WLCR0200        Ds         65535    Qualified
     D  BytRtn                       10i 0
     D  BytAvl                       10i 0
     D  OfsWlgLst                    10i 0
     D  NbrWlgLstE                   10i 0
     D  LenWlgLstE                   10i 0
     **
     D WlgLst          Ds                  Qualified  Based( pWlgLst )
     D  WlcGrp                       10a
     D  MaxPrcLmt                    10i 0

     **-- API query data structures:
     D WLCQ0100        Ds                  Qualified
     D  PrdId                         7a
     D  LicTerm                       6a
     D  Feature                       4a
     D  Option                        4a
     **
     D WLCQ0200        Ds                  Qualified
     D  PrdDirNamLen                 10i 0
     D  DirNamCcsId                  10i 0
     D  PrdDirNam                  1024a
     **
     D WLCQ0300        Ds                  Qualified
     D  WlcGrp                       10a

     **-- Check special authority
     D ChkSpcAut       Pr                  ExtPgm( 'QSYCUSRS' )
     D  AutInf                        1a
     D  UsrPrf                       10a   Const
     D  SpcAut                       10a   Const  Dim( 8 )  Options( *VarSize )
     D  NbrAut                       10i 0 Const
     D  CalLvl                       10i 0 Const
     D  Error                     32767a          Options( *VarSize )
     **-- Retrieve workload groups information:
     D RtvWlgInf       Pr                  ExtPgm( 'QLZRTVWC' )
     D  RcvVar                    65535a   Const  Options( *VarSize )
     D  RcvVarLen                    10i 0 Const
     D  RcvFmtNam                     8a   Const
     D  QryVal                    65535a   Const  Options( *VarSize )
     D  QryFmtNam                     8a   Const
     D  Error                     32767a          Options( *VarSize )
     **-- Retrieve data area:
     D RtvDtaAra       Pr                  ExtPgm( 'QWCRDTAA' )
     D  RcvVar                    32767a          Options( *VarSize )
     D  RcvVarLen                    10i 0 Const
     D  DtaAra_q                     20a   Const
     D  StrPos                       10i 0 Const
     D  DtaLen                       10i 0 Const
     D  Error                     32767a          Options( *VarSize )
     **-- Send program message:
     D SndPgmMsg       Pr                  ExtPgm( 'QMHSNDPM' )
     D  MsgId                         7a   Const
     D  MsgFil_q                     20a   Const
     D  MsgDta                      128a   Const
     D  MsgDtaLen                    10i 0 Const
     D  MsgTyp                       10a   Const
     D  CalStkE                      10a   Const  Options( *VarSize )
     D  CalStkCtr                    10i 0 Const
     D  MsgKey                        4a
     D  Error                     32767a          Options( *VarSize )

     **-- Open display application:
     D OpnDspApp       Pr                  ExtPgm( 'QUIOPNDA' )
     D  AppHdl                        8a
     D  PnlGrp_q                     20a   Const
     D  AppScp                       10i 0 Const
     D  ExtPrmIfc                    10i 0 Const
     D  FulScrHlp                     1a   Const
     D  Error                     32767a          Options( *VarSize )
     D  OpnDtaRcv                   128a          Options( *NoPass: *VarSize )
     D  OpnDtaRcvLen                 10i 0 Const  Options( *NoPass )
     D  OpnDtaRcvAvl                 10i 0        Options( *NoPass )
     **-- Display panel:
     D DspPnl          Pr                  ExtPgm( 'QUIDSPP' )
     D  AppHdl                        8a   Const
     D  FncRqs                       10i 0
     D  PnlNam                       10a   Const
     D  ReDspOpt                      1a   Const
     D  Error                     32767a          Options( *VarSize )
     D  UsrTsk                        1a   Const  Options( *NoPass )
     D  CalStkCnt                    10i 0 Const  Options( *NoPass )
     D  CalMsgQue                   256a   Const  Options( *NoPass: *VarSize )
     D  MsgKey                        4a   Const  Options( *NoPass )
     D  CsrPosOpt                     1a   Const  Options( *NoPass )
     D  FinLstEnt                     4a   Const  Options( *NoPass )
     D  ErrLstEnt                     4a   Const  Options( *NoPass )
     D  WaitTim                      10i 0 Const  Options( *NoPass )
     D  CalMsgQueLen                 10i 0 Const  Options( *NoPass )
     D  CalQlf                       20a   Const  Options( *NoPass )
     **-- Put dialog variable:
     D PutDlgVar       Pr                  ExtPgm( 'QUIPUTV' )
     D  AppHdl                        8a   Const
     D  VarBuf                    32767a   Const  Options( *VarSize )
     D  VarBufLen                    10i 0 Const
     D  VarRcdNam                    10a   Const
     D  Error                     32767a          Options( *VarSize )
     **-- Get dialog variable:
     D GetDlgVar       Pr                  ExtPgm( 'QUIGETV' )
     D  AppHdl                        8a   Const
     D  VarBuf                    32767a          Options( *VarSize )
     D  VarBufLen                    10i 0 Const
     D  VarRcdNam                    10a   Const
     D  Error                     32767a          Options( *VarSize )
     **-- Add list entry:
     D AddLstEnt       Pr                  ExtPgm( 'QUIADDLE' )
     D  AppHdl                        8a   Const
     D  VarBuf                    32767a   Const  Options( *VarSize )
     D  VarBufLen                    10i 0 Const
     D  VarRcdNam                    10a   Const
     D  LstNam                       10a   Const
     D  EntLocOpt                     4a   Const
     D  LstEntHdl                     4a
     D  Error                     32767a          Options( *VarSize )
     **-- Get list entry:
     D GetLstEnt       Pr                  ExtPgm( 'QUIGETLE' )
     D  AppHdl                        8a   Const
     D  VarBuf                    32767a   Const  Options( *VarSize )
     D  VarBufLen                    10i 0 Const
     D  VarRcdNam                    10a   Const
     D  LstNam                       10a   Const
     D  PosOpt                        4a   Const
     D  CpyOpt                        1a   Const
     D  SltCri                       20a   Const
     D  SltHdl                        4a   Const
     D  ExtOpt                        1a   Const
     D  LstEntHdl                     4a
     D  Error                     32767a          Options( *VarSize )
     **-- Retrieve list attributes:
     D RtvLstAtr       Pr                  ExtPgm( 'QUIRTVLA' )
     D  AppHdl                        8a   Const
     D  LstNam                       10a   Const
     D  AtrRcv                    32767a          Options( *VarSize )
     D  AtrRcvLen                    10i 0 Const
     D  Error                     32767a          Options( *VarSize )
     **-- Set list attributes:
     D SetLstAtr       Pr                  ExtPgm( 'QUISETLA' )
     D  AppHdl                        8a   Const
     D  LstNam                       10a   Const
     D  LstCon                        4a   Const
     D  ExtPgmVar                    10a   Const
     D  DspPos                        4a   Const
     D  AlwTrim                       1a   Const
     D  Error                     32767a          Options( *VarSize )
     **-- Delete list:
     D DltLst          Pr                  ExtPgm( 'QUIDLTL' )
     D  AppHdl                        8a   Const
     D  LstNam                       10a   Const
     D  Error                     32767a          Options( *VarSize )
     **-- Close application:
     D CloApp          Pr                  ExtPgm( 'QUICLOA' )
     D  AppHdl                        8a   Const
     D  CloOpt                        1a   Const
     D  Error                     32767a          Options( *VarSize )

     **-- Get product entries count:
     D GetPrdCnt       Pr            10i 0
     D  PxWlcGrp                     10a   Const
     **-- Send escape message:
     D SndEscMsg       Pr            10i 0
     D  PxMsgId                       7a   Const
     D  PxMsgF                       10a   Const
     D  PxMsgDta                    512a   Const  Varying

     **-- Entry parameters:
     D CBX3021         Pr
     D  PxWlcGrp                     10a
     **
     D CBX3021         Pi
     D  PxWlcGrp                     10a

      /Free

        SpcAutLst( 1 ) = '*JOBCTL';

        ChkSpcAut( AutFlg
                 : PgmSts.UsrPrf
                 : SpcAutLst
                 : 1
                 : ADP_PRV_INVLVL
                 : ERRC0100
                 );

        If  ERRC0100.BytAvl > *Zero  Or  AutFlg = 'N';
          SndEscMsg( 'CPFB0CE': 'QCPFMSG': '*JOBCTL' );
        EndIf;

        OpnDspApp( UIM.AppHdl
                 : PNLGRP_Q
                 : SCP_AUT_RCL
                 : PRM_IFC_0
                 : HLP_WDW
                 : ERRC0100
                 );

        If  ERRC0100.BytAvl > *Zero;
          ExSr  EscApiErr;
        EndIf;

        PutDlgVar( UIM.AppHdl: ExpRcd: %Size( ExpRcd ): 'EXPRCD': ERRC0100 );

        ExSr  BldHdrRcd;
        ExSr  BldWlgLst;

        DoU  UIM.FncRqs = FNC_EXIT  Or  UIM.FncRqs = FNC_CNL;

          DspPnl( UIM.AppHdl: UIM.FncRqs: PNLGRP: RDS_OPT_INZ: ERRC0100 );

          Select;
          When  ERRC0100.BytAvl > *Zero;
            ExSr  EscApiErr;

          When  UIM.FncRqs = RTN_ENTER;
            Leave;
          EndSl;

          GetDlgVar( UIM.AppHdl: HdrRcd: %Size( HdrRcd ): 'HDRRCD': ERRC0100 );

          If  UIM.FncRqs = KEY_F05  And  UIM.EntLocOpt = 'NEXT';
            ExSr  GetLstPos;
            ExSr  DltWlgLst;
          EndIf;

          If  UIM.FncRqs = KEY_F05;
            ExSr  BldWlgLst;
            ExSr  SetLstPos;
          EndIf;

          ExSr  BldHdrRcd;
        EndDo;

        CloApp( UIM.AppHdl: CLO_NORM: ERRC0100 );

        *InLr = *On;
        Return;


        BegSr  BldWlgLst;

          UIM.EntLocOpt = 'FRST';

          WLCQ0300.WlcGrp = PxWlcGrp;

          RtvWlgInf( WLCR0200
                   : %Size( WLCR0200 )
                   : 'WLCR0200'
                   : WLCQ0300
                   : 'WLCQ0300'
                   : ERRC0100
                   );

          If  ERRC0100.BytAvl > *Zero;
            ExSr  EscApiErr;

          Else;
            ExSr  PrcWlcLst;
          EndIf;

          SetLstAtr( UIM.AppHdl
                   : 'DTLLST'
                   : LIST_COMP
                   : EXIT_SAME
                   : POS_TOP
                   : TRIM_SAME
                   : ERRC0100
                   );

        EndSr;

        BegSr  PrcWlcLst;

          pWlgLst = %Addr( WLCR0200 ) + WLCR0200.OfsWlgLst;
          For  Idx = 1  to  WLCR0200.NbrWlgLstE;

            LstEnt.Option = *Zero;

            LstEnt.WlcGrp    = WlgLst.WlcGrp;
            LstEnt.MaxPrcLmt = WlgLst.MaxPrcLmt;
            LstEnt.NbrPrdEnt = GetPrdCnt( WlgLst.WlcGrp );

            AddLstEnt( UIM.AppHdl
                     : LstEnt
                     : %Size( LstEnt )
                     : 'DTLRCD'
                     : 'DTLLST'
                     : UIM.EntLocOpt
                     : UIM.LstHdl
                     : ERRC0100
                     );

            UIM.EntLocOpt = 'NEXT';

            If  Idx < WLCR0200.NbrWlgLstE;
              pWlgLst += WLCR0200.LenWlgLstE;
            EndIf;
          EndFor;

        EndSr;

        BegSr  GetLstPos;

          RtvLstAtr( UIM.AppHdl: 'DTLLST': LstAtr: %Size( LstAtr ): ERRC0100 );

          If  LstAtr.DspPos <> 'TOP';

            GetLstEnt( UIM.AppHdl
                     : LstEnt
                     : %Size( LstEnt )
                     : 'DTLRCD'
                     : 'DTLLST'
                     : 'HNDL'
                     : 'Y'
                     : *Blanks
                     : LstAtr.DspPos
                     : 'N'
                     : UIM.EntHdl
                     : ERRC0100
                     );

            LstEntPos = LstEnt;
          EndIf;

        EndSr;

        BegSr  SetLstPos;

          If  LstAtr.DspPos <> 'TOP';

            LstEnt = LstEntPos;

            PutDlgVar( UIM.AppHdl
                     : LstEnt
                     : %Size( LstEnt )
                     : 'DTLRCD'
                     : ERRC0100
                     );

            GetLstEnt( UIM.AppHdl
                     : LstEnt
                     : %Size( LstEnt )
                     : '*NONE'
                     : 'DTLLST'
                     : 'FSLT'
                     : 'N'
                     : 'EQ        WLCGRP'
                     : LstAtr.DspPos
                     : 'N'
                     : UIM.EntHdl
                     : ERRC0100
                     );

            If  ERRC0100.BytAvl = *Zero;

              SetLstAtr( UIM.AppHdl
                       : 'DTLLST'
                       : LIST_SAME
                       : EXIT_SAME
                       : UIM.EntHdl
                       : TRIM_SAME
                       : ERRC0100
                       );

            EndIf;
          EndIf;

        EndSr;

        BegSr  BldHdrRcd;

          SysDts = %Timestamp();

          HdrRcd.SysDat = %Char( %Date( SysDts ): *CYMD0 );
          HdrRcd.SysTim = %Char( %Time( SysDts ): *HMS0 );
          HdrRcd.TimZon = '*SYS';

          HdrRcd.WlgRqs = PxWlcGrp;

          PutDlgVar( UIM.AppHdl: HdrRcd: %Size( HdrRcd ): 'HDRRCD': ERRC0100 );

        EndSr;

        BegSr  DltWlgLst;

          DltLst( UIM.AppHdl: 'DTLLST': ERRC0100 );

        EndSr;

        BegSr  EscApiErr;

          If  ERRC0100.BytAvl < OFS_MSGDTA;
            ERRC0100.BytAvl = OFS_MSGDTA;
          EndIf;

          SndEscMsg( ERRC0100.MsgId
                   : 'QCPFMSG'
                   : %Subst( ERRC0100.MsgDta: 1: ERRC0100.BytAvl - OFS_MSGDTA )
                   );

        EndSr;

      /End-Free

     **-- Get product entries count:
     P GetPrdCnt       B
     D                 Pi            10i 0
     D  PxWlcGrp                     10a   Const

      /Free

        WLCQ0300.WlcGrp = PxWlcGrp;

        RtvWlgInf( WLCR0100
                 : %Size( WLCR0100 )
                 : 'WLCR0100'
                 : WLCQ0300
                 : 'WLCQ0300'
                 : ERRC0100
                 );

        If  ERRC0100.BytAvl > *Zero;
          Return  *Zero;

        Else;
          Return  WLCR0100.NbrWlgLstE;
        EndIf;

      /End-Free

     P GetPrdCnt       E
     **-- Send escape message:
     P SndEscMsg       B
     D                 Pi            10i 0
     D  PxMsgId                       7a   Const
     D  PxMsgF                       10a   Const
     D  PxMsgDta                    512a   Const  Varying
     **
     D MsgKey          s              4a

      /Free

        SndPgmMsg( PxMsgId
                 : PxMsgF + '*LIBL'
                 : PxMsgDta
                 : %Len( PxMsgDta )
                 : '*ESCAPE'
                 : '*PGMBDY'
                 : 1
                 : MsgKey
                 : ERRC0100
                 );

        If  ERRC0100.BytAvl > *Zero;
          Return  -1;

        Else;
          Return   0;
        EndIf;

      /End-Free

     P SndEscMsg       E
