Retrieving and Processing Target Items

To allow you to report statuses for each item, cycle through your Target Items and process each item individually.

The TargetItem class provides helper methods that allow for the lookup of dynamic exportCodes during the publication phase, at the level of TargetItemMetadata and TargetAttributeDefinition. Keep the adapter flexible by relying on the TargetItem and target extension XML exportCode properties. The properties make the export execution generic for a number of different target item types.

TargetItem exposes the following APIs:

  • public String getExportCode() - Get the individual exportCode of the item type defined in the extension.
  • public Optional<String> getExportCodeAttributeByName(final String attributeName) - Retrieve a SpEL-resolved (exportCode) attribute value by its name.

Depending on your target system, however, you may be forced to sequence the export order for lookup tables and similar items. To reduce the number of items loaded into memory, Data Hub uses a paging concept for retrieving target items by target item type. The PublicationActionService can then be queried for target items by type, while providing a "paging" context until all eligible items have been retrieved.

for (TargetItemMetadata itemMetadata : targetSystemPublication.getTargetSystem().getTargetItemMetadata())
{
    final String targetItemType = itemMetadata.getItemType();
    int lastProcessedId = 0;
    List<? extends TargetItem> items;

        final DataHubPageable pageable = new DefaultDataHubIdBasedPageRequest(1000, lastProcessedId);
        DataHubPage<? extends TargetItem> page = publicationActionService.findByPublication(targetSystemPublication.getPublicationId(), targetItemType, pageable);
        items = page.getContent();
        for (final TargetItem targetItem : items)
        {
            // process actual target item
        }
}

In this sample code, you first construct a list of target items by item type. For each item type, you then paginate the results for processing, with 1000 items per page. Looping through this page of target items then allows you to process each item individually.

What's next?

Since each item is processed, be sure to return any errors to Data Hub so that the correct Canonical Item publication statuses are set. Error Handling is covered in the topic that follows.