project QadFinancials > class BExchangeRate > method ApiStdMaintainTT

Description

With this method an Exchange Rate can be added, modified or deleted.


Parameters


tApiExchangeRateinputtemp-tableTemp table like exchange rate.
icActioninputcharacterAction Code.
ocLstPrimKeyoutputcharacterThe list of Prim Key value, seperated by chr(4).
ocLstReturnoutputcharacterThe list of Return Status, serperated by chr(4).
ocLstRowidoutputcharacterThe list of Row Id, serperated by chr(4).
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program7/bexchangerate.p)

/* ========================================================= */
/* Method Info:                                              */
/* ExchangeRate 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 BExchangeRate>

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 vcExchangeRateMsg   = trim(subst(#T-39'An exchange rate integration error occurred. The specified action (&1) must be in the list '&2'.':255(1885)t-39#,trim(icAction),"{&DAEMONACTION-SAVESTORE},{&DAEMONACTION-SAVE},{&DAEMONACTION-STORE},{&DAEMONACTION-VALIDATE}":U))
           oiReturnStatus      = -1.
           <M-2 run SetMessage (input  vcExchangeRateMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2623':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BExchangeRate>                           
    return.
end. /* if icAction */

/* ======================== */
/* Count main-table records */
/* ======================== */
for each tApiExchangeRate no-lock :
    assign viMainTableRecordCount = viMainTableRecordCount + 1.
end. /* for each */
if viMainTableRecordCount = 0
then do:
    assign vcExchangeRateMsg    = trim(#T-40'An exchange rate integration error occurred. The system could not find main table records (exchange rate).':255(1886)t-40#)
           oiReturnStatus       = -1.
    
           <M-14 run SetMessage (input  vcExchangeRateMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2622':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BExchangeRate>    
    return.
end. /* if viMainTableRecordCount */

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

<Q-3 run ExchangeRatePrim (Start) in BExchangeRate >

/* ================================ */
/* Start loop on main-table records */
/* ================================ */
for each tApiExchangeRate by tApiExchangeRate.tc_Rowid: 

    MAIN-BLOCK:
    DO:

    /* ========== */
    /* Initialize */
    /* ========== */
    assign vcExchangeRateMsg   = "":U
           vcRowID             = "":U
           vlWarningsFound     = false
           vlSaveAsDraft       = icAction = {&DAEMONACTION-STORE}.

    /* ================================================ */
    /* Get the ExchangeRate_ID based on the logical key */
    /* ================================================ */
    if (tApiExchangeRate.ExchangeRate_ID           = ?   or
        tApiExchangeRate.ExchangeRate_ID           = 0)  and
        tApiExchangeRate.tcExchangeRateTypeCode    <> "" and
        tApiExchangeRate.tcExchangeRateTypeCode    <> ?  and
        tApiExchangeRate.ExchangeRateValidDateFrom <> ?  and
        tApiExchangeRate.tcFromCurrencyCode        <> "" and
        tApiExchangeRate.tcFromCurrencyCode        <> ?  and
        tApiExchangeRate.tcToCurrencyCode          <> "" and
        tApiExchangeRate.tcToCurrencyCode          <> ?
    then do:
        <Q-4 run ExchangeRatePrim (all) (Read) (NoCache)
          (input tApiExchangeRate.ExchangeRateValidDateFrom, (ValidDateFrom)
           input tApiExchangeRate.FromCurrency_ID, (FromCurrencyId)
           input tApiExchangeRate.ToCurrency_ID, (ToCurrencyId)
           input tApiExchangeRate.ExchangeRateType_ID, (ExchangeRateTypeId)
           input ?, (ExchangeRateId)
           input viCompanyId, (CompanyId)
           input tApiExchangeRate.tcFromCurrencyCode, (FromCurrencyCode)
           input tApiExchangeRate.tcToCurrencyCode, (ToCurrencyCode)
           input tApiExchangeRate.tcExchangeRateTypeCode, (ExchangeRateTypeCode)
           output dataset tqExchangeRatePrim) in BExchangeRate >

     find tqExchangeRatePrim where 
             tqExchangeRatePrim.ttExchangeRateValidDateFrom = tApiExchangeRate.ExchangeRateValidDateFrom    and
             tqExchangeRatePrim.tcFromCurrencyCode          = tApiExchangeRate.tcFromCurrencyCode           and
             tqExchangeRatePrim.tcToCurrencyCode            = tApiExchangeRate.tcToCurrencyCode             and             
             tqExchangeRatePrim.tcExchangeRateTypeCode      = tApiExchangeRate.tcExchangeRateTypeCode          
             no-error.
     if available tqExchangeRatePrim
     then assign tApiExchangeRate.ExchangeRate_ID = tqExchangeRatePrim.tiExchangeRate_ID.
    end.

    /* ========================= */
    /* Load/Create ExchangeRate  */
    /* ========================= */

    if tApiExchangeRate.ExchangeRate_ID = ? or
       tApiExchangeRate.ExchangeRate_ID = 0
    then assign viFcReturnSuper = -4.
    else do:
        <M-5 run DataLoad (input  '':U (icRowids), 
                   input  string(tApiExchangeRate.ExchangeRate_ID) (icPkeys), 
                   input  ? (icObjectIds), 
                   input  ? (icFreeform), 
                   input  false (ilKeepPrevious), 
                   output viFcReturnSuper (oiReturnStatus)) in BExchangeRate>        
    end.
    if viFcReturnSuper = -4
    then do:
        assign viFcReturnSuper = 0.
        <M-16 run DataNew (output viFcReturnSuper (oiReturnStatus)) in BExchangeRate>
                        
        if viFcReturnSuper < 0
        then assign vcExchangeRateMsg = trim(subst(#T-41'Cannot create a new instance. Error number: &1.':255(308)t-41#, string(viFcReturnSuper))).

        find first tExchangeRate no-error.
    end.
    else do:
        if viFcReturnSuper < 0
        then assign vcExchangeRateMsg = trim(subst(#T-42'Cannot load the instance (error number: &1, instance ID: &2).':255(289)t-42#, string(viFcReturnSuper), string(tApiExchangeRate.ExchangeRate_ID))).
        else do:
            find tExchangeRate where
                 tExchangeRate.ExchangeRate_ID = tApiExchangeRate.ExchangeRate_ID
                 no-error.
            if not available tExchangeRate
            then assign vcExchangeRateMsg = trim(subst(#T-43'Cannot find the instance after it has been loaded (instance ID: &1).':255(290)t-43#,
                                               string(tApiExchangeRate.ExchangeRate_ID))).
            else assign tExchangeRate.tc_Status = "C":U.
        end.
    end.

    /* ====================================== */
    /* Check for errors in the previous block */
    /* ====================================== */
    if vcExchangeRateMsg <> "":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-6 run SetMessage (input  vcExchangeRateMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2624':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BExchangeRate> 
               
        leave MAIN-BLOCK.
    end.

    /* =========================== */
    /* Copy data for ExchangeRate  */
    /* =========================== */          
    
    buffer-copy tApiExchangeRate
         except tApiExchangeRate.ExchangeRate_ID
                tApiExchangeRate.tc_ParentRowid
                tApiExchangeRate.tc_Rowid
                tApiExchangeRate.tc_Status     
                tApiExchangeRate.ExchangeRateValidDateFrom
                tApiExchangeRate.tcExchangeRateTypeCode
                tApiExchangeRate.tcFromCurrencyCode
                tApiExchangeRate.tcToCurrencyCode
                tApiExchangeRate.SharedSet_ID
             to tExchangeRate.
    if tExchangeRate.tc_Status = 'N':U
    then assign tExchangeRate.tcExchangeRateTypeCode    = tApiExchangeRate.tcExchangeRateTypeCode
                tExchangeRate.ExchangeRateValidDateFrom = tApiExchangeRate.ExchangeRateValidDateFrom
                tExchangeRate.tcFromCurrencyCode        = tApiExchangeRate.tcFromCurrencyCode
                tExchangeRate.tcToCurrencyCode          = tApiExchangeRate.tcToCurrencyCode.
    
    assign vcRowID                          = tExchangeRate.tc_Rowid
           tApiExchangeRate.ExchangeRate_ID = tExchangeRate.ExchangeRate_ID.

    
    /* =================================================== */
    /* 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 tExchangeRate where
			           tExchangeRate.tc_Status = "N":U)
	then assign vcActivityCode = "Create":U.
	else if can-find (first tExchangeRate where
				            tExchangeRate.tc_Status = "C":U)
	then assign vcActivityCode = "Modify":U.

    /* ================= */
    /* Validate the data */
    /* ================= */
    <M-7 run ValidateBC (output viFcReturnSuper (oiReturnStatus)) in BExchangeRate>
       
    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-8 run AdditionalUpdates (output viFcReturnSuper (oiReturnStatus)) in BExchangeRate>
            
    if viFcReturnSuper < 0
    then do:
        assign ocLstReturn   = ocLstReturn  + chr(4) + string(viFcReturnSuper)
               ocLstPrimKey  = ocLstPrimKey + chr(4) + "*":U
               ocLstRowid    = ocLstRowid   + chr(4) + "*":U
               vlSaveAsDraft = 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-9 run DataSave (output viFcReturnSuper (oiReturnStatus)) in BExchangeRate>
                                
        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(tApiExchangeRate.ExchangeRate_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 vcExchangeRateMsg = trim(#T-52'Only one draft instance can be created per method call.':255(13856)T-52#) +
                                       chr(10) +
                                       trim(#T-53'This second object cannot be stored as a draft instance and so is an error.':255(13879)t-53#).
            <M-54 run SetMessage (input  vcExchangeRateMsg (icMessage), 
                      input  '':U (icArguments), 
                      input  '':U (icFieldName), 
                      input  '':U (icFieldValue), 
                      input  'E':U (icType), 
                      input  1 (iiSeverity), 
                      input  '':U (icRowid), 
                      input  'QADFIN-3203':U (icFcMsgNumber), 
                      input  '' (icFcExplanation), 
                      input  '' (icFcIdentification), 
                      input  '' (icFcContext), 
                      output viFcReturnSuper (oiReturnStatus)) in BExchangeRate>
            next.
        end.
        <M-15 run StoreState (input  vcStoreStateMsg (icDescription), 
                      input  ? (icUIClass), 
                      output viFcReturnSuper (oiReturnStatus)) in BExchangeRate>
        assign vlDraftInstanceCreated                                         = true
               entry(num-entries(ocLstReturn , chr(4)), ocLstReturn , chr(4)) = string(viFcReturnSuper)
               entry(num-entries(ocLstPrimKey, chr(4)), ocLstPrimKey, chr(4)) = string(tApiExchangeRate.ExchangeRate_ID)
               entry(num-entries(ocLstRowid  , chr(4)), ocLstRowid  , chr(4)) = vcRowID.        
    end.    
end. /* for each tApiCode */

/* ============ */
/* Stop queries */
/* ============ */
<Q-10 run ExchangeRatePrim (Stop) in BExchangeRate >

/* ============================================================ */
/* 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 vcExchangeRateMsg   = trim(#T-45'An exchange rate integration error occurred. An API method returned incomplete information. See other messages for further details.':200(1888)t-45#) + chr(10) + 
                            trim(substitute(#T-46'Main table records: &1.':255(310)T-46#,string(viMainTableRecordCount))) + chr(10) + 
                            trim(substitute(#T-47'Return Statuses: &1.':255(311)T-47#,ocLstReturn))  + chr(10) +
                            trim(substitute(#T-48'Primary keys: &1.':255(312)T-48#,ocLstPrimKey))    + chr(10) +
                            trim(substitute(#T-49'Row IDs: &1.':255(313)T-49#,ocLstRowid)).
           oiReturnStatus = -3.
   
    <M-11 run SetMessage (input  vcExchangeRateMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2619':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BExchangeRate> 
    
    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 vcExchangeRateMsg = trim(#T-50'An error has occurred while running this API method. See other error messages for more details.':255(301)T-50#)
               oiReturnStatus    = -1.


         <M-12 run SetMessage (input  vcExchangeRateMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  1 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2620':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BExchangeRate> 
         
    end. /* if index(ocLstReturn,"-":U) <> 0 */
    else do : 
        assign vcExchangeRateMsg = trim(#T-51'A warning has occurred while running this API method. See other error messages for more details.':255(302)T-51#)
               oiReturnStatus    = +1.
        <M-13 run SetMessage (input  vcExchangeRateMsg (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'W':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-2621':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BExchangeRate> 
    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 = "BExchangeRate".
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/bexchangerate.apistdmaintaintt.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icAction = <parameter value>.

vhParameter = vhInputDS:get-buffer-handle("tApiExchangeRate").
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.