project BLF > class BDocumentLink > method SerializeAndStoreDocument

Description

Store a document in the application database, and link it to parent object.
If the document already exists, it will be replaced.


Parameters


icBusinessComponentinputcharacterBusiness component name of the parent object
iiObjectIdinputintegerIdentity value of the parent object
icFileNameinputcharacterfile name to be added to the document properties (informational only)
icFullPathinputcharacterFull path of the file to be serialized and stored
oiDocumentLinkIDoutputintegerIdentity value of the document
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


BLF
method BScanDaemonPerfWorkItem.ApiProcessSingleScanDaemonQueue

QadFinancials
method BConCheckProcessor.ConCheckProcessFinish


program code (program3/bdocumentlink.p)

empty temp-table tDocumentSegments.
set-size(vmFile) = 0.
set-size(vmOut) = 0.
assign file-info:file-name = icFullPath.

if file-info:full-pathname = ?
then do:
    <M-96 run SetMessage
       (input  trim(#T-78'File not found: $1.':100(17)T-78#) (icMessage), 
        input  icFullPath (icArguments), 
        input  '' (icFieldName), 
        input  '' (icFieldValue), 
        input  'E' (icType), 
        input  3 (iiSeverity), 
        input  '' (icRowid), 
        input  'blf-796379':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BDocumentLink>

    assign oiReturnStatus = -1.
    return.
end.

/* Check for empty document */
if file-info:file-size = 0
then return.

set-size(vmFile) = file-info:file-size.
input from value(icFullPath) binary no-convert.
import vmFile.
input close.

assign vcBase64Chars [1] = "A"
       vcBase64Chars [2] = "B"
       vcBase64Chars [3] = "C"
       vcBase64Chars [4] = "D"
       vcBase64Chars [5] = "E"
       vcBase64Chars [6] = "F"
       vcBase64Chars [7] = "G"
       vcBase64Chars [8] = "H"
       vcBase64Chars [9] = "I"
       vcBase64Chars[10] = "J"
       vcBase64Chars[11] = "K"
       vcBase64Chars[12] = "L"
       vcBase64Chars[13] = "M"
       vcBase64Chars[14] = "N"
       vcBase64Chars[15] = "O"
       vcBase64Chars[16] = "P"
       vcBase64Chars[17] = "Q"
       vcBase64Chars[18] = "R"
       vcBase64Chars[19] = "S"
       vcBase64Chars[20] = "T"
       vcBase64Chars[21] = "U"
       vcBase64Chars[22] = "V"
       vcBase64Chars[23] = "W"
       vcBase64Chars[24] = "X"
       vcBase64Chars[25] = "Y"
       vcBase64Chars[26] = "Z"
       vcBase64Chars[27] = "a"
       vcBase64Chars[28] = "b"
       vcBase64Chars[29] = "c"
       vcBase64Chars[30] = "d"
       vcBase64Chars[31] = "e"
       vcBase64Chars[32] = "f"
       vcBase64Chars[33] = "g"
       vcBase64Chars[34] = "h"
       vcBase64Chars[35] = "i"
       vcBase64Chars[36] = "j"
       vcBase64Chars[37] = "k"
       vcBase64Chars[38] = "l"
       vcBase64Chars[39] = "m"
       vcBase64Chars[40] = "n"
       vcBase64Chars[41] = "o"
       vcBase64Chars[42] = "p"
       vcBase64Chars[43] = "q"
       vcBase64Chars[44] = "r"
       vcBase64Chars[45] = "s"
       vcBase64Chars[46] = "t"
       vcBase64Chars[47] = "u"
       vcBase64Chars[48] = "v"
       vcBase64Chars[49] = "w"
       vcBase64Chars[50] = "x"
       vcBase64Chars[51] = "y"
       vcBase64Chars[52] = "z"
       vcBase64Chars[53] = "0"
       vcBase64Chars[54] = "1"
       vcBase64Chars[55] = "2"
       vcBase64Chars[56] = "3"
       vcBase64Chars[57] = "4"
       vcBase64Chars[58] = "5"
       vcBase64Chars[59] = "6"
       vcBase64Chars[60] = "7"
       vcBase64Chars[61] = "8"
       vcBase64Chars[62] = "9"
       vcBase64Chars[63] = "+"
       vcBase64Chars[64] = "/".

/* vmOut should have the right length set */
assign viLength = get-size(vmFile)
       viMod    = viLength mod 3.

if viMod <> 0
then assign viLength = viLength + 3 - viMod.

assign viLength = viLength * 4 / 3.
set-size(vmOut) = viLength.

/* Base64 encoding converts three bytes of input to four bytes of output */
do while not vlDone:
    assign viInBuffer[1] = get-byte(vmFile, viCount + 1)
           viInBuffer[2] = get-byte(vmFile, viCount + 2)
           viInBuffer[3] = get-byte(vmFile, viCount + 3).
    /*
    // Calculate the outBuffer
    // The first byte of our in buffer will always be valid
    // but we must check to make sure the other two bytes
    // are not -1 before using them.
    // The basic idea is that the three bytes get split into
    // four bytes along these lines:
    // [AAAAAABB] [BBBBCCCCC] [CCDDDDDD]
    // [xxAAAAAA] [xxBBBBBB] [xxCCCCCC] [xxDDDDDD]
    // bytes are considered to be zero when absent.
    // the four bytes are then mapped to common ASCII symbols
    */
    /* A's: first six bits of first byte */
    assign viPos = viPos + 1.
    put-byte(vmOut, viPos) = asc(vcBase64Chars[get-bits(viInBuffer[1], 3, 6) + 1]).

    if viInBuffer[2] <> ?
    then do:
        /* B's: last two bits of first byte, first four bits of second byte */
        assign viPos = viPos + 1.
        put-byte(vmOut, viPos) = asc(vcBase64Chars[get-bits(viInBuffer[1], 1, 2) * 16 + get-bits(viInBuffer[2], 5, 4) + 1]).

        if viInBuffer[3] <> ?
        then do:
            /* C's: last four bits of second byte, first two bits of third byte */
            assign viPos = viPos + 1.
            put-byte(vmOut, viPos) = asc(vcBase64Chars[get-bits(viInBuffer[2], 1, 4) * 4 + get-bits(viInBuffer[3], 7, 2) + 1]).

            /* D's: last six bits of third byte */
            assign viPos = viPos + 1.
            put-byte(vmOut, viPos) = asc(vcBase64Chars[get-bits(viInBuffer[3], 1, 6) + 1]).
        end.
        else do:
            /* C's: last four bits of second byte */
            assign viPos = viPos + 1.
            put-byte(vmOut, viPos) = asc(vcBase64Chars[get-bits(viInBuffer[2], 1, 4) * 4 + 1]).

            /* An equals sign for a character that is not a Base64 character */
            assign viPos = viPos + 1.
            put-byte(vmOut, viPos) = asc("=").
            assign vlDone = true.
        end.
    end.
    else do:
        /* B's: last two bits of first byte */
        assign viPos = viPos + 1.
        put-byte(vmOut, viPos) = asc(vcBase64Chars[get-bits(viInBuffer[1], 1, 2) * 16 + 1]).

        /* An equal signs for characters that is not a Base64 characters */
        assign viPos = viPos + 1.
        put-byte(vmOut, viPos) = asc("=").
        assign viPos = viPos + 1.
        put-byte(vmOut, viPos) = asc("=").
        assign vlDone = true.
    end.

    assign viCount = viCount + 3.

    if viCount >= get-size(vmFile)
    then assign vlDone = true.
end.

assign viLength = get-size(vmOut)
       viPos    = 0
       viCount  = 0.

do while true:
    if viCount = 4000     or
       viPos   = viLength and
       viPos  <> 0
    then do:
        create tDocumentSegments.

        assign viCnt                                            = viCnt + 1
               tDocumentSegments.tiDocumentStorageSegmentNumber = viCnt
               tDocumentSegments.tcDocumentStorageString        = vcStr
               viCount                                          = 0
               vcStr                                            = "".

        if viPos = viLength
        then leave.
    end.

    assign viCount = viCount + 1
           viPos   = viPos + 1
           vcStr   = vcStr + chr(get-byte(vmOut, viPos)).
end.

<M-1 run StoreDocument
   (input  icBusinessComponent (icBusinessComponent), 
    input  iiObjectId (iiObjectId), 
    input  tDocumentSegments (tDocumentSegments), 
    input  icFileName (icFileName), 
    input  ? (ilOpenDocument), 
    input  file-info:file-size (iiSize), 
    output oiDocumentLinkID (oiDocumentLinkID), 
    output viFcReturnSuper (oiReturnStatus)) in BDocumentLink>

if viFcReturnSuper < 0 or
   oiReturnStatus  = 0
then assign oiReturnStatus = viFcReturnSuper.

if oiReturnStatus < 0
then return.

finally:
    set-size(vmFile) = 0.
    set-size(vmOut) = 0.
end finally.


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 = "BDocumentLink".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "SerializeAndStoreDocument".
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/bdocumentlink.serializeandstoredocument.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icBusinessComponent = <parameter value>
       vhParameter::iiObjectId = <parameter value>
       vhParameter::icFileName = <parameter value>
       vhParameter::icFullPath = <parameter value>.

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