project BLF > class BUser > method ApiSynchronise

Description

Creates a system user 'mfg' with access to all known domains and entities.


Parameters


olUpdatesDoneoutputlogicalUpdates were done during this run of ApiSynchronise
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


BLF
method BSystem.ApiSynchronise2


program code (program9/buser.p)

/* ========================================== */ 
/* Clear Data                                 */
/* ========================================== */ 
<M-1 run ClearData (output oiReturnStatus (oiReturnStatus)) in BUser>

if oiReturnStatus <> 0
then return.

/* ======================== */
/* Get the default language */
/* ======================== */
<Q-2 run LanguagePrim (all) (Read) (NoCache)
   (input 'US':U, (LngCode)
    input ?, (LngID)
    output dataset tqLanguagePrim) in BLanguage >
find first tqLanguagePrim where
           tqLanguagePrim.tcLngCode = 'US':U
           no-lock no-error.
if not available tqLanguagePrim
then do:
    <Q-3 run LanguagePrim (all)  (Read)  (NoCache)  (input ?, (LngCode) 
                       input ?, (LngID)
                       output dataset tqLanguagePrim) in BLanguage >
    find first tqLanguagePrim
               no-lock no-error.
end.

if available tqLanguagePrim
then assign vcLanguageCode = tqLanguagePrim.tcLngCode.
else assign vcLanguageCode = '':U.

/* ========================================== */ 
/* Check if Users exists                      */
/* Create users if they do not exist          */
/* Load the users if they do exist            */
/* ========================================== */
assign vcUsrNames     = 'mfg':U .
USERPASSWORDBLOCK: DO :
do viUsrCnt = num-entries(vcUsrNames, chr(2)) to 1 by -1:
    assign vcUsrName     = entry(viUsrCnt, vcUsrNames, chr(2))
           viUsrId       = 0.
    /* Check if the user (login) exists */
    <Q-8 run UserByUsrLogin (all)  (Read)  (NoCache)  (input vcUsrName, (UsrLogin)
                             output dataset tqUserByUsrLogin) in BUser >
    find first tqUserByUsrLogin no-error.
    if available tqUserByUsrLogin
    then viUsrId = tqUserByUsrLogin.tiUsr_ID.
    else do:
    /* Check if the user (initials) exists */
    <Q-9 run UserByUsrInitials (all)  (Read)  (NoCache)  (input vcUsrName, (Initials)
                            output dataset tqUserByUsrInitials) in BUser >
    find first tqUserByUsrInitials no-error.
    if available tqUserByUsrInitials
    then viUsrId = tqUserByUsrInitials.tiUsr_ID.
    else do:
    /* Check if the user (name) exists */
    <Q-10 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 viUsrId = tqUserPrim.tiUsr_ID.
    end.
    end.

    if viUsrId <> 0
    then do:
        /* Load Usr */
        <M-11 run DataLoad (input  ? (icRowids), 
                    input  viUsrId (icPkeys), 
                    input  ? (icObjectIds), 
                    input  '' (icFreeform), 
                    input  true (ilKeepPrevious), 
                    output viFcReturnSuper (oiReturnStatus)) in BUser>
        find tUsr where
             tUsr.Usr_ID = viUsrId
             no-error.
        if tUsr.UsrIsActive        = false          or
           tUsr.tcLngCode         <> vcLanguageCode
        then assign tUsr.UsrIsActive               = true
                    tUsr.tcLngCode                 = vcLanguageCode
                    tUsr.tc_Status                 = 'C':U
                    olUpdatesDone                  = true.
    end.
    /*The user should already be available*/
    else do:
        /* Create Usr */
        <M-12 run AddDetailLine (input  'Usr':U (icTable), 
                         input  ? (icParentRowid), 
                         output viFcReturnSuper (oiReturnStatus)) in BUser>
        if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
        then assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then leave USERPASSWORDBLOCK.

        assign tUsr.UsrLogin                  = vcUsrName
               tUsr.UsrInitials               = vcUsrName
               tUsr.UsrName                   = vcUsrName
               tUsr.UsrIsActive               = true
               tUsr.tcLngCode                 = vcLanguageCode
               olUpdatesDone                  = true.
    end. /* if viUsrId <> 0 */       
    
    /* Create a link to the system domain */
    <Q-30 run DomainsByDomainType (all) (Read) (NoCache)
       (input ?, (DomainID)
        input {&DOMAINSDOMAINTYPE-SYSTEM}, (DomainType)
        output dataset tqDomainsByDomainType) in BDomain > 
        
    find first tqDomainsByDomainType no-error.
    if available tqDomainsByDomainType
    then do:
        find first tUsrDomain Where
                   tUsrDomain.tc_ParentRowid = tUsr.tc_Rowid And
                   tUsrDomain.tcDomainCode   = tqDomainsByDomainType.tcDomainCode And
                   tUsrDomain.tc_Status      <> 'D':U no-error.
        if not available tUsrDomain
        Then Do:
        
            <M-27 run AddDetailLine
               (input  'UsrDomain':U (icTable), 
                input  tUsr.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BUser>
                
            if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
            then assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0
            then leave USERPASSWORDBLOCK.
            
            Assign tUsrDomain.tcDomainCode = tqDomainsByDomainType.tcDomainCode
                   tUsrDomain.Domain_ID    = tqDomainsByDomainType.tiDomain_ID                                                                       
                   olUpdatesDone           = True.
        End.
        
        if not can-find(first bUsrDomain where 
                              bUsrDomain.tc_ParentRowid     = tUsr.tc_Rowid and                          
                              bUsrDomain.UsrDomainIsDefault = true)
        then do :               
            Assign tUsrDomain.UsrDomainIsDefault = true
                   tUsrDomain.tc_Status          = if tUsrDomain.tc_Status = 'N' then tUsrDomain.tc_Status else 'C':U
                   olUpdatesDone = True.
        end.
                           
        /*link companies of system domain*/
        <Q-31 run DomainsForCompanies (all) (Read) (NoCache)
           (input tqDomainsByDomainType.tiDomain_ID, (DomainId)
            input ?, (DomainCode)
            input ?, (CompanyId)
            output dataset tqDomainsForCompanies) in BDomain >
    
        for each tqDomainsForCompanies on error undo, throw:
    
            find first tUsrCompany where
                       tUsrCompany.tc_ParentRowid = tUsr.tc_Rowid and
                       tUsrCompany.Company_ID = tqDomainsForCompanies.tiCompany_ID no-error.
            if not available tUsrCompany
            then do:
                <M-29 run AddDetailLine
                   (input  'UsrCompany':U (icTable), 
                    input  tUsr.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BUser>
                if viFcReturnSuper < 0 or (viFcReturnSuper > 0 and oiReturnStatus = 0)
                then assign oiReturnStatus = viFcReturnSuper.
                if oiReturnStatus < 0

                then leave USERPASSWORDBLOCK.
                assign tUsrCompany.Company_ID = tqDomainsForCompanies.tiCompany_ID
                       tUsrCompany.tcCompanyCode = tqDomainsForCompanies.tcCompanyCode
                       olUpdatesDone = true.
            end.
        end.
    End. /*if available tqDomainsByDomainType*/
    
end. /* do viUsrCnt = num-entries(vcUsrNames, chr(2)) to 1 by -1: */
END. /* USERPASSWORDBLOCK */

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

/* Set output indication */
if can-find (first tUsr where tUsr.tc_Status <> "":U) or
   vlUpdatesDone
then assign olUpdatesDone = true.

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

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

/* ========================================== */                                                  
/* DataSave                                   */
/* ========================================== */
<M-22 run DataSave (output viFcReturnSuper (oiReturnStatus)) in BUser>
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 = "BUser".
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.