project BLF > class BUser > method ApiSaveUserDomainEntityAccess

Description

Method to save user domain/entitiy access


Parameters


tUserDomainEntityAccessinputtemp-table
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program3/buser.p)

/*get the users to do a dataload for*/
assign vcUserLoginList = ''.
for each tUserDomainEntityAccess where
         tUserDomainEntityAccess.tlChanged = true on error undo, throw:
    if lookup(tUserDomainEntityAccess.tcUsrLogin, vcUserLoginList) = 0
    then assign vcUserLoginList = (if vcUserLoginList = ''
                                   then tUserDomainEntityAccess.tcUsrLogin
                                   else vcUserLoginList + ',' + tUserDomainEntityAccess.tcUsrLogin).
end. /*for each tUserDomainEntityAccess:*/

if vcUserLoginList = ''
then do:
    assign oiReturnStatus = 0.
    return.
end.

/*create freeform to load needed users*/
assign vcFreeForm = "for each Usr where can-do('":U + replace(vcUserLoginList,"'","''") + "',Usr.UsrLogin)":U.

<M-1 run DataLoad
   (input  '' (icRowids), 
    input  '' (icPkeys), 
    input  '' (icObjectIds), 
    input  vcFreeForm (icFreeform), 
    input  false (ilKeepPrevious), 
    output viFcReturnSuper (oiReturnStatus)) in BUser>
if viFcReturnSuper <> 0
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

for each tUsr on error undo, throw:

    /*tUserDomainEntityAccess.tlLink = true*/
    for each tUserDomainEntityAccess where 
             tUserDomainEntityAccess.tcUsrLogin = tUsr.UsrLogin and
             tUserDomainEntityAccess.tlChanged = true on error undo, throw:

        /* tUsrCompany records */
        find tUsrCompany where
             tUsrCompany.tc_ParentRowid = tUsr.tc_Rowid and
             tUsrCompany.tcCompanyCode = tUserDomainEntityAccess.tcCompanyCode no-error.
        if not available tUsrCompany and
           tUserDomainEntityAccess.tlLink = true
        then do:
            <M-3 run AddDetailLine
               (input  'UsrCompany':U (icTable), 
                input  tUsr.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BUser>
            if viFcReturnSuper <> 0
            then assign oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then return.
            assign tUsrCompany.tcCompanyCode = tUserDomainEntityAccess.tcCompanyCode.
        end. /*if not available tUsrCompany*/
        else do:
            if tUserDomainEntityAccess.tlLink = true /*available tUsrCompany and tlLink = true*/
            then do:
                assign tUsrCompany.tc_Status = (if tUsrCompany.tc_Status = '':U or
                                                   tUsrCompany.tc_Status = 'D':U
                                                then 'C':U
                                                else tUsrCompany.tc_Status).
            end.
            else do: /*available and link false or not available and link false*/
                if available tUsrCompany
                then do:                    
                    /*if the status has not changed yet set status to D else keep the status*/
                    /*on dataload, all statuses are '', so not changed*/
                    /*if we need a usrcompany record we set the status to C*/
                    /*we make sure this status is not changed to D after is was changed to C or for a new usrcompany record (status N)*/
                    assign tUsrCompany.tc_Status = (if tUsrCompany.tc_Status = '':U
                                                    then 'D':U
                                                    else tUsrCompany.tc_Status).
                end.
            end.
        end.   

        /* tUsrDomain records */
        /*check which domain to set as default*/
        /*create a usrdomain record if needed*/
        find tUsrDomain where
             tUsrDomain.tc_ParentRowid = tUsr.tc_Rowid and
             tUsrDomain.tcDomainCode = tUserDomainEntityAccess.tcDomainCode no-error.
        if not available tUsrDomain and
           tUserDomainEntityAccess.tlLink = true
        then do:
            <M-7 run AddDetailLine
               (input  'UsrDomain':U (icTable), 
                input  tUsr.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BUser>
            if viFcReturnSuper <> 0
            then assign oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then return.

            assign tUsrDomain.tcDomainCode = tUserDomainEntityAccess.tcDomainCode
                   tUsrDomain.UsrDomainIsDefault = tUserDomainEntityAccess.tlUsrDomainIsDefault.
        end. /*if not available tUsrDomain*/
        else do:
            if tUserDomainEntityAccess.tlLink = true /*available tUsrDomain and tlLink = true*/
            then do:
                assign tUsrDomain.UsrDomainIsDefault = tUserDomainEntityAccess.tlUsrDomainIsDefault
                       tUsrDomain.tc_Status = (if tUsrDomain.tc_Status = '':U or
                                                  tUsrDomain.tc_Status = 'D':U
                                               then 'C':U
                                               else tUsrDomain.tc_Status).
            end.            
        end.        

    end. /*for each tUserDomainEntityAccess where*/

    /*check which user domain combinations need te be deleted*/
    for each tUsrDomain where
             tUsrDomain.tc_ParentRowid = tUsr.tc_Rowid on error undo, throw:
        if /*check if we can not find any good record in UsrCompany*/
           not can-find(first tUsrCompany where
                              tUsrCompany.Domain_ID      = tUsrDomain.Domain_ID              and
                              tUsrCompany.tc_ParentRowid = tUsr.tc_Rowid                     and
                              tUsrCompany.tc_Status      <> 'D':U                               ) and
           /*tUsrCompany.Domain_ID is not filled for new tUsrCompany records so we need to check also tUserDomainEntityAccess*/
           not can-find(first tUserDomainEntityAccess where
                              tUserDomainEntityAccess.tcUsrLogin   = tUsr.UsrLogin           and
                              tUserDomainEntityAccess.tcDomainCode = tUsrDomain.tcDomainCode and
                              tUserDomainEntityAccess.tlLink       = true                       )
        then assign tUsrDomain.tc_Status = 'D':U.

    end.

end. /*for each tUsr:*/

<M-4 run ValidateBC
   (output viFcReturnSuper (oiReturnStatus)) in BUser>
if viFcReturnSuper <> 0
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

<M-5 run AdditionalUpdates
   (output viFcReturnSuper (oiReturnStatus)) in BUser>
if viFcReturnSuper <> 0
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

<M-6 run DataSave
   (output viFcReturnSuper (oiReturnStatus)) in BUser>
if viFcReturnSuper <> 0
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 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 = "ApiSaveUserDomainEntityAccess".
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 = "".

/* Create input dataset */
create dataset vhInputDS.
vhInputDS:read-xmlschema("file", "xml/buser.apisaveuserdomainentityaccess.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tUserDomainEntityAccess").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

/* 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.