/*********************************************************************/
/*  (c)  Noordeloos Informatica Support, 2022                        */
/*-------------------------------------------------------------------*/
/*  Program ID          -    LOCKMSG                                 */
/*  Description         -    Produce a message on a locked record    */
/*  Author              -    Rob Noordeloos                          */
/*  Date generated      -    August 15th, 2022                       */
/*-------------------------------------------------------------------*/
/*  Validate Parameter  :    usrprf(*owner)                          */
/*                                                                   */
/*********************************************************************/

             pgm        (&status)

/*-------------------------------------------------------------------*/
/*  Declarations                                                     */
/*-------------------------------------------------------------------*/

             dcl        &status   *char   len(333)
             dcl        &code     *char   len(5)
             dcl        &msg      *char   len(80)
             dcl        &jobtype  *char   len(1)
             dcl        &job      *char   len(10)
             dcl        &job2     *char   len(10)
             dcl        &user     *char   len(10)
             dcl        &nbr      *char   len(6)
             dcl        &sepcnt   *dec    len(3 0)
             dcl        &x        *dec    len(3 0)
             dcl        &y        *dec    len(3 0)
             dcl        &msgrpy   *char   len(1)
             dcl        &msgkey   *char   len(4)

/*-------------------------------------------------------------------*/
/* If it is not a record lock condition, send back an escape message */
/*-------------------------------------------------------------------*/

             chgvar     &code %sst(&status 209 5)
             if         (&code *ne '01218')                          +
                        msgf(qcpfmsg)                                +
                        msgtype(*escape)                             +
                        msgdta('Input operation failed for a reason  +
                        other than a locked record')

             chgvar     &msg %sst(&status 91 80)
             rtvjoba    job(&job)                                    +
                        user(&user)                                  +
                        nbr(&nbr)                                    +
                        type(&jobtype)

/*-------------------------------------------------------------------*/
/* If it is an interactive job, send to end user with a 3 way option.*/
/*-------------------------------------------------------------------*/

             if         (&jobtype *eq '1') do
             sndusrmsg  msgid(TAA1001)                               +
                        msgf(taatool/taamsgf)                        +
                        msgtype(*info)
             sndusrmsg  msg(&msg)                                    +
                        values(r j s)                                +
                        msgrpy(&msgrpy)

/*-------------------------------------------------------------------*/
/* Check user input                                                  */
/*-------------------------------------------------------------------*/

             if         (&msgrpy *eq 'r')                            +
                        return

             if         (&msgrpy *eq 'j') do

/*-------------------------------------------------------------------*/
/* Send to job with lock                                             */
/* Extract the job name from the msg text                            */
/*-------------------------------------------------------------------*/

             chgvar     &y 22
/*-------------------------------------------------------------------*/
/* Start pos in &msg of job name X/X/X                               */
/* Loop until 2nd slash (/) is found                                 */
/*-------------------------------------------------------------------*/

 seploop:    chgvar     &y (&y + 1)
             if         (%sst(&msg &y 1) *ne '/')                    +
                        goto seploop

/*-------------------------------------------------------------------*/
/* Slash found                                                       */
/*-------------------------------------------------------------------*/

             chgvar     &sepcnt (&sepcnt + 1)
             if         (&sepcnt *ne 2)                              +
                        goto seploop

/*-------------------------------------------------------------------*/
/* Extract the name (assume 10 char)                                 */
/*-------------------------------------------------------------------*/

namloop:    chgvar     &y (&y + 1)
            chgvar     &x (&x + 1)

*--------------------------------------------------------------------*/
* Message text ends with a period                                    */
*--------------------------------------------------------------------*/

            if         (%sst(&msg &y 1) *ne '.') do
            chgvar     %sst(&job2 &x 1) %sst(&msg &y 1)
            if         (&x *ne 10)                                  +
                       goto namloop
            enddo

*--------------------------------------------------------------------*/
* Send message to user who has the lock                              */
*--------------------------------------------------------------------*/

            sndbrkmsg  msg('From ' *cat &user *tcat '- You have a +
                         record locked that I need. Please end your +
                          transaction.')                             +
                        tomsgq(&job2)

/*-------------------------------------------------------------------*/
/* Message failed                                                    */
/*-------------------------------------------------------------------*/

             monmsg     msgid(cpf2469) exec(do)
             sndusrmsg  msg('Message could not be sent.Job may be a +
                          batch job. Press Enter.')                 +
                        msgtype(*info)
             enddo
             enddo

/*-------------------------------------------------------------------*/
/* Send to System operator                                           */
/*-------------------------------------------------------------------*/

             if         (&msgrpy *eq 's') do
             sndusrmsg  msg(&msg)                                    +
                        msgtype(*info)                               +
                        tomsgq(qsysopr)
             sndusrmsg  msg('Interactive job ' *cat &nbr *tcat '/' +
                          *cat &user *tcat '/' *cat &job *tcat ' at +
                          rcd lock wait. See previous message.')     +
                        msgtype(*info)                               +
                        tomsgq(qsysopr)
             enddo
             enddo

/*-------------------------------------------------------------------*/
/* If batch is locked, send msg to System Operator                   */
/*-------------------------------------------------------------------*/

             if         (&jobtype *ne '1') do
             sndusrmsg  msg(&msg)                                    +
                        msgtype(*info)                               +
                        tomsgq(qsysopr)
             sndpgmmsg  msg('Batch job ' *cat &nbr *tcat '/' *cat +
                          &user *tcat '/' *cat &job *tcat ' at rcd +
                          lock wait. See prev. Reply(G).')           +
                        tomsgq(qsysopr)                              +
                        msgtype(*inq)                                +
                        keyvar(&msgkey)

/*-------------------------------------------------------------------*/
/* Wait up to 120 seconds for reply                                  */
/*-------------------------------------------------------------------*/

             rcvmsg     msgtype(*rpy)                                +
                        msgkey(&msgkey)                              +
                        wait(120)
             enddo

/*-------------------------------------------------------------------*/
/* End of Program                                                    */
/*-------------------------------------------------------------------*/

             endpgm