project BLF > class BBusinessComponent > method DataLoadByInput

Description

This method loads database records corresponding to input values (records found in the t_s*** tables).
When nothing found in the database, a new record should be initialised.
Input values should also be updated with :
- identity fields (override input values)
- tc_Rowid / tc_ParentRowid / tc_Status

Annotation

This template can be used to implement the code for this method.

* Replace [MainTableName] with the name of the main table in the class dataset (no prefixes).
* This template assumes that the main table has an identity field, and this field is the only field of the primary index of the table. Replace [PrimaryKeyFieldName] with the name of this field.
* This template assumes that the table has one logical alternate key (a key that is visible and editable to end-users) with one field. Replace [LogicalKeyFieldName] with the name of this field. If the logical key contains more than one field, repeat the code with this tag for every field in the key.
* The run statement following [METHOD CALL] should not be copied literally, but inserted as a method call (run the method in your own component).




for each t_s[MainTableName] by rowid(t_s[MainTableName]) on error undo, throw:

/* ================================================================= */
/* Lookup object by logical key */
/* When in create mode, skip lookup. */
/* ================================================================= */
if vcActivityCode <> "Create":U
then do:
vcFreeform = "for each [MainTableName] where [MainTableName].[LogicalKeyFieldName] = '":U + t_s[MainTableName].[LogicalKeyFieldName] + "'":U.
[METHOD CALL]
run DataLoad
(input '' (icRowids),
input '' (icPkeys),
input '' (icObjectIds),
input vcFreeform (icFreeform),
input yes (ilKeepPrevious),
output viFcReturnSuper (oiReturnStatus))
if viFcReturnSuper >= 0
then do:
/* DataLoad succeeded, no no-error needed */
find first t[MainTableName] where t[MainTableName].[LogicalKeyFieldName] = t_s[MainTableName].[LogicalKeyFieldName].
if t[MainTableName].tc_Status <> ""
then do:
[METHOD CALL]
run SetMessage
(input [duplicate key error] (icMessage),
input t_s[MainTableName].[LogicalKeyFieldName] (icArguments),
input 't[MainTableName].[LogicalKeyFieldName]':U (icFieldName),
input t_s[MainTableName].[LogicalKeyFieldName] (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input t_s[MainTableName].tc_Rowid (icRowid),
input 'ERR-xyz':U (icFcMsgNumber),
input '' (icFcExplanation),
input '' (icFcIdentification),
input '' (icFcContext),
output viFcReturnSuper (oiReturnStatus))
assign oiReturnStatus = -1.
return.
end.
end.
end.

/* ================================================================= */
/* When in delete mode, ignore input data. */
/* When object not found, do not return an error. */
/* ================================================================= */
if vcActivityCode = "Delete":U
then do:
if viFcReturnSuper = -4
then delete t_s[MainTableName].
else do:
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

/* keep the UI rowid for usage in error messages (tFcMessages.tcFcRowid) */
[METHOD CALL]
run SetRowidConvert
(input t_s[MainTableName].tc_Rowid (icOldRowid),
input t[MainTableName].tc_Rowid (icNewRowid),
output viFcReturnSuper (oiReturnStatus))

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign t_s[MainTableName].tc_Rowid = t[MainTableName].tc_Rowid
t_s[MainTableName].tc_ParentRowid = ""
opPrimeKey = (if opPrimeKey = "" then "" else opPrimeKey + chr(4))
+ string(t[MainTableName].[PrimaryKeyFieldName]).
end.

next.
end.

if vcActivityCode = "Create":U
or viFcReturnSuper = -4
then do:
/* ================================================================= */
/* When object not found and in modify only mode, return an error. */
/* ================================================================= */
if vcActivityCode = "Modify":U
then do:
[METHOD CALL]
run SetMessage
(input #T'Object with key $1 does not exist.':100T# (icMessage),
input t_s[MainTableName].[LogicalKeyFieldName] (icArguments),
input 't[MainTableName].[LogicalKeyFieldName]':U (icFieldName),
input t_s[MainTableName].[LogicalKeyFieldName] (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input t_s[MainTableName].tc_Rowid (icRowid),
input 'ERR-xyz':U (icFcMsgNumber),
input '' (icFcExplanation),
input '' (icFcIdentification),
input '' (icFcContext),
output viFcReturnSuper (oiReturnStatus))
assign oiReturnStatus = -1.
return.
end.

/* ================================================================= */
/* When object not found or in create mode, create it. */
/* ================================================================= */
[METHOD CALL]
run AddDetailLine
(input '[MainTableName]':U (icTable),
input '' (icParentRowid),
output viFcReturnSuper (oiReturnStatus))

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign t_s[MainTableName].tc_Status = "N":U.
end.
else do:
/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign t_s[MainTableName].tc_Status = "C":U
t[MainTableName].tc_Status = "C":U.
end.

if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

/* insert code for subtables here */

/* keep the UI rowid for usage in error messages (tFcMessages.tcFcRowid) */
[METHOD CALL]
run SetRowidConvert
(input t_s[MainTableName].tc_Rowid (icOldRowid),
input t[MainTableName].tc_Rowid (icNewRowid),
output viFcReturnSuper (oiReturnStatus))

/* ================================================================= */
/* override value for identity fields in input */
/* ================================================================= */
assign t_s[MainTableName].[PrimaryKeyFieldName] = t[MainTableName].[PrimaryKeyFieldName]

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
t_s[MainTableName].tc_Rowid = t[MainTableName].tc_Rowid
t_s[MainTableName].tc_ParentRowid = ""

opPrimeKey = (if opPrimeKey = "" then "" else opPrimeKey + chr(4))
+ string(t[MainTableName].[PrimaryKeyFieldName]).
end.






If the class dataset contains more than one table, insert this code for every additional table.

/* ================================================================= */
/* Delete existing detail */
/* ================================================================= */
for each t[ChildTableName] where
t[ChildTableName].tc_ParentRowid = t[ParentTableName].tc_Rowid:
assign t[ChildTableName].tc_Status = "D":U.
end.

/* ================================================================= */
/* Update / add detail */
/* ================================================================= */
for each t_s[ChildTableName] where
t_s[ChildTableName].tc_ParentRowid = t_s[ParentTableName].tc_Rowid
on error undo, throw:

find first t[ChildTableName] where
t[ChildTableName].tc_ParentRowid = t[ParentTableName].tc_Rowid and
t[ChildTableName].[LogicalKeyFieldName] = t_s[ChildTableName].[LogicalKeyFieldName] no-error.
if available t[ChildTableName]
then if t[ChildTableName].tc_Status = "D":U
then assign t_s[ChildTableName].tc_Status = "C":U
t[ChildTableName].tc_Status = "C":U.
else do:
[METHOD CALL]
run SetMessage
(input [duplicate key error] (icMessage),
input t_s[ChildTableName].[LogicalKeyFieldName] (icArguments),
input 't[ChildTableName].[LogicalKeyFieldName]':U (icFieldName),
input t_s[ChildTableName].[LogicalKeyFieldName] (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input t_s[ChildTableName].tc_Rowid (icRowid),
input 'ERR-xyz':U (icFcMsgNumber),
input '' (icFcExplanation),
input '' (icFcIdentification),
input '' (icFcContext),
output viFcReturnSuper (oiReturnStatus))
assign oiReturnStatus = -1.
return.
end.
else do:
[METHOD CALL]
run AddDetailLine (input '[ChildTableName]':U (icTable),
input t[ParentTableName].tc_Rowid (icParentRowid),
output viFcReturnSuper (oiReturnStatus))
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.
assign t_s[ChildTableName].tc_Status = "N":U
t[ChildTableName].[LogicalKeyFieldName] = t_s[ChildTableName].[LogicalKeyFieldName].
end.

/* keep the UI rowid for usage in error messages (tFcMessages.tcFcRowid) */
[METHOD CALL]
run SetRowidConvert
(input t_s[ChildTableName].tc_Rowid (icOldRowid),
input t[ChildTableName].tc_Rowid (icNewRowid),
output viFcReturnSuper (oiReturnStatus))

/* ================================================================= */
/* override value for identity fields in input */
/* ================================================================= */
assign t_s[ChildTableName].[ChildPrimaryKeyFieldName] = t[ChildTableName].[ChildPrimaryKeyFieldName]

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
t_s[ChildTableName].[ParentPrimaryKeyFieldName] = t[ChildTableName].[ParentPrimaryKeyFieldName]
t_s[ChildTableName].tc_Rowid = t[ChildTableName].tc_Rowid
t_s[ChildTableName].tc_ParentRowid = t[ParentTableName].tc_Rowid.
end.

PreCondition

The code of this method must always be overridden, not extended.


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 (program2/bbusinesscomponent.p)

for each t_sBusComponent
         by rowid(t_sBusComponent)
         on error undo, throw:
    /* ============================ */
    /* Lookup object by logical key */
    /* ============================ */
    assign vcFreeform = "for each BusComponent where BusComponent.BusComponentCode = '" + t_sBusComponent.BusComponentCode + "'".

    <M-1 run DataLoad
       (input  '' (icRowids), 
        input  '' (icPkeys), 
        input  '' (icObjectIds), 
        input  vcFreeform (icFreeform), 
        input  true (ilKeepPrevious), 
        output viFcReturnSuper (oiReturnStatus)) in BBusinessComponent>

    if viFcReturnSuper >= 0
    then find first tBusComponent where
                    tBusComponent.BusComponentCode = t_sBusComponent.BusComponentCode
                    no-error.

    /* ============================================= */
    /* When in delete mode, ignore input data.       */
    /* When object not found, do not return an error */
    /* ============================================= */
    if t_sBusComponent.tc_Status = "D"
    then do:
        if viFcReturnSuper = -4
        then do:
            for each t_sBusActivity where
                     t_sBusActivity.tc_ParentRowid = t_sBusComponent.tc_Rowid:
                delete t_sBusActivity.
            end.

            delete t_sBusComponent.
        end.
        else do:
            if viFcReturnSuper <> 0
            then assign oiReturnStatus = viFcReturnSuper.

            if viFcReturnSuper < 0
            then return.
    
            /* ====================================== */
            /* Update input to allow correct matching */
            /* ====================================== */
            if opPrimeKey <> ""
            then assign opPrimeKey = opPrimeKey + chr(4).

            assign t_sBusComponent.BusComponent_ID = tBusComponent.BusComponent_ID
                   t_sBusComponent.tc_Rowid        = tBusComponent.tc_Rowid
                   t_sBusComponent.tc_ParentRowid  = ""
                   opPrimeKey                      = opPrimeKey + string(tBusComponent.BusComponent_ID).
        end.

        next.
    end.

    if viFcReturnSuper = -4
    then do:
        /* ========================================================= */
        /* When object not found and in modify mode, return an error */
        /* ========================================================= */
        if t_sBusComponent.tc_Status = "C"
        then do:
            <M-2 run SetMessage
               (input  trim(#T-1'Object with key $1 does not exist.':100(8887)T-1#) (icMessage), 
                input  t_sBusComponent.BusComponentCode (icArguments), 
                input  'tBusComponent.BusComponentCode' (icFieldName), 
                input  t_sBusComponent.BusComponentCode (icFieldValue), 
                input  'E' (icType), 
                input  3 (iiSeverity), 
                input  t_sBusComponent.tc_Rowid (icRowid), 
                input  'BLF-223':U (icFcMsgNumber), 
                input  '' (icFcExplanation), 
                input  '' (icFcIdentification), 
                input  '' (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BBusinessComponent>

            assign oiReturnStatus = -1.
            return.
        end.

        /* ================================================== */
        /* When object not found or in create mode, create it */
        /* ================================================== */
        <M-3 run AddDetailLine
           (input  'BusComponent' (icTable), 
            input  '' (icParentRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BBusinessComponent>

        /* ====================================== */
        /* Update input to allow correct matching */
        /* ====================================== */
        assign t_sBusComponent.tc_Status = "N".
    end.
    else do:
        /* ====================================== */
        /* Update input to allow correct matching */
        /* ====================================== */
        assign t_sBusComponent.tc_Status = "C".
    end.

    if viFcReturnSuper <> 0
    then assign oiReturnStatus = viFcReturnSuper.

    if viFcReturnSuper < 0
    then return.

    /* ====================== */
    /* Delete existing detail */
    /* ====================== */
    for each tBusActivity where
             tBusActivity.tc_ParentRowid = tBusComponent.tc_Rowid:
        assign tBusActivity.tc_Status = "D".
    end.

    /* =================== */
    /* Update / add detail */
    /* =================== */
    for each t_sBusActivity where
             t_sBusActivity.tc_ParentRowid = t_sBusComponent.tc_Rowid:
        if t_sBusActivity.tc_Status = "N"
        then do:
            <M-4 run AddDetailLine
               (input  'BusActivity' (icTable), 
                input  tBusComponent.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BBusinessComponent>

            if viFcReturnSuper <> 0
            then assign oiReturnStatus = viFcReturnSuper.

            if viFcReturnSuper < 0
            then return.
        end.
        else do:
            find first tBusActivity where
                       tBusActivity.tc_ParentRowid  = tBusComponent.tc_Rowid and
                       tBusActivity.BusActivityCode = t_sBusActivity.BusActivityCode
                       no-error.

            if not available tBusActivity
            then do:
                <M-5 run SetMessage
                   (input  trim(#T-2'Activity $1 not found for component $2.':255(733755856)T-2#) (icMessage), 
                    input  t_sBusActivity.BusActivityCode + chr(2) + t_sBusComponent.BusComponentCode (icArguments), 
                    input  '' (icFieldName), 
                    input  '' (icFieldValue), 
                    input  'E' (icType), 
                    input  3 (iiSeverity), 
                    input  '' (icRowid), 
                    input  'BLF-239':U (icFcMsgNumber), 
                    input  '' (icFcExplanation), 
                    input  '' (icFcIdentification), 
                    input  '' (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BBusinessComponent>

                assign oiReturnStatus = -1.
                return.
            end.
        end.

        /* =========================================== */
        /* Override value for identity fields in input */
        /* Update input to allow correct matching      */
        /* =========================================== */
        assign t_sBusActivity.BusActivity_ID  = tBusActivity.BusActivity_ID
               t_sBusActivity.BusComponent_ID = tBusActivity.BusComponent_ID
               t_sBusActivity.tc_Rowid        = tBusActivity.tc_Rowid
               t_sBusActivity.tc_ParentRowid  = tBusComponent.tc_Rowid.

        <I-47 {UpdateTransString
             &TABLE = "BusActivity"}>
    end.
    
    <I-25 {UpdateTransString
         &TABLE = "BusComponent"}>
         
    /* =========================================== */
    /* Override value for identity fields in input */
    /* Update input to allow correct matching      */
    /* =========================================== */
    if opPrimeKey <> ""
    then assign opPrimeKey = opPrimeKey + chr(4).

    assign t_sBusComponent.BusComponent_ID = tBusComponent.BusComponent_ID
           t_sBusComponent.tc_Rowid        = tBusComponent.tc_Rowid
           t_sBusComponent.tc_ParentRowid  = ""
           opPrimeKey                      = opPrimeKey + string(tBusComponent.BusComponent_ID).
end.