project BLF > class Database Component > method ApiGetIdentification

Description

This method gives the possibility to get identification information (primary key, alternate key, rowid, object ID, object status) of the main table of the object, based on any of the keys of the table.
NOTE: for some business components, a few functions must be implemented manually :
- loading data starting from the alternate key
- defining the alternate key field names + values, starting from any of the other keys
(This is when the alternate key cannot be defined unambiguously by CB.)


Parameters


bcPrimaryKeyinput-outputcharacterThe primary key value of the main table of the object.
In case of a combined primary key, the different values should be separated with chr(2)
bcAlternateKeyinput-outputcharacterThe alternate key value of the main table of the object.
In case of a combined alternate key, the different values should be separated with chr(2)
bcRowidinput-outputcharacterThe rowid of the main table record of the object.
biObjectIdinput-outputintegerIdentity field of the main table of the object.
ocObjectStatusValuesoutputcharacterchr(2) seperated list of value of object status fields
ocObjectStatusFieldNamesoutputcharacterComma seperated list of field name of object status fields
ocPrimaryKeyFieldNamesoutputcharacterList of primary key fields of the main table of the object.
This is a comma separated list.
ocAlternateKeyFieldNamesoutputcharacterList of alternate key fields (names) of the main table of the object.
This is a comma separated list.
ocAlternateKeyFieldLabelsoutputcharacterList of alternate key fields (translated labels) of the main table of the object.
This is a chr(2) separated list.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


BLF
method database.GetActivitiesForObject
method BDocumentLink.ApiDumpDocuments
method BScanDaemonPerfWorkItem.ApiProcessSingleScanDaemonQueue
method BWorkObject.PostSave
method Session.GetWorkAssistantObjects


program code (program3/database.p)

if oiReturnStatus = 0
then oiReturnStatus = -98.

/* ================================================================= */
/* First get key field names (and convert to business field name)    */
/* ================================================================= */
<M-5 run GetKeyFields (input-output vcMainTable (bcTableName), 
                       output ocPrimaryKeyFieldNames (ocPrimaryKey), 
                       output ocAlternateKeyFieldNames (ocAlternateKey), 
                       output vcIdentityName (ocObjectID), 
                       output ocObjectStatusFieldNames (ocObjectStatus), 
                       output viFcReturnSuper (oiReturnStatus)) in business>
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

vcPKnames = ocPrimaryKeyFieldNames.
do viFcCount1 = 1 to num-entries(ocPrimaryKeyFieldNames):
    entry(viFcCount1,ocPrimaryKeyFieldNames) = "t":U + vcMainTable + ".":U + entry(viFcCount1,ocPrimaryKeyFieldNames).
end.
vcAKnames = ocAlternateKeyFieldNames.
do viFcCount1 = 1 to num-entries(ocAlternateKeyFieldNames):
    entry(viFcCount1,ocAlternateKeyFieldNames) = "t":U + vcMainTable + ".":U + entry(viFcCount1,ocAlternateKeyFieldNames).
end.

vcFieldList = "rowid,":U + vcPKnames.
if vcAKnames <> "" then vcFieldList = vcFieldList + ",":U + vcAKnames.
if vcIdentityName <> "" then vcFieldList = vcFieldList + ",":U + vcIdentityName.
if ocObjectStatusFieldNames <> "" then vcFieldList = vcFieldList + ",":U + ocObjectStatusFieldNames.

empty temp-table tBusinessFields.
run value ("appinfo/":U + lc(vcFcComponentName) + "_bf.p":U) (input table tBusinessFields by-reference, input vhFcSuper, output viFcReturnSuper).
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

/* ================================================================= */
/* Get key labels                                                    */
/* ================================================================= */
if ocAlternateKeyFieldNames <> ""
then do viFcCount1 = 1 to num-entries(ocAlternateKeyFieldNames):
    find first tBusinessFields where
               tBusinessFields.tcFcFieldName = entry(viFcCount1,ocAlternateKeyFieldNames) no-error.
    assign ocAlternateKeyFieldLabels = ocAlternateKeyFieldLabels
                                     + (if viFcCount1 = 1 then "" else chr(2))
                                     + (if available tBusinessFields
                                        then tBusinessFields.tcColumnLabel
                                        else "").
end.

/* ================================================================= */
/* Build query for database access.                                  */
/* ================================================================= */
if bcPrimaryKey = ""
or bcPrimaryKey = ?
then if bcAlternateKey = ""
     or bcAlternateKey = ?
     or ocAlternateKeyFieldNames = ""
     then if bcRowid = ""
          or bcRowid = ?
          then if biObjectId = 0
               or biObjectId = ?
               or vcIdentityName = ""
               then assign vcDBQuery = "".
               else assign vcDBQuery = "for each ":U + vcMainTable + " where ":U + vcMainTable
                                     + ".":U + vcIdentityName + " = ":U + string(biObjectId)
                           vcTTQuery = "for each t_o":U + vcMainTable + " where t_o":U + vcMainTable
                                     + ".":U + vcIdentityName + " = ":U + string(biObjectId).
          else assign vcDBQuery = "for each ":U + vcMainTable + " where rowid(":U
                                + vcMainTable + ") = to-rowid('":U + bcRowid + "')":U
                      vcTTQuery = "for each t_o":U + vcMainTable + " where t_o":U
                                + vcMainTable + ".tc_Rowid = '":U + bcRowid + "'":U.
     else do:
         /* Check key names against key values */
         if num-entries(ocAlternateKeyFieldNames) <> num-entries(bcAlternateKey,chr(2))
         then do:
             <M-4 run SetMessage (input  'Invalid key value ($1) for key ($2)':U (icMessage), 
                     input  replace (bcAlternateKey,chr(2),'|':U) + chr(2) + ocAlternateKeyFieldNames (icArguments), 
                     input  '' (icFieldName), 
                     input  '' (icFieldValue), 
                     input  'S':U (icType), 
                     input  3 (iiSeverity), 
                     input  '' (icRowid), 
                     input  'BLF-302':U (icFcMsgNumber), 
                     input  '' (icFcExplanation), 
                     input  '' (icFcIdentification), 
                     input  '' (icFcContext), 
                     output viFcReturnSuper (oiReturnStatus)) in database>
             assign oiReturnStatus = -3.
             return.
         end.

         do viFcCount1 = 1 to num-entries(ocAlternateKeyFieldNames):
             find first tBusinessFields where
                        tBusinessFields.tcFcFieldName = entry(viFcCount1,ocAlternateKeyFieldNames) no-error.
             if available tBusinessFields
             then vcDataType = tBusinessFields.tcDataType.
             else vcDataType = "i":U.
             vcDBQuery = vcDBQuery
                       + " and ":U + vcMainTable + ".":U
                       + entry(viFcCount1,vcAKnames) + " = ":U
                       + (if vcDataType = "c":U then "'":U else "")
                       + (if vcDataType = "t":U
                          and length(entry(viFcCount1,bcAlternateKey,chr(2)),"CHARACTER":U) = 8
                          then "date(":U + substring(entry(viFcCount1,bcAlternateKey,chr(2)),5,2,"CHARACTER":U)
                                 + ",":U + substring(entry(viFcCount1,bcAlternateKey,chr(2)),7,2,"CHARACTER":U)
                                 + ",":U + substring(entry(viFcCount1,bcAlternateKey,chr(2)),1,4,"CHARACTER":U) + ")":U
                          else entry(viFcCount1,bcAlternateKey,chr(2)))
                       + (if vcDataType = "c":U then "'":U else "").
             vcTTQuery = vcTTQuery
                       + " and t_o":U + vcMainTable + ".":U
                       + entry(viFcCount1,vcAKnames) + " = ":U
                       + (if vcDataType = "c":U then "'":U else "")
                       + (if vcDataType = "t":U
                          and length(entry(viFcCount1,bcAlternateKey,chr(2)),"CHARACTER":U) = 8
                          then "date(":U + substring(entry(viFcCount1,bcAlternateKey,chr(2)),5,2,"CHARACTER":U)
                                 + ",":U + substring(entry(viFcCount1,bcAlternateKey,chr(2)),7,2,"CHARACTER":U)
                                 + ",":U + substring(entry(viFcCount1,bcAlternateKey,chr(2)),1,4,"CHARACTER":U) + ")":U
                          else entry(viFcCount1,bcAlternateKey,chr(2)))
                       + (if vcDataType = "c":U then "'":U else "").
         end.    
    
         vcDBQuery = "for each ":U + vcMainTable + " where":U + substring(vcDBQuery,5,-1,"CHARACTER":U).
         vcTTQuery = "for each t_o":U + vcMainTable + " where":U + substring(vcTTQuery,5,-1,"CHARACTER":U).
     end.
else do:
     /* Check key names against key values */
     if num-entries(ocPrimaryKeyFieldNames) <> num-entries(bcPrimaryKey,chr(2))
     then do:
         <M-12 run SetMessage (input  'Invalid key value ($1) for key ($2)':U (icMessage), 
                      input  replace (bcPrimaryKey,chr(2),'|':U) + chr(2) + ocPrimaryKeyFieldNames (icArguments), 
                      input  '' (icFieldName), 
                      input  '' (icFieldValue), 
                      input  'S':U (icType), 
                      input  3 (iiSeverity), 
                      input  '' (icRowid), 
                      input  'BLF-303':U (icFcMsgNumber), 
                      input  '' (icFcExplanation), 
                      input  '' (icFcIdentification), 
                      input  '' (icFcContext), 
                      output viFcReturnSuper (oiReturnStatus)) in database>
         assign oiReturnStatus = -3.
         return.
     end.

     do viFcCount1 = 1 to num-entries(ocPrimaryKeyFieldNames):
         find first tBusinessFields where
                    tBusinessFields.tcFcFieldName = entry(viFcCount1,ocPrimaryKeyFieldNames) no-error.
         if available tBusinessFields
         then vcDataType = tBusinessFields.tcDataType.
         else vcDataType = "i":U.
         vcDBQuery = vcDBQuery
                   + " and ":U + vcMainTable + ".":U
                   + entry(viFcCount1,vcPKnames) + " = ":U
                   + (if vcDataType = "c":U then "'":U else "")
                   + (if vcDataType = "t":U
                      and length(entry(viFcCount1,bcPrimaryKey,chr(2)),"CHARACTER":U) = 8
                      then "date(":U + substring(entry(viFcCount1,bcPrimaryKey,chr(2)),5,2,"CHARACTER":U)
                             + ",":U + substring(entry(viFcCount1,bcPrimaryKey,chr(2)),7,2,"CHARACTER":U)
                             + ",":U + substring(entry(viFcCount1,bcPrimaryKey,chr(2)),1,4,"CHARACTER":U) + ")":U
                      else entry(viFcCount1,bcPrimaryKey,chr(2)))
                   + (if vcDataType = "c":U then "'":U else "").
         vcTTQuery = vcTTQuery
                   + " and t_o":U + vcMainTable + ".":U
                   + entry(viFcCount1,vcPKnames) + " = ":U
                   + (if vcDataType = "c":U then "'":U else "")
                   + (if vcDataType = "t":U
                      and length(entry(viFcCount1,bcPrimaryKey,chr(2)),"CHARACTER":U) = 8
                      then "date(":U + substring(entry(viFcCount1,bcPrimaryKey,chr(2)),5,2,"CHARACTER":U)
                             + ",":U + substring(entry(viFcCount1,bcPrimaryKey,chr(2)),7,2,"CHARACTER":U)
                             + ",":U + substring(entry(viFcCount1,bcPrimaryKey,chr(2)),1,4,"CHARACTER":U) + ")":U
                      else entry(viFcCount1,bcPrimaryKey,chr(2)))
                   + (if vcDataType = "c":U then "'":U else "").
     end.    

     vcDBQuery = "for each ":U + vcMainTable + " where":U + substring(vcDBQuery,5,-1,"CHARACTER":U).
     vcTTQuery = "for each t_o":U + vcMainTable + " where":U + substring(vcTTQuery,5,-1,"CHARACTER":U).
end.

/* clean up */
empty temp-table tBusinessFields.

/* ================================================================= */
/* Get key values                                                    */
/* ================================================================= */
if vcDBQuery = ""
then vcTTQuery  = "for each t_o":U + vcMainTable.
 
/* first check if record is already loaded */
<M-15 run ReadTable (input  't_o':U + vcMainTable (icTableName), 
                 input  vcTTQuery (icQuery), 
                 input  'tc_':U + vcFieldList (icFieldNames), 
                 input  no (ilConvertToDisplayFormat), 
                 output vcValueList (ocFieldValues), 
                 output viFcReturnSuper (oiReturnStatus)) in database>
if vcValueList = ""
and vcDBQuery <> ""
then do:
    /* if not, read from database */
    <M-13 run StartPersistence (output vhFcComponent (ohPersistence), 
                        output viFcReturnSuper (oiReturnStatus)) in database>
    if viFcReturnSuper <> 0
    then oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then return.

    <M-14 run ReadDirect (input  vcMainTable (icTableName), 
                  input  vcDBQuery (icPrepare), 
                  input  vcFieldList (icFieldList), 
                  output vcValueList (ocValueList), 
                  input  {&TARGETPROCEDURE} (ihClass), 
                  output viFcReturnSuper (oiReturnStatus)) in persistence>
    vcValueList = replace(vcValueList,chr(2),chr(3)).
end.
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

if  vcValueList <> ""
and vcValueList <> ?
then do:
    bcRowid = entry(1,vcValueList,chr(3)).
    
    bcPrimaryKey = "".
    do viFcCount1 = 1 to num-entries(vcPKnames):
        if viFcCount1 > 1 then bcPrimaryKey = bcPrimaryKey + chr(2).
        bcPrimaryKey = bcPrimaryKey
                     + entry(lookup(entry(viFcCount1,vcPKnames),vcFieldList),vcValueList,chr(3)).
    end.
    
    bcAlternateKey = "".
    if vcAKnames <> ""
    then do viFcCount1 = 1 to num-entries(vcAKnames):
        if viFcCount1 > 1 then bcAlternateKey = bcAlternateKey + chr(2).
        bcAlternateKey = bcAlternateKey
                     + entry(lookup(entry(viFcCount1,vcAKnames),vcFieldList),vcValueList,chr(3)).
    end.
    
    biObjectId = 0.
    if vcIdentityName <> ""
    then biObjectId = integer(entry(lookup(vcIdentityName,vcFieldList),vcValueList,chr(3))) no-error.
    
    if ocObjectStatusFieldNames <> ""
    then do viFcCount1 = 1 to num-entries(ocObjectStatusFieldNames):
        if viFcCount1 > 1 then ocObjectStatusValues = ocObjectStatusValues + chr(2).
        ocObjectStatusValues = ocObjectStatusValues
                     + entry(lookup(entry(viFcCount1,ocObjectStatusFieldNames),vcFieldList),vcValueList,chr(3)).
        entry(viFcCount1,ocObjectStatusFieldNames) = "t":U + vcMainTable + ".":U + entry(viFcCount1,ocObjectStatusFieldNames).
    end.
end.

if oiReturnStatus = -98
then oiReturnStatus = 0.