project QadFinancials > class BBusinessRelation > method DataLoadByInputSampleCode

Description

Create example DataLoadByInput
We will create a new method DataLoadByInputSampleCode as an example with a better performance. This new method will not be called by the framework: it is just a working example to be used in the documentation later on.


Parameters


opPrimeKeyoutputlongcharPrimary Key field value of loaded record(s).
Keys are chr(4) separated.
Key fields are chr(2) separated.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program9/bbusinessrelation.p)

/* === BBusinessRelation:DataLoadByInput === */

/* ================== */
/* Exception handling */
/* ================== */
assign oiReturnStatus      = -98
       viLocalReturnStatus = 0.

/* ======================================== */
/* Start queries that are used in all cases */
/* ======================================== */

/** ================== */
/* Start main block   */
/* ================== */
PROCESSBLOCK: DO :
    
    /* ============================================================================================================ */
    /* First loop on the main table to load all business-relations that need to be loaded into the instance         */
    /* Raise no error in case the data couldn't be read as this will be checked later on combined with the activity */
    /* The max-length a character field (like vcDataLoadFreeFrom) is limited to 32 Kb so we need to do several      */
    /* DataLoads to load all db-instances into the business-instance: we will do a DataLoadfor every 500 business-  */
    /* relations as counting them is much more performant then just checking the length of a string.                */
    /* ============================================================================================================ */
    if vcActivityCode <> "Create":U 
    then do : 
        assign vcDataLoadFreeFrom = "":U.
        for each t_sBusinessRelation :
            if vcDataLoadFreeFrom = "":U
            then assign vcDataLoadFreeFrom = "for each BusinessRelation where ":U.
            else assign vcDataLoadFreeFrom = vcDataLoadFreeFrom + " or ":U.
            assign vcDataLoadFreeFrom = " BusinessRelation.BusinessRelationCode = '":U + t_sBusinessRelation.BusinessRelationCode + "'":U
                   viBusRelCounter    = viBusRelCounter + 1.
            if viBusRelCounter > 500
            then do :
                <M-36 run DataLoad
                   (input  ? (icRowids), 
                    input  ? (icPkeys), 
                    input  ? (icObjectIds), 
                    input  vcDataLoadFreeFrom (icFreeform), 
                    input  true (ilKeepPrevious), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                if viFcReturnSuper <> 0 and 
                   viFcReturnSuper <> -4
                then assign viLocalReturnStatus = viFcReturnSuper.
                if viLocalReturnStatus < 0
                then Leave PROCESSBLOCK.
                assign vcDataLoadFreeFrom = "":U
                       viBusRelCounter    = 0.
            end. /* if viBusRelCounter > 500 */
        end. /* for each t_sBusinessRelation */
        if vcDataLoadFreeFrom <> "":U 
        then do :
            <M-37 run DataLoad
               (input  ? (icRowids), 
                input  ? (icPkeys), 
                input  ? (icObjectIds), 
                input  vcDataLoadFreeFrom (icFreeform), 
                input  true (ilKeepPrevious), 
                output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
            if viFcReturnSuper <> 0 and 
               viFcReturnSuper <> -4
            then assign viLocalReturnStatus = viFcReturnSuper.
            if viLocalReturnStatus < 0
            then Leave PROCESSBLOCK.
        end. /* if vcDataLoadFreeFrom <> "":U */
    end. /* if vcActivityCode <> "Create":U */
    
    /* =========================================================================================================== */
    /* Now go through the input BusRels and take the appropriate actions for each input BusRel record of the input */
    /* =========================================================================================================== */
    for each t_sBusinessRelation by t_sBusinessRelation.tc_Rowid:
        
        /* =================================== */
        /* Create mode: create new BusRel      */
        /* =================================== */
        if vcActivityCode = "Create":U 
        then do :
            <M-40 run AddDetailLine
               (input  'BusinessRelation':U (icTable), 
                input  '' (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
            if viFcReturnSuper <> 0
            then viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0
            then leave PROCESSBLOCK.
            /* Update input to allow correct matching */
            assign t_sBusinessRelation.tc_Status = "N":U.
        end. /* if vcActivityCode = "Create":U  */
        else do :
            /* ======================================================= */
            /* Not in create mode: try to find the BusRel              */
            /* ======================================================= */
            find first tBusinessRelation where 
                       tBusinessRelation.BusinessRelationCode = t_sBusinessRelation.BusinessRelationCode 
                       no-error.
            /* ======================================= */
            /* Actions when we cannot find the BusRel  */
            /* ======================================= */
            if not available tBusinessRelation
            then do :
                /* For delete: just remove the input record and go to the next one */
                if vcActivityCode = "Delete":U
                then do :
                    delete t_sBusinessRelation.
                    Next.
                end. /* if vcActivityCode = "Delete":U */
                else /* For delete: raise an error */
                     if vcActivityCode = "Modify":U
                     then do :
                        assign vcMessage           = trim(substitute(#T-42'Business-relation with code &1 cannot be updated as it cannot be found.':255(73461913)T-42#,t_sBusinessRelation.BusinessRelationCode))
                               viLocalReturnStatus = -3.
                        <M-41 run SetMessage
                           (input  vcMessage (icMessage), 
                            input  '':U (icArguments), 
                            input  'tBusinessRelation.BusinessRelationCode':U (icFieldName), 
                            input  t_sBusinessRelation.BusinessRelationCode (icFieldValue), 
                            input  'E':U (icType), 
                            input  3 (iiSeverity), 
                            input  t_sBusinessRelation.tc_Rowid (icRowid), 
                            input  'QadFin-9583':U:U (icFcMsgNumber), 
                            input  '' (icFcExplanation), 
                            input  '' (icFcIdentification), 
                            input  '' (icFcContext), 
                            output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>            
                        Leave PROCESSBLOCK.
                     end. /* if vcActivityCode = "Modify":U */
                     /* For non-delete and non-modify (thus also including blank) activity: create just one */
                     else do :
                        <M-43 run AddDetailLine
                           (input  'BusinessRelation':U (icTable), 
                            input  '' (icParentRowid), 
                            output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                        if viFcReturnSuper <> 0
                        then viLocalReturnStatus = viFcReturnSuper.
                        if viFcReturnSuper < 0
                        then leave PROCESSBLOCK.
                        /* Update input to allow correct matching */
                        assign t_sBusinessRelation.tc_Status = "N":U.
                     end. /* Not if vcActivityCode = "Modify":U */ 
            end. /* if not available tBusinessRelation */
            /* ======================================= */
            /* Actions when we can find the BusRel     */
            /* ======================================= */
            else do:
                /* ======================================================= */
                /* If we can find the record with a filled tc_status then  */
                /* this means we have a duplicate record in our input      */
                /* ======================================================= */
                if tBusinessRelation.tc_Status <> "":U
                then do :
                    assign vcMessage           = trim(substitute(#T-46'Business-relation with code &1 was detected multiple times in the input. Unable to state which one should be the one to handle.':255(332065570)T-46#,t_sBusinessRelation.BusinessRelationCode))
                           viLocalReturnStatus = -3.
                    <M-44 run SetMessage
                       (input  vcMessage (icMessage), 
                        input  '':U (icArguments), 
                        input  'tBusinessRelation.BusinessRelationCode':U (icFieldName), 
                        input  t_sBusinessRelation.BusinessRelationCode (icFieldValue), 
                        input  'E':U (icType), 
                        input  3 (iiSeverity), 
                        input  t_sBusinessRelation.tc_Rowid (icRowid), 
                        input  'QadFin-9606':U:U (icFcMsgNumber), 
                        input  '' (icFcExplanation), 
                        input  '' (icFcIdentification), 
                        input  '' (icFcContext), 
                        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>            
                    Leave PROCESSBLOCK.
                end. /* if tBusinessRelation.tc_Status <> "":U */
                /* ======================================================================== */
                /* For delete: just mark the input record as deleted and go to the next one */
                /* ======================================================================== */
                if vcActivityCode = "Delete":U
                then do :
                    /* Update input to allow correct matching and go to the next one */
                    assign tBusinessRelation.tc_status        = "D":U
                           t_sBusinessRelation.tc_status      = "D":U
                           t_sBusinessRelation.tc_Rowid       = tBusinessRelation.tc_Rowid
                           t_sBusinessRelation.tc_ParentRowid = "":U
                           opPrimeKey                         = (if opPrimeKey = "" then "" else opPrimeKey + chr(4)) + 
                                                                 string(tBusinessRelation.BusinessRelation_ID).
                    Next.
                end. /* if vcActivityCode = "Delete":U */
                /* ================================== */
                /* For modify: just set the tc_status */
                /* ================================== */
                else assign t_sBusinessRelation.tc_Status = "C":U  /* Update input to allow correct matching */
                            tBusinessRelation.tc_Status   = "C":U. 
            end. /* Not if not available tBusinessRelation */
        end. /* Not if vcActivityCode = "Create":U  */
        
        /* ===================================================== */
        /* Delete existing Addresses of the BusinessRelation */
        /* ===================================================== */
        for each tAddress where
                 tAddress.tc_ParentRowid = tBusinessRelation.tc_Rowid:
            assign tAddress.tc_Status = "D":U.
        end. /* For each tAddress where */
        
        /* ===================================================== */
        /* Actions for the new Addresses of the BusinessRelation */
        /* ===================================================== */
        for each t_sAddress where
                 t_sAddress.tc_ParentRowid = t_sBusinessRelation.tc_Rowid and
                 t_sAddress.tc_Status     <> "D":U:
            /* Try to find the address based upon the parentrowid and the first logical unique key of the table */
            find first tAddress where 
                       tAddress.tc_ParentRowid      = tBusinessRelation.tc_Rowid and 
                       tAddress.AddressStreet1      = t_sAddress.AddressStreet1  and 
                       tAddress.AddressStreet2      = t_sAddress.AddressStreet2  and 
                       tAddress.AddressStreet3      = t_sAddress.AddressStreet3  and 
                       tAddress.AddressZip          = t_sAddress.AddressZip      and 
                       tAddress.AddressCity         = t_sAddress.AddressCity     and 
                       tAddress.tcAddressTypeCode   = t_sAddress.tcAddressTypeCode
                       no-error.
            /* Try to find the address based upon the parentrowid and the second logical unique key of the table */
            if not available tAddress
            then find first tAddress where 
                            tAddress.tc_ParentRowid        = tBusinessRelation.tc_Rowid       and
                            tAddress.AddressLogicKeyString = t_sAddress.AddressLogicKeyString and
                            tAddress.tcAddressTypeCode     = t_sAddress.tcAddressTypeCode
                            no-error.
            /* Mark the record as being changed or create new record in case it could not be found */
            if available tAddress
            then assign t_sAddress.tc_Status  = "C":U
                        t_sAddress.Address_ID = tAddress.Address_ID.
            else do :
                <M-22 run AddDetailLine
                   (input  'Address':U (icTable), 
                    input  tBusinessRelation.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                if viFcReturnSuper <> 0
                then viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0
                then leave PROCESSBLOCK.
                assign t_sAddress.tc_Status = "N":U. /* Update input to allow correct matching */
            end. /* Not if available tAddress */
            
            /* ===================================================== */
            /* Delete existing Contacts of the Address               */
            /* ===================================================== */
            for each tContact where
                     tContact.tc_ParentRowid = tAddress.tc_Rowid:
                assign tContact.tc_Status = "D":U.
            end. /* for each tContact where */
            
            /* ===================================================== */
            /* Actions for the new Contacts of the Address           */
            /* ===================================================== */
            for each t_sContact where
                     t_sContact.tc_ParentRowid = t_sAddress.tc_Rowid and
                     t_sContact.tc_Status     <> "D":U:
                /* Try to find the address based upon the parentrowid and the first logical unique key of the table */
                find first tContact where
                           tContact.tc_ParentRowid = tAddress.tc_Rowid and
                           tContact.ContactName    = t_sContact.ContactName no-error.
                /* Mark the record as being changed or create new record in case it could not be found */
                if available tContact
                then assign t_sContact.tc_Status = "C":U.
                else do:
                    <M-23 run AddDetailLine
                       (input  'Contact':U (icTable), 
                        input  tAddress.tc_Rowid (icParentRowid), 
                        output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                    if viFcReturnSuper <> 0
                    then viLocalReturnStatus = viFcReturnSuper.
                    if viFcReturnSuper < 0
                    then leave PROCESSBLOCK.
                    assign t_sContact.tc_Status = "N":U. /* Update input to allow correct matching */
                end. /* Not if available tContact */
        
                /* ============================================== */
                /* Update input-contact to allow correct matching */
                /* ============================================== */
                assign t_sContact.Contact_ID     = tContact.Contact_ID
                       t_sContact.Address_ID     = tContact.Address_ID
                       t_sContact.tc_Rowid       = tContact.tc_Rowid
                       t_sContact.tc_ParentRowid = tAddress.tc_Rowid.
                
            end. /*  for each t_sContact where */

            /* ============================================== */
            /* Update input-contact to allow correct matching */
            /* ============================================== */
            assign t_sAddress.Address_ID          = tAddress.Address_ID
                   t_sAddress.BusinessRelation_ID = tAddress.BusinessRelation_ID
                   t_sAddress.tc_Rowid            = tAddress.tc_Rowid
                   t_sAddress.tc_ParentRowid      = tBusinessRelation.tc_Rowid.

        end. /* for each t_sAddress where */
        
        /* ========================================================= */
        /* Delete existing BusRelSafDefaults of the BusinessRelation */
        /* ========================================================= */
        for each tBusRelationSafDefault where
                 tBusRelationSafDefault.tc_ParentRowid = tBusinessRelation.tc_Rowid :
            assign tBusRelationSafDefault.tc_Status = "D":U.
        end. /* For each tBusRelationSafDefault where */
        
        /* ============================================================= */
        /* Actions for the new BusRelSafDefaults of the BusinessRelation */
        /* ============================================================= */
        for each t_sBusRelationSafDefault where
                 t_sBusRelationSafDefault.tc_ParentRowid = t_sBusinessRelation.tc_Rowid and
                 t_sBusRelationSafDefault.tc_Status     <> "D":U :
            /* Try to find the BusRelSafDefaults based upon the parentrowid and the logical unique key of the table */
            find first tBusRelationSafDefault where 
                       tBusRelationSafDefault.tc_ParentRowid    = tBusinessRelation.tc_Rowid and
                       tBusRelationSafDefault.tcSafConceptCode  = t_sBusRelationSafDefault.tcSafConceptCode
                       no-error.
            /* Mark the record as being changed or create new record in case it could not be found */
            if available tBusRelationSafDefault
            then assign t_sBusRelationSafDefault.tc_Status = "C":U.
            else do:
                <M-24 run AddDetailLine
                   (input  'BusRelationSafDefault':U (icTable), 
                    input  tBusinessRelation.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                if viFcReturnSuper <> 0
                then viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0
                then leave PROCESSBLOCK.
                assign t_sBusRelationSafDefault.tc_Status = "N":U. /* /* Update input to allow correct matching */ */
            end. /* Not if available tBusRelationSafDefault */
    
            /* ======================================================== */
            /* Update input-BusRelSafDefaults to allow correct matching */
            /* ======================================================== */
            assign t_sBusRelationSafDefault.BusRelationSafDefault_ID = tBusRelationSafDefault.BusRelationSafDefault_ID
                   t_sBusRelationSafDefault.BusinessRelation_ID      = tBusRelationSafDefault.BusinessRelation_ID
                   t_sBusRelationSafDefault.tc_Rowid                 = tBusRelationSafDefault.tc_Rowid
                   t_sBusRelationSafDefault.tc_ParentRowid           = tBusinessRelation.tc_Rowid.
            
        end. /* for each t_sBusRelationSafDefault where */
        
        /* =================================================== */
        /* Delete existing VATNumbers of the BusinessRelation  */
        /* =================================================== */
        for each tVatNumber where
                 tVatNumber.tc_ParentRowid = tBusinessRelation.tc_Rowid:
            assign tVatNumber.tc_Status = "D":U.
        end. /* for each tVatNumber where */
        
        /* ======================================================= */
        /* Actions for the new VATNumbers of the BusinessRelation  */
        /* ======================================================= */
        for each t_sVatNumber where
                 t_sVatNumber.tc_ParentRowid = t_sBusinessRelation.tc_Rowid and
                 t_sVatNumber.tc_Status     <> "D":U:
            /* Try to find the address based upon the parentrowid and the logical unique key of the table */
            find first tVatNumber where 
                       tVatNumber.tc_ParentRowid        = tBusinessRelation.tc_Rowid     and 
                       tVatNumber.VatNumberIdentity     = t_sVatNumber.VatNumberIdentity and 
                       tVatNumber.tcIdentityCountryCode = t_sVatNumber.tcIdentityCountryCode
                       no-error.
            /* Mark the record as being changed or create new record in case it could not be found */
            if available tVatNumber
            then assign t_sVatNumber.tc_Status = "C":U.
            else do:
                <M-25 run AddDetailLine
                   (input  'VatNumber':U (icTable), 
                    input  tBusinessRelation.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
                if viFcReturnSuper <> 0
                then viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0
                then leave PROCESSBLOCK.
                assign t_sVatNumber.tc_Status = "N":U. /* Update input to allow correct matching */
            end. /* Not if available tVatNumber */
        
            /* ============================================== */
            /* Update input-contact to allow correct matching */
            /* ============================================== */
            assign t_sVatNumber.VatNumber_ID        = tVatNumber.VatNumber_ID
                   t_sVatNumber.BusinessRelation_ID = tVatNumber.BusinessRelation_ID
                   t_sVatNumber.tc_Rowid            = tVatNumber.tc_Rowid
                   t_sVatNumber.tc_ParentRowid      = tBusinessRelation.tc_Rowid.
        
        end. /* or each t_sVatNumber where */
        
        /* ============================================== */
        /* Update input-contact to allow correct matching */
        /* ============================================== */
        assign t_sBusinessRelation.BusinessRelation_ID  = tBusinessRelation.BusinessRelation_ID
               t_sBusinessRelation.tc_Rowid             = tBusinessRelation.tc_Rowid
               t_sBusinessRelation.tc_ParentRowid       = "":U
               opPrimeKey                               = (if opPrimeKey = "" then "" else opPrimeKey + chr(4)) + 
                                                          string(tBusinessRelation.BusinessRelation_ID).
        
    end. /* for each t_sBusinessRelation by t_sBusinessRelation.tc_Rowid: */
    
END. /* PROCESSBLOCK: DO : */

/* =============================================================================== */
/* Start queries that are used in all cases and queries that are used conditionaly */
/* =============================================================================== */

/* ================== */
/* Exception handling */
/* ================== */
assign oiReturnStatus = viLocalReturnStatus.