Versions Compared
Version | Old Version 10 | New Version 11 |
---|---|---|
Changes made by | ||
Saved on |
Key
- This line was added.
- This line was removed.
- Formatting was changed.
...
Creating a Batch for Processing
To create a batch for processing the Mez:createBatch
built-in function can be used:
Code Block | ||
---|---|---|
| ||
MezBatch stockUpdateBatch = Mez:createBatch(fileUpload._id, fileUpload.data); |
In this case fileUpload
represents an object instance containing a blob
attribute named data
.
The first parameter of Mez:createBatch represents the name of the batch. This can be any value that can be implicitly converted to a string. Note that there is no validation on uniqueness for this value meaning more than one batch can have the same name. If, however, batch creation and batch processing are done separately it makes sense that the batch should be uniquely identifiable. For this purpose we use the id of the fileUpload object instance to ensure uniqueness.
The second parameter represents the blob value that represents the CSV file to process. blob values can populated using, amongst other methods, the file upload widget, or the inbound API.
The above command will create entries in the database that looks as follows:
Code Block |
---|
Code Block |
---|
MezBatch and MezBatchItem Built-in Objects
The following two objects are used to store batches that are created by the use of the Mez:createBatch
built-in function. These objects are built-in objects meaning they are included, by Helium, as part of every Helium app:
Code Block | ||
---|---|---|
| ||
@NotTracked
persistent object MezBatch {
// User specified description of this batch
string name;
// String representing the headers for the CSV file associated with this batch
string header;
} |
Code Block |
---|
@NotTracked
persistent object MezBatchItem {
// Flag indicating whether this batch item has been processed with fromCsvLine
bool processed;
// A string representing an entire line of the CSV file associated with the batch
string value;
// Relationship to the built-in MezBatch object
@ManyToOne
MezBatch batch via batchItems;
} |
These objects can be queried like any other object in Helium using for example the native SQL statements provided by the DSL or using selectors.
Creating a Batch for Processing
To create a batch for processing the Mez:createBatch
built-in function can be used:
Code Block | ||
---|---|---|
| ||
MezBatch stockUpdateBatch = Mez:createBatch(fileUpload._id, fileUpload.data); |
In this case fileUpload
represents an object instance containing a blob
attribute named data
.
The first parameter of Mez:createBatch represents the name of the batch. This can be any value that can be implicitly converted to a string. Note that there is no validation on uniqueness for this value meaning more than one batch can have the same name. If, however, batch creation and batch processing are done separately it makes sense that the batch should be uniquely identifiable. For this purpose we use the id of the fileUpload object instance to ensure uniqueness.
The second parameter represents the blob value that represents the CSV file to process. blob values can populated using, amongst other methods, the file upload widget, or the inbound API.
The above command will create entries in the database that looks as follows:
Code Block |
---|
|
Code Block |
---|
|
Processing an Existing Batch
Once a batch has been created, it can be processed line for line using the fromCsvLine
built-in function:
Code Block |
---|
// Get the batch items related to the batch being processed
MezBatchItem stockUpdateBatchItems = MezBatchItem:relationshipIn(batch, stockUpdateBatch);
// Results from CSV processing will be stored in this collection
StockUpdate[] results;
// Iterate over the batch items and process one by one using fromCsvLine
for(MezBatchItem stockUpdateBatchItem: stockUpdateBatchItems) {
StockUpdate stockUpdate = StockUpdate:fromCsvLine(stockUpdateBatch.header, stockUpdateBatchItem.value);
results.append(stockUpdate);
stockUpdateBatchItem.processed = true;
} |
To retrieve the batch items associated with the batch being processed, we make use of the relationship between the two objects.
The first parameter of fromCsvLine represents the header string of the CSV file associated with our batch. This can be retrieved from the MezBatchItem object instance being processed.
Exception Handling and Debugging
Additional Mentions and References
Description
Whereas standard CSV processing in Helium processes an entire blob, representing a CSV file, at once to produce a collection of object instances, the batch CSV processing features takes a multi-step approach:
- First the
Mez:createBatch
built-in function is used to create a batch from a blob representing a valid CSV file. - The built-in
MezBatch
andMezBatchItem
objects are used to store the created batch. - The created batch can then be processed item by item using the
fromCsvLine
built-in function.
Processing an Existing Batch
Once a batch has been created, it can be processed line for line using the fromCsvLine
built-in function:
Code Block |
---|
// Get the batch items related to the batch being processed
MezBatchItem stockUpdateBatchItems = MezBatchItem:relationshipIn(batch, stockUpdateBatch);
// Results from CSV processing will be stored in this collection
StockUpdate[] results;
// Iterate over the batch items and process one by one using fromCsvLine
for(MezBatchItem stockUpdateBatchItem: stockUpdateBatchItems) {
StockUpdate stockUpdate = StockUpdate:fromCsvLine(stockUpdateBatch.header, stockUpdateBatchItem.value);
results.append(stockUpdate);
// Once a batch item has been successfully processed it can be marked as such
stockUpdateBatchItem.processed = true;
} |
To retrieve the batch items associated with the batch being processed, we make use of the relationship between the two objects.
The first parameter of fromCsvLine
represents a string representing the header of the CSV file being processed. This can be retrieved from the MezBatch
object instance as shown.
The second parameter of fromCsvLine
represents a string representing the current line of the CSV file being processed. This can be retrieved from the MezBatchItem
object instance as shown.
Exception Handling and Debugging
Seeing as each line of a CSV file is processed separately we can make use of the Helium exception handling functionality to continue processing lines of the CSV file even if one or more lines fail. Additional follow-up tasks can then be created to manually process or address lines in the CSV file that could not be parsed.
The above example with exception handling included might look something like this: