FoodBroker project: Manage rates

So far in your project you have completed the MB_extractor DML Block: the Decision-Path and the two Maps are completely filled. All the nodes of all the output Business Documents are generated with the appropriate number of occurrences and values.

The only remaining task is the partner rates feeding. If you refer back to chapter Introduction to DML, you’ll see that this feeding involves a network of DML Blocks.

The first DML Block (MB_tableLoader) is activated by a start event that is triggered each time a new message is fed to the DML Block input. The DML Block loads the XML file that contains the rates.

For this DML Block you need to define only a Decision-Path that activates a Map. The task of the Map is to load the file into the V class node of the output.

This Map uses the LoadFile Custom Function to accomplish the loading task. Basically, this Custom Function opens a file, places the file content into the returned V value, and then closes the file. If the file is not found, the Custom Function returns absent.

Custom Function example

The following shows how the C code performs this task:

int LoadFile(EXE_HANDLE hCtx,
             EXE_hRegister hFileContents,
             EXE_hRegister hFileName)
{
char szFileName[MAXPATH + 1];
FILE *pFile;
char *pBuffer;
size_t ulSize;

    /* Reject calls with an empty filename */
    if (xibrt_isNull(hCtx, hFilename) || xibrt_isNull
       (hCtx, hFilename))
    {
        xibrt_setNull(hCtx, hFileContents);
        return XIBRT_SUCCESS;
    }
    /* Extract filename arguments
     * The contents of hFileName is cloned into a static buffer
     * theoricaly large enough to contain the whole filename.
     *Only a loss of data is possible here (truncated filename)
     *that occurs only when the filename is not filesystem

     *compatible.
     */
    xibrt_getString(hCtx, hFileName, sizeof(szFilename),
      szFilename);

    /* Try to open the file and return ‘absent’ on failure */
    pFile = fopen(szFilename, "r");
    if (!pFile)
    {
        xibrt_setAbsent(hCtx, hFileContents);
        return XIBRT_SUCCESS;
    }

    /* Collect file size and allocate room for contents,
       then load the file into the buffer and close it */
    ulSize = fseek(pFile, 0, SEEK_END);
    fseek(pFile, 0, SEEK_SET);
    pBuffer = (char *) malloc(ulSize);
    if (!pBuffer)
    {
        xibrt_setAbsent(hCtx, hFileContents);
        return XIBRT_SUCCESS;
    }
    ulSize = fread(pBuffer, 1, ulSize, pFile);
    fclose(pFile);

    /* Copy the file contents into the result blob */
    xibrt_setBinary(hCtx, ulSize, pBuffer, hFileContents);

    /* Perform some cleanup and return success */
    free(pBuffer);
    return XIBRT_SUCCESS;
}

After you’ve compiled this code compiled and defined the Custom Function, the function is operational and can be used at runtime to load your file. The second stage of the feeding is accomplished by another DML Block (MB_tableBuilder) that parses the XML file and generates the corresponding Business Document.

In the Decision-Path of the MB_tableBuilder DML Block you create a parse step that generates a BD_partnerRates Business Document instance from the rawdata node of the input Business Document:

Creating a parse step

Rawdata node

Now, your MB_extractor DML Block is correctly fed with the partner rates. The project is complete.

Related topics

FoodBroker project: Do the first steps

Create a Business Document for the customer request

Create a Business Document for the partner

Create a Business Document for partner rates

Build the Mapping Flow

FoodBroker project: Fill headers

FoodBroker project: Determine the incoming order ID

FoodBroker project: Generate the order ID

FoodBroker project: First steps in filling nodes

FoodBroker project: Extra charge for VISA payments

FoodBroker project: Introduction to data aggregation

FoodBroker project: Collect and copy parts

FoodBroker project: Manage rates

FoodBroker project: Handle absence of partner rates

Related Links