project BLF > class BUserRole > method ApiSynchronise

Description

Creates a membership for user 'mfg' in role 'SuperUser'.


Parameters


olUpdatesDoneoutputlogical
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


BLF
method BSystem.ApiSynchronise2


program code (program9/buserrole.p)

/* ========================================== */ 
/* Clear Data                                 */
/* ========================================== */ 
<M-1 run ClearData (output oiReturnStatus (oiReturnStatus)) in BUserRole>
if oiReturnStatus < 0
then return.

/*Find SuperUser role*/
<Q-2 run RolePrim (all)  (Read)  (NoCache)  (input ?, (RoleID) 
                   input 'SuperUser':U, (RoleName)
                   output dataset tqRolePrim) in BRole >
find tqRolePrim where 
     tqRolePrim.tcRoleName = 'SuperUser':U
     no-lock no-error.
if Not available tqRolePrim
Then Do:
    Assign vcMessage      = trim(substitute(#T-3'Synchronization of roles failed: unable to retrieve the role (&1).':255(6722)T-3#,trim(tqRolePrim.tcRoleName)))
           oiReturnStatus = -1.
    <M-4 run SetMessage (input  vcMessage (icMessage), 
                     input  '':U (icArguments), 
                     input  '':U (icFieldName), 
                     input  '':U (icFieldValue), 
                     input  'S':U (icType), 
                     input  3 (iiSeverity), 
                     input  '':U (icRowid), 
                     input  'BLF-427':U:U (icFcMsgNumber), 
                     input  '':U (icFcExplanation), 
                     input  '':U (icFcIdentification), 
                     input  '':U (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in BUserRole>
    Return.
End.


/* ===================================================================================== */
/* Link this role to following users: mfg */
/* ===================================================================================== */
<Q-16 run UsrDomainPrim
   (Start) in BUser >
<Q-19 run UsrRolePrim
   (Start) in BUserRole >
assign vcUsrNames = 'mfg':U.
do viUsrCnt = num-entries(vcUsrNames, chr(2)) to 1 by -1:
    assign vcUsrName = entry(viUsrCnt, vcUsrNames, chr(2)).
    /* Check if the user exists */
    <Q-5 run UserPrim (all) (Read) (NoCache)
       (input ?, (UsrId)
        input vcUsrName, (UsrLogin)
        output dataset tqUserPrim) in BUser >    
    find tqUserPrim where
         tqUserPrim.tcUsrLogin = vcUsrName
         no-lock no-error.
    if available tqUserPrim
    then do:
        <Q-18 run UsrRolePrim (all) (Read) (NoCache)
           (input 0, (UsrRoleID)
            input tqRolePrim.tcRoleName, (RoleName)
            input tqUserPrim.tcUsrLogin, (UsrLogin)
            output dataset tqUsrRolePrim) in BUserRole >
        Find tqUsrRolePrim Where
             tqUsrRolePrim.tcRoleName = tqRolePrim.tcRoleName And
             tqUsrRolePrim.tcUsrLogin  = tqUserPrim.tcUsrLogin No-error.
        If Available tqUsrRolePrim
        Then Do:
            <M-21 run DataLoad
               (input  '' (icRowids), 
                input  tqUsrRolePrim.tiUsrRole_ID (icPkeys), 
                input  '' (icObjectIds), 
                input  '' (icFreeform), 
                input  True (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BUserRole>
            if (viFcReturnSuper < 0 And viFcReturnSuper <> -4) or 
               (viFcReturnSuper > 0 and oiReturnStatus = 0)
            then assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0
            then return.

            find tUsrRole where
                 tUsrRole.UsrRole_ID = tqUsrRolePrim.tiUsrRole_ID
                 no-error.
        End.
        /* Check if user is already linked to this role, else create the link */
        
        if not Available tqUsrRolePrim Or 
           not available tUsrRole            
        then do:
            <M-6 run AddDetailLine (input  'UsrRole':U (icTable), 
                        input  ? (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BUserRole>            
            if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
            then assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0
            then return.

            assign tUsrRole.tcUsrLogin  = tqUserPrim.tcUsrLogin  
                   tUsrRole.tcRoleName  = tqRolePrim.tcRoleName
                   olUpdatesDone        = true.
        end. /* if not available tUsrRole */                    

        /* if this user does not have a default role, then make this role the default */
        /* Check for DefaultRole */
        <Q-7 assign vlFcQueryRecordsAvailable = UserRoleByUserRole (NoCache)
           (input ?, (UsrID)
            input ?, (RoleID)
            input True, (UsrRoleIsDefaultRole)
            input ?, (RoleName)
            input tUsrRole.tcUsrLogin, (UsrLogin)
            input ?, (UsrName)) in BUserRole >             
        
        if vlFcQueryRecordsAvailable = false and
           not can-find(first bUsrRole where
                              bUsrRole.tcUsrLogin           = tUsrRole.tcUsrLogin and
                              bUsrRole.tcRoleName           = tUsrRole.tcRoleName And
                              bUsrRole.UsrRoleIsDefaultRole = true                and
                              bUsrRole.tc_Status           <> 'D':U)
        then assign tUsrRole.UsrRoleIsDefaultRole = true
                    tUsrRole.tc_Status            = (if tUsrRole.tc_Status = '':U then 'C':U Else tUsrRole.tc_Status)
                    olUpdatesDone                 = true.  
        
        /*create usrroledomain records to all domains linked to the user*/   
        <Q-14 run UsrDomainPrim (all) (Read) (NoCache)
           (input vcUsrName, (UsrLogin)
            input ?, (DomainCode)
            input ?, (UsrDomainId)
            output dataset tqUsrDomainPrim) in BUser >
        For Each tqUsrDomainPrim:

            If Not Can-find (First tUsrRoleDomain Where
                                   tUsrRoleDomain.tc_ParentRowid = tUsrRole.tc_Rowid And
                                   tUsrRoleDomain.tcDomainCode   = tqUsrDomainPrim.tcDomainCode And
                                   tUsrRoleDomain.tc_Status      <> 'D':U)
            Then Do:
                <M-17 run AddDetailLine
                   (input  'UsrRoleDomain':U (icTable), 
                    input  tUsrRole.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BUserRole>   
                if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
                then assign oiReturnStatus = viFcReturnSuper.
                if oiReturnStatus < 0
                then Return.
                Assign tUsrRoleDomain.tcDomainCode = tqUsrDomainPrim.tcDomainCode                      
                       olUpdatesDone                = True.
            End.
        End. /*For Each tqUsrDomainPrim:*/

        /*create usrrolecompanies for all companies linked the the user*/
        <Q-22 run UsrCompanyPrim (all) (Read) (NoCache)
           (input ?, (CompanyId)
            input ?, (UsrCompanyID)
            input tUsrRole.tcUsrLogin, (UsrLogin)
            input ?, (CompanyCode)
            output dataset tqUsrCompanyPrim) in BUser >
        for each tqUsrCompanyPrim:
            if not can-find (first tUsrRoleCompany where
                                   tUsrRoleCompany.tc_ParentRowid = tUsrRole.tc_Rowid and
                                   tUsrRoleCompany.tcCompanyCode   = tqUsrCompanyPrim.tcCompanyCode and
                                   tUsrRoleCompany.tc_Status      <> 'D':U)
            then do:
                <M-23 run AddDetailLine
                   (input  'UsrRoleCompany':U (icTable), 
                    input  tUsrRole.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BUserRole>   
                if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
                then assign oiReturnStatus = viFcReturnSuper.
                if oiReturnStatus < 0
                then return.
                assign tUsrRoleCompany.tcCompanyCode = tqUsrCompanyPrim.tcCompanyCode                      
                       olUpdatesDone                = True.
            End.
        end. /*for each tqUsrCompanyPrim*/
                                                                                         
    end. /* if available tqUserPrim */
end. /* do viUsrCnt = num-entries(vcUsrNames, chr(2)) to 1 by -1: */
<Q-15 run UsrDomainPrim
   (Stop) in BUser >
<Q-20 run UsrRolePrim
   (Stop) in BUserRole >

/*ApiSynchroniseCustom*/
<M-11 run ApiSynchroniseCustom (output vlUpdatesDone (olUpdatesDone), 
                                output viFcReturnSuper (oiReturnStatus)) in BUserRole>
if vifcreturnsuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
if oiReturnStatus < 0 then return.

/* Set output indication */

if can-find (first tUsrRole where tUsrRole.tc_Status <> "":U) or
   vlUpdatesDone
then assign olUpdatesDone = true.

/* ========================================== */ 
/* Validate BC                                */
/* ========================================== */ 
<M-8 run ValidateBC (output viFcReturnSuper (oiReturnStatus)) in BUserRole>
if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
then assign oiReturnStatus = viFcReturnSuper.
if oiReturnStatus < 0
then return.

/* ========================================== */                                                  
/* AdditionalUpdates                          */
/* ========================================== */ 
<M-9 run AdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BUserRole>
if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
then assign oiReturnStatus = viFcReturnSuper.
if oiReturnStatus < 0
then return.

/* ========================================== */                                                  
/* DataSave                                   */
/* ========================================== */
<M-10 run DataSave (output viFcReturnSuper (oiReturnStatus)) in BUserRole>
if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
then assign oiReturnStatus = viFcReturnSuper.
if oiReturnStatus < 0
then return.


Sample code: how to call this method through RPCRequestService (QXtend Inbound)

define temp-table ttContext no-undo
    field propertyQualifier as character
    field propertyName as character
    field propertyValue as character
    index entityContext is primary unique
        propertyQualifier
        propertyName
    index propertyQualifier
        propertyQualifier.

define dataset dsContext for ttContext.

define variable vhContextDS as handle no-undo.
define variable vhExceptionDS as handle no-undo.
define variable vhServer as handle no-undo.
define variable vhInputDS as handle no-undo.
define variable vhInputOutputDS as handle no-undo.
define variable vhOutputDS as handle no-undo.
define variable vhParameter as handle no-undo.

/* Create context */
create ttContext.
assign ttContext.propertyName = "programName"
       ttContext.propertyValue = "BUserRole".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "ApiSynchronise".
create ttContext.
assign ttContext.propertyName = "applicationId"
       ttContext.propertyValue = "fin".
create ttContext.
assign ttContext.propertyName = "entity"
       ttContext.propertyValue = "1000".
create ttContext.
assign ttContext.propertyName = "userName"
       ttContext.propertyValue = "mfg".
create ttContext.
assign ttContext.propertyName = "password"
       ttContext.propertyValue = "".

/* Connect the AppServer */
create server vhServer.
vhServer:connect("-URL <appserver-url>").

if not vhServer:connected()
then do:
    message "Could not connect AppServer" view-as alert-box error title "Error".
    return.
end.

/* Run */
assign vhContextDS = dataset dsContext:handle.

run program/rpcrequestservice.p on vhServer
    (input-output dataset-handle vhContextDS by-reference,
           output dataset-handle vhExceptionDS,
     input        dataset-handle vhInputDS by-reference,
     input-output dataset-handle vhInputOutputDS by-reference,
           output dataset-handle vhOutputDS).

/* Handle output however you want, in this example, we dump it to xml */
if valid-handle(vhExceptionDS)
then vhExceptionDS:write-xml("file", "Exceptions.xml", true).

if valid-handle(vhOutputDS)
then vhOutputDS:write-xml("file", "Output.xml", true).

/* Cleanup */
vhServer:disconnect().
assign vhServer = ?.

if valid-handle(vhInputDS)
then delete object vhInputDS.

if valid-handle(vhOutputDS)
then delete object vhOutputDS.

if valid-handle(vhExceptionDS)
then delete object vhExceptionDS.