project QadFinancials > class BJournal > method ApiStdMaintainTT

Description

This method can create new instances or update existing instances based on the content of the input temp-table.
The action that is passed as a parameter will define if the records are saved, saved as draft or just validated.

If the journal can be found (based on its code), the existing journal is updated.
If not, a new journal is created.

Each record in the main input temp-table will start a seperate transaction.

PostCondition

After the execution of the method the records in the temp-tables are created/modified in the database. In case of a delete action (tc_status = 'D'), the records are removed from the database.


Parameters


tApiJournalinputtemp-tableTemp-table like journal with the records that need to be created or modified.
icActioninputcharacterThis input parameter defines what should happen with the input data:
- SAVE: Save the data if there were no errors.
- SAVESTORE: Save the data if there were no errors. If there were, save as draft.
- STORE: Save the data as draft.
- VALIDATE: Validate the input data, but do not save the data.
ocLstPrimKeyoutputcharacterReturns the list of IDs for all records that were processed.
ocLstReturnoutputcharacterReturns the list of returnvalues for all records that were processed.
ocLstRowidoutputcharacterReturns the list of Rowids for all record that were processed.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program7/bjournal.p)

/* Method Info:                                        */
/* Journal will be loaded based on the Logical Key     */
/* =================================================== */

/* ============================================================================ */
/* Clear the instance:                                                          */
/* this will also clear the t_s tables that were filled in ApiMaintainByDataSet */
/* ============================================================================ */

<M-1 run ClearData (output viFcReturnSuper (oiReturnStatus)) in BJournal>
if viFcReturnSuper <> 0
then assign oiReturnStatus = viFcReturnSuper.
if oiReturnStatus < 0
then return.

/* ==================== */
/* Set Default Return   */
/* Parameter Validation */
/* ==================== */
assign oiReturnStatus = -98
       vlDraftInstanceCreated = false.
if icAction = ? then assign icAction = "":U.
if icAction <> {&DAEMONACTION-SAVESTORE} and
   icAction <> {&DAEMONACTION-SAVE}      and
   icAction <> {&DAEMONACTION-STORE}     and
   icAction <> {&DAEMONACTION-VALIDATE}
then do:
    assign vcJournalMsg   = trim(subst(#T-18'Daybook integration: the specified action (&1) should be in the list &2.':255(2706)t-18#,trim(icAction),"{&DAEMONACTION-SAVESTORE},{&DAEMONACTION-SAVE},{&DAEMONACTION-STORE},{&DAEMONACTION-VALIDATE}":U))
           oiReturnStatus = -1.
    <M-2 run SetMessage (input  vcJournalMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2636':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BJournal>
    return.
end. /* if icAction */

/* ======================== */
/* Count main-table records */
/* ======================== */
for each tApiJournal no-lock :
    assign viMainTableRecordCount = viMainTableRecordCount + 1.
end. /* for each */
if viMainTableRecordCount = 0
then do:
    assign vcJournalMsg   = trim(#T-19'Daybook integration: no main table records (daybook) found.':255(2707)T-19#)
           oiReturnStatus = -1.

    <M-3 run SetMessage (input  vcJournalMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2637':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BJournal>
    return.
end. /* if viMainTableRecordCount */

/* ================================================================= */
/*   Start queries                                                   */
/* ================================================================= */

<Q-9 run JournalPrim (Start) in BJournal >

/* ================================ */
/* Start loop on main-table records */
/* ================================ */
for each tApiJournal by tApiJournal.tc_Rowid: 
    
    MAIN-BLOCK:
    DO:

    /* ========== */
    /* Initialize */
    /* ========== */
    assign vcJournalMsg        = "":U
           vcRowID             = "":U
           vlWarningsFound     = false
           vlSaveAsDraft       = icAction = {&DAEMONACTION-STORE}.
    
    
    /* =========================================== */
    /* Get the Journal_ID based on the logical key */
    /* =========================================== */
    if (tApiJournal.Journal_ID  = ?   or
        tApiJournal.Journal_ID  = 0)  and
        tApiJournal.JournalCode <> "" and
        tApiJournal.JournalCode <> ?
    then do:
        <Q-10 run JournalPrim (all) (Read) (NoCache)
          (input viCompanyId, (CompanyId)
           input tApiJournal.JournalCode, (JournalCode)
           input ?, (JournalID)
           output dataset tqJournalPrim) in BJournal >
                
        find tqJournalPrim where 
             tqJournalPrim.tcJournalCode = tApiJournal.JournalCode             
             no-lock no-error.
        if available tqJournalPrim
        then assign tApiJournal.Journal_ID = tqJournalPrim.tiJournal_ID.
    end.

    /* ==================== */
    /* Load/Create Journal  */
    /* ==================== */
    if tApiJournal.Journal_ID = ? or
       tApiJournal.Journal_ID = 0
    then assign viFcReturnSuper = -4.
    else do:
        <M-11 run DataLoad (input  ? (icRowids), 
                    input  string(tApiJournal.Journal_ID) (icPkeys), 
                    input  ? (icObjectIds), 
                    input  ? (icFreeform), 
                    input  false (ilKeepPrevious), 
                    output viFcReturnSuper (oiReturnStatus)) in BJournal>        
    end.
    if viFcReturnSuper = -4
    then do:
        assign viFcReturnSuper = 0.
        <M-12 run DataNew (output viFcReturnSuper (oiReturnStatus)) in BJournal>
                       
        if viFcReturnSuper < 0
        then assign vcJournalMsg = trim(subst(#T-20'Cannot create a new instance. Error number: &1.':255(308)t-20#, string(viFcReturnSuper))).

        find first tJournal no-error.
    end. /*if viFcReturnSuper = -4*/
    else do:
        if viFcReturnSuper < 0
        then assign vcJournalMsg = trim(subst(#T-21'Cannot load the instance (error number: &1, instance ID: &2).':255(289)t-21#, string(viFcReturnSuper), string(tApiJournal.Journal_ID))).
        else do:
            find tJournal where
                 tJournal.Journal_ID = tApiJournal.Journal_ID
                 no-error.
            if not available tJournal
            then assign vcJournalMsg = trim(subst(#T-22'Cannot find the instance after it has been loaded (instance ID: &1).':255(290)t-22#,
                                               string(tApiJournal.Journal_ID))).
            else assign tJournal.tc_Status = "C":U.
        end.
    end. /*NOT if viFcReturnSuper = -4*/
    
    /* ====================================== */
    /* Check for errors in the previous block */
    /* ====================================== */

    if vcJournalMsg <> "":U
    then do:
        assign ocLstReturn   = ocLstReturn  + chr(4) + (if viFcReturnSuper = 0 then "-3":U else string(viFcReturnSuper))
               ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid    = ocLstRowid   + chr(4) + "*":U 
               vlSaveAsDraft = false.

        <M-5 run SetMessage (input  vcJournalMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2638':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BJournal>               
        leave MAIN-BLOCK.
    end.

    /* ====================== */
    /* Copy data for Journal  */
    /* ====================== */          
    
    buffer-copy tApiJournal
         except tApiJournal.Journal_ID
                tApiJournal.SharedSet_ID
                tApiJournal.tc_ParentRowid
                tApiJournal.tc_Rowid
                tApiJournal.tc_Status      
                tApiJournal.JournalCode
             to tJournal.
    if tJournal.tc_Status = "N":U
    then assign tJournal.JournalCode = tApiJournal.JournalCode.

    assign vcRowID                = tJournal.tc_Rowid
           tApiJournal.Journal_ID = tJournal.Journal_ID /*Needed to set the prim-key after the save*/.
    
    /* =================================================== */
    /* This is where it ends if only SaveAsDraft is needed */
    /* =================================================== */
    if vlSaveAsDraft
    then do:
        assign ocLstReturn  = ocLstReturn  + chr(4) + "-1":U
               ocLstPrimKey = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid   = ocLstRowid   + chr(4) + "*":U.
        leave MAIN-BLOCK.
    end.

    /* =============================== */
	/* Set Actvity                     */
	/* =============================== */
	if can-find (first tJournal where
			   		   tJournal.tc_Status = "N":U)
	then assign vcActivityCode = "Create":U.
	else if can-find (first tJournal where
							tJournal.tc_Status = "C":U)
	then assign vcActivityCode = "Modify":U.

    /* ================= */
    /* Validate the data */
    /* ================= */
    <M-13 run ValidateBC (output viFcReturnSuper (oiReturnStatus)) in BJournal>
       
    if viFcReturnSuper < 0
    then do:   
        assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
               ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid    = ocLstRowid   + chr(4) + "*":U
               vlSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE}).
        leave main-block.
    end.
    else if viFcReturnSuper > 0
    then assign vlWarningsFound = true.
        
    
    /* ================ */
    /* External updates */
    /* ================ */
    <M-15 run AdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BJournal>
            
    if viFcReturnSuper < 0
    then do:
        assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
               ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid    = ocLstRowid   + chr(4) + "*":U
               vlSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE}).
        leave main-block.               
    end.
    else if viFcReturnSuper > 0
    then assign vlWarningsFound = true.
         
    /* ============= */
    /* Save the data */
    /* ============= */
    if icAction <> {&DAEMONACTION-VALIDATE}
    then do:
        <M-16 run DataSave (output viFcReturnSuper (oiReturnStatus)) in BJournal>
                               
        if viFcReturnSuper < 0
        then do:   
            assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
                   ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
                   ocLstRowid    = ocLstRowid   + chr(4) + "*":U
                   vlSaveAsDraft = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE}).                         
            leave main-block.
        end.
        else if viFcReturnSuper > 0
        then assign vlWarningsFound = true.
    end.
  
    /* ============================================ */
    /* Indicate that everything was saved correctly */
    /* ============================================ */
    assign ocLstReturn   = ocLstReturn  + chr(4) + (if vlWarningsFound = true then "1":U else "0":U)
           ocLstPrimKey  = ocLstPrimKey + chr(4) + string(tApiJournal.Journal_ID)
           ocLstRowid    = ocLstRowid   + chr(4) + vcRowID.
                
    END. /* MAIN-BLOCK */

    /* ================================================================== */
    /* Handle errors: store if SaveAsDraft is needed                      */
    /* Check: only 1 draft is allowed for a single bus-component instance */
    /* ================================================================== */
    if vlSaveAsDraft and
        index(substring(ocLstReturn, r-index(ocLstReturn, chr(4)) + 1, -1, "CHARACTER":U), "-":U) <> 0 /* Negative return-status for this entry */
    then do:
        if vlDraftInstanceCreated = true
        then do:
            assign vcJournalMsg = trim(#T-31'Only one draft instance can be created per method call.':255(13856)T-31#) +
                                  chr(10) + 
                                  trim(#T-32'This second object cannot be stored as a draft instance and so is an error.':255(13879)t-32#)  
                   .
            <M-33 run SetMessage (input  vcJournalMsg (icMessage), 
                      input  '':U (icArguments), 
                      input  '':U (icFieldName), 
                      input  '':U (icFieldValue), 
                      input  'E':U (icType), 
                      input  1 (iiSeverity), 
                      input  '':U (icRowid), 
                      input  'QADFIN-3123':U (icFcMsgNumber), 
                      input  '' (icFcExplanation), 
                      input  '' (icFcIdentification), 
                      input  '' (icFcContext), 
                      output viFcReturnSuper (oiReturnStatus)) in BJournal>
            next.
        end.
   
        <M-14 run StoreState
          (input  '(':U + string(tJournal.JournalCode) + ') ':U + trim(#T-23'Daybook created using an API method.':200(2708)t-23#) (icDescription), 
           input  ? (icUIClass), 
           output viFcReturnSuper (oiReturnStatus)) in BJournal>
        assign vlDraftInstanceCreated = true
               entry(num-entries(ocLstReturn , chr(4)), ocLstReturn , chr(4)) = string(viFcReturnSuper)
               entry(num-entries(ocLstPrimKey, chr(4)), ocLstPrimKey, chr(4)) = string(tApiJournal.Journal_ID)
               entry(num-entries(ocLstRowid  , chr(4)), ocLstRowid  , chr(4)) = vcRowID.
    end. /*if vlSaveAsDraft and index*/   
end. /* for each tApiCode */

/* ================================================================= */
/* Stop queries                                                      */
/* ================================================================= */

<Q-17 run JournalPrim (Stop) in BJournal >

/* ============================================================ */
/* Trim the first character of ocLstReturn and primkey          */
/* Check for correct number and format of the return-parameters */
/* ============================================================ */
if length(ocLstReturn,"CHARACTER":U)  > 1 then assign ocLstReturn  = substring(ocLstReturn,2,-1,"CHARACTER":U).
if length(ocLstPrimKey,"CHARACTER":U) > 1 then assign ocLstPrimKey = substring(ocLstPrimKey,2,-1,"CHARACTER":U).
if length(ocLstRowid,"CHARACTER":U)   > 1 then assign ocLstRowid   = substring(ocLstRowid,2,-1,"CHARACTER":U).
if num-entries(ocLstReturn,chr(4))  <> viMainTableRecordCount or
   num-entries(ocLstPrimKey,chr(4)) <> viMainTableRecordCount or
   num-entries(ocLstRowid,chr(4))   <> viMainTableRecordCount
then do :
    assign vcJournalMsg   = trim(#T-24'Daybook integration: incomplete information returned by an API method. See other messages for further details.':200(2709)T-24#) + chr(10) + 
                            trim(substitute(#T-25'Main table records: &1.':255(310)T-25#,string(viMainTableRecordCount))) + chr(10) + 
                            trim(substitute(#T-26'Return Statuses: &1.':255(311)T-26#,ocLstReturn))  + chr(10) +
                            trim(substitute(#T-27'Primary keys: &1.':255(312)T-27#,ocLstPrimKey)) + chr(10) +
                            trim(substitute(#T-28'Row IDs: &1.':255(313)T-28#,ocLstRowid)).
           oiReturnStatus = -3.
   
    <M-6 run SetMessage (input  vcJournalMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2639':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BJournal>    
    return.
end. /* if num-entries */

/* ===================================================== */
/* Set global return status based on the individual ones */
/* ===================================================== */
if ocLstReturn = substring(fill("0":U + CHR(4),viMainTableRecordCount),1,length(ocLstReturn,"CHARACTER":U),"CHARACTER":U)
then assign oiReturnStatus = 0.
else do : 
    if index(ocLstReturn,"-":U) <> 0
    then do :
        assign vcJournalMsg      = trim(#T-29'An error has occurred while running this API method. See other error messages for more details.':255(301)T-29#)
               oiReturnStatus    = -1.

         <M-7 run SetMessage (input  vcJournalMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2640':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BJournal>         
    end. /* if index(ocLstReturn,"-":U) <> 0 */
    else do : 
        assign vcJournalMsg     = trim(#T-30'A warning has occurred while running this API method. See other error messages for more details.':255(302)T-30#)
               oiReturnStatus   = +1.
        <M-8 run SetMessage (input  vcJournalMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'W':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2641':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BJournal>
    end. /* not if lookup */
end. /* not if ocLstReturn */


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 = "BJournal".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "ApiStdMaintainTT".
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/bjournal.apistdmaintaintt.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icAction = <parameter value>.

vhParameter = vhInputDS:get-buffer-handle("tApiJournal").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

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