project BLF > class BDocumentLink > method ApiDumpDocuments

Description

Dump document attachments from the application database into an external folder, and optionally delete the attachments.


Parameters


icComponentNameinputcharacterName of the component for which to dump documents. When unspecified, documents for all components will be dumped.
iiobjectIDinputintegerPrimary identification number of the object for which to dump documents. When zero, documents for all objects of the type specified in icComponentName will be dumped.
icFolderNameinputcharacterFolder in the file system where documents will be dumped.
ilCreateSubfoldersinputlogicalWhen true, documents will be dumped in a subfolder created for each component and each object.
ilDeleteDocumentsinputlogicalWhen true, documents will be deleted after dumping.
ilDeleteFolderContentsinputlogicalDelete all contents from the destination folder before dumping documents.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program3/bdocumentlink.p)

vcSeparator = (if opsys = "UNIX" then "/" else "~\").

file-info:file-name = icFolderName.
if file-info:file-type <> "DRW"
then do:
    oiReturnStatus = -3.
    <M-77 run SetMessage
       (input  #T-80'The specified directory does not exist or is not writable.':100(9034)T-80# (icMessage), 
        input  '' (icArguments), 
        input  '' (icFieldName), 
        input  '' (icFieldValue), 
        input  'E' (icType), 
        input  3 (iiSeverity), 
        input  '' (icRowid), 
        input  'blf-333478':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BDocumentLink>
    return.
end.

if ilDeleteFolderContents
then do:
    if ilCreateSubfolders = yes
    and icComponentName <> "" and icComponentName <> ?
    and (iiobjectID = 0 or iiobjectID = ?)
    then file-info:file-name = icFolderName + vcSeparator + icComponentName.
    
    if file-info:file-type = "DRW"
    and (iiobjectID = 0 or iiobjectID = ? or ilCreateSubfolders = no)
    then do:
        input stream sdumpdir from os-dir (file-info:full-pathname).
        repeat on error undo, leave:   /* no throw */
            import stream sdumpdir vcFileName vcFileFullname vcFileType.
            if vcFileName = "."
            or vcFileName = ".."
            then next.
            else if vcFileType begins "F"
                 then os-delete value(vcFileFullname).
                 else if vcFileType begins "D"
                      then os-delete value(vcFileFullname) recursive.
        end.
        input stream sdumpdir close.
    end.
end.

if icComponentName <> "" and icComponentName <> ?
then vcFreeformLoad = "for each DocumentLink where DocumentLink.DocumentLinkComponent = '" + icComponentName + "'"
                    + (if iiobjectID = 0 or iiobjectID = ?
                       then ""
                       else " and DocumentLink.DocumentLinkInternal_ID = " + string(iiobjectID)).
else if iiobjectID <> 0 and iiobjectID <> ?
     then vcFreeformLoad = "for each DocumentLink where DocumentLink.DocumentLinkInternal_ID = " + string(iiobjectID).
<M-12 run DataLoad
   (input  '' (icRowids), 
    input  '' (icPkeys), 
    input  '' (icObjectIds), 
    input  vcFreeformLoad (icFreeform), 
    input  no (ilKeepPrevious), 
    output viFcReturnSuper (oiReturnStatus)) in BDocumentLink>
if viFcReturnSuper = -4
then do:
    oiReturnStatus = 1.
    vcArg = (if icComponentName = "" or icComponentName = ?
             then ""
             else #T-24'Component Code':50(8022)T-24# + "=" + icComponentName)
          + " "
          + (if iiobjectID = 0 or iiobjectID = ?
             then ""
             else #T-78'Object ID':30(7156)T-78# + "=" + string(iiobjectID)).
    <M-7 run SetMessage
       (input  #T-47'Document not found. ($1)':100(476)T-47# (icMessage), 
        input  vcArg (icArguments), 
        input  '' (icFieldName), 
        input  '' (icFieldValue), 
        input  'W' (icType), 
        input  3 (iiSeverity), 
        input  '' (icRowid), 
        input  'blf-151343':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in BDocumentLink>
    return.
end.
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

for each tDocumentLink on error undo, throw:

    if ilCreateSubfolders
    then do:
        vcDocAltKeyLabel = "".
        vcDocAltKeyName = "".
        vcDocAltKeyValue = "".
        vcDocComponentMainTable = "".
        vcDocCYID = "".
        vcDocPrimKeyName = "".
        vcDocPrimKeyValue = "".
        vcDocRowid = "".
        vcDocSSETID = "".
        
        /* ================================================================= */
        /* Convert internal structure into webdav store structure.           */
        /* ================================================================= */
        <M-83 run BusinessClassActions
           (input  tDocumentLink.DocumentLinkComponent (icClassShortname), 
            input  'START+OPEN' (icAction), 
            input  no (ilSubtransaction), 
            input-output viBBusinessComponent5OC (biClassOpenCount), 
            input-output vhBBusinessComponent5Inst (bhClassInstanceHandle), 
            input-output viBBusinessComponent5ID (biClassInstanceId), 
            input  '' (icDraftReference), 
            input  '' (icUserDefinedContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDocumentLink>
        if viFcReturnSuper <> 0
        then oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then return.
        
        <M-75 run DataLoad
           (input  '' (icRowids), 
            input  '' (icPkeys), 
            input  string(tDocumentLink.DocumentLinkInternal_ID) (icObjectIds), 
            input  '' (icFreeform), 
            input  no (ilKeepPrevious), 
            output viFcReturnSuper (oiReturnStatus)) in BBusinessComponent>
        
        if viFcReturnSuper <> -4
        then do:
            /* ignore errors */
            <M-81 run ApiGetIdentification
               (input-output vcDocPrimKeyValue (bcPrimaryKey), 
                input-output vcDocAltKeyValue (bcAlternateKey), 
                input-output vcDocRowid (bcRowid), 
                input-output viDocObjectID (biObjectId), 
                output vcDummy1 (ocObjectStatusValues), 
                output vcDummy2 (ocObjectStatusFieldNames), 
                output vcDocPrimKeyName (ocPrimaryKeyFieldNames), 
                output vcDocAltKeyName (ocAlternateKeyFieldNames), 
                output vcDocAltKeyLabel (ocAlternateKeyFieldLabels), 
                output viFcReturnSuper (oiReturnStatus)) in BBusinessComponent>
            
            /* ignore errors */
            <M-42 run GetKeyFields
               (input-output vcDocComponentMainTable (bcTableName), 
                output vcDocPrimKeyName (ocPrimaryKey), 
                output vcDocAltKeyName (ocAlternateKey), 
                output vcDummy1 (ocObjectID), 
                output vcDummy2 (ocObjectStatus), 
                output viFcReturnSuper (oiReturnStatus)) in BBusinessComponent>
            
            if vcDocComponentMainTable <> ""
            then do:
                if tDocumentLink.DocumentLinkComponent = "bcinvoice"
                then do:
                    /* alternate key for cinvoice is incorrect */
                    <M-68 run ReadTable
                       (input  't_o' + vcDocComponentMainTable (icTableName), 
                        input  'for each t_o' + vcDocComponentMainTable (icQuery), 
                        input  'Company_ID,CInvoiceRegistrationNr' (icFieldNames), 
                        input  no (ilConvertToDisplayFormat), 
                        output vcDummy1 (ocFieldValues), 
                        output viFcReturnSuper (oiReturnStatus)) in BBusinessComponent>
                    if num-entries (vcDummy1,chr(3)) = 2
                    then assign vcDocCYID        = entry (1,vcDummy1,chr(3))
                                vcDocAltKeyValue = entry (2,vcDummy1,chr(3)).                    
                end.
                else do:
                    /* ignore errors */
                    <M-11 run ReadTable
                       (input  't_o' + vcDocComponentMainTable (icTableName), 
                        input  'for each t_o' + vcDocComponentMainTable (icQuery), 
                        input  'Company_ID,SharedSet_ID' (icFieldNames), 
                        input  no (ilConvertToDisplayFormat), 
                        output vcDummy1 (ocFieldValues), 
                        output viFcReturnSuper (oiReturnStatus)) in BBusinessComponent>
                    if num-entries (vcDummy1,chr(3)) = 2
                    then assign vcDocCYID   = entry (1,vcDummy1,chr(3))
                                vcDocSSETID = entry (2,vcDummy1,chr(3)).
                end.
            end.
        end.
        
        /* ignore errors */
        <M-57 run BusinessClassActions
           (input  tDocumentLink.DocumentLinkComponent (icClassShortname), 
            input  'CLOSE+STOP' (icAction), 
            input  no (ilSubtransaction), 
            input-output viBBusinessComponent5OC (biClassOpenCount), 
            input-output vhBBusinessComponent5Inst (bhClassInstanceHandle), 
            input-output viBBusinessComponent5ID (biClassInstanceId), 
            input  '' (icDraftReference), 
            input  '' (icUserDefinedContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDocumentLink>
        
        if vcDocCYID = ""
        or vcDocCYID = ?
        then vcDocCYID = "0".
        
        if vcDocSSETID = ""
        or vcDocSSETID = ?
        then vcDocSSETID = "0".
        
        if vcDocAltKeyValue = ""
        or vcDocAltKeyValue = ?
        then vcDocAltKeyValue = (if vcDocPrimKeyValue = "" or vcDocPrimKeyValue = ? then string(tDocumentLink.DocumentLinkInternal_ID) else vcDocPrimKeyValue).
        
        vcExportDir = icFolderName + vcSeparator + tDocumentLink.DocumentLinkComponent.
        os-create-dir value(vcExportDir).
        vcExportDir = vcExportDir + vcSeparator + vcDocCYID.
        os-create-dir value(vcExportDir).
        vcExportDir = vcExportDir + vcSeparator + vcDocSSETID.
        os-create-dir value(vcExportDir).
        vcExportDir = vcExportDir + vcSeparator + replace (vcDocAltKeyValue,chr(2),"-").
        os-create-dir value(vcExportDir).

        if ilDeleteFolderContents = yes
        and iiobjectID <> 0 and iiobjectID <> ?
        then do:
            ilDeleteFolderContents = no.
            input stream sdumpdir from os-dir (vcExportDir).
            repeat on error undo, leave:   /* no throw */
                import stream sdumpdir vcFileName vcFileFullname vcFileType.
                if vcFileName = "."
                or vcFileName = ".."
                then next.
                else if vcFileType begins "F"
                     then os-delete value(vcFileFullname).
                     else if vcFileType begins "D"
                          then os-delete value(vcFileFullname) recursive.
            end.
            input stream sdumpdir close.
        end.
    end.
    else vcExportDir = icFolderName.
    
    viFileCount = 0.
    do while true:
       vcExportFile = entry(num-entries(tDocumentLink.DocumentLinkDescription,"/"),tDocumentLink.DocumentLinkDescription,"/").
       vcExportFile = entry(num-entries(vcExportFile,"~\"),vcExportFile,"~\").
       if viFileCount > 0
       then if num-entries(vcExportFile,".") > 1
            then entry(1,vcExportFile,".") = entry(1,vcExportFile,".") + "(" + string(viFileCount) + ")".
            else vcExportFile = vcExportFile + "(" + string(viFileCount) + ")".
        if search (vcExportDir + vcSeparator + vcExportFile) = ?
        then leave.
        else viFileCount = viFileCount + 1.
    end.
    
    vpDocument = "".
    for each tDocumentStorage where
             tDocumentStorage.tc_ParentRowid = tDocumentLink.tc_Rowid
             by tDocumentStorage.DocumentStorageSegmentNr on error undo, throw:
        vpDocument = vpDocument + tDocumentStorage.DocumentStorageString.
    end.
    vmDocument = base64-decode(vpDocument).
    copy-lob from vmDocument to file vcExportDir + vcSeparator + vcExportFile.
end.

if ilDeleteDocuments
then do:
    <M-4 run DataDelete  (output viFcReturnSuper (oiReturnStatus)) in BDocumentLink>
    if viFcReturnSuper <> 0
    then oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then return.

    <M-44 run DataSave  (output viFcReturnSuper (oiReturnStatus)) in BDocumentLink>
    if viFcReturnSuper <> 0
    then oiReturnStatus = viFcReturnSuper.
    if viFcReturnSuper < 0
    then return.
end.

finally:
    set-size (vmDocument) = 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 = "ApiDumpDocuments".
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.apidumpdocuments.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icComponentName = <parameter value>
       vhParameter::iiobjectID = <parameter value>
       vhParameter::icFolderName = <parameter value>
       vhParameter::ilCreateSubfolders = <parameter value>
       vhParameter::ilDeleteDocuments = <parameter value>
       vhParameter::ilDeleteFolderContents = <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.