Creating a custom data reader

In this lesson, you create a custom data reader to read your XML input file and then transform the input data into business objects.

About this task

The Data Load utility already provides you with a CSV data reader, which is implemented by the com.ibm.commerce.foundation.dataload.datareader.CSVReader class. It is necessary to create custom data readers in order to support data source of types other than CSV files. This tutorial shows you how to create a custom data reader to support the XML file that you created (warranty.xml). A DOM parser is used to parse the XML file to a DOM tree, and then build a commerce business object.

To create a custom data reader, you need to implement the com.ibm.commerce.foundation.dataload.datareader.DataReader interface. The com.ibm.commerce.foundation.dataload.datareader.AbstractDataReader class implements most methods in the DataReader interface. The custom data reader class can extend this class and provide the additional implementation for the next(), getSourcePosition(), and init() methods.

The following Java classes are provided:
  • WarrantyReader.java: This class implements the default behavior of the DataReader interface. It reads the XML input file, and then creates the business object.
  • WarrantyConstants.java: This class defines the constants that are used in parsing the XML file.

Procedure

  1. Extract the DataLoadSample.zip file to a temporary directory on your machine.
  2. Open WebSphere Commerce Developer.
  3. In the Enterprise explorer view, right-click the WebSphereCommerceServerExtensionsLogic folder and click New > Package.
  4. In the Name field, enter com.mycompany.commerce.dataload.reader and click Finish.
  5. Right-click the com.mycompany.commerce.dataload.reader package, and click Import > General > File system.
  6. Click Next; then click Browse and navigate to the Dataload_Temp directory, where Dataload_Temp is the location where you extracted the sample Java files.
    Select the WarrantyReader.java file and WarrantyConstants.java file. Click Finish to import the files.
  7. Open the WarrantyReader.java file and examine its content.
    The following methods are implemented:
    • init(): This method initializes the data reader. It gets the input XML file location from the configuration file, and then reads the XML file into the DOM tree object.
    • next(): This method parses the XML DOM tree object, builds the next logical object, and encapsulates it within a com.ibm.commerce.foundation.dataload.object.DataLoadBusinessObject. It then returns the DataLoadBusinessObject.

      The returned data is passed to the business object builder to generate business objects. Each business object is passed to the corresponding business object mediator to process the data.

      In this tutorial, a business object builder is not needed because the data reader is creating the business objects, which are encapsulated in the DataLoadBusinessObject.

      For example, the following code snippets generate SDO for the attribute part number:
      // Get partNumber
      				attrvalue = ((Element) node)
      						.getAttribute(WarrantyConstants.PART_NUMBER);
      				if (attrvalue != null) {
      					attrvalue = attrvalue.trim();
      				}
      				if (LOGGER.isLoggable(Level.FINER)) {
      					LOGGER.logp(Level.FINER, CLASSNAME, METHODNAME,
      							WarrantyConstants.PART_NUMBER + ":" + attrvalue);
      				}
      				CatalogEntryType catentry = CatalogFactory.eINSTANCE
      						.createCatalogEntryType();
      				CatalogEntryIdentifierType identifier = CommerceFoundationFactory.eINSTANCE
      						.createCatalogEntryIdentifierType();
      				PartNumberIdentifierType partNumber = CommerceFoundationFactory.eINSTANCE
      						.createPartNumberIdentifierType();
      				identifier.setExternalIdentifier(partNumber);
      				catentry.setCatalogEntryIdentifier(identifier);
      				partNumber.setPartNumber(attrvalue);
      If you want to know more about generating SDOs, see Service Data Objects (SDO).
    • getSourcePosition(): This method returns the source position of the data object returned by the next() method last called.
    • parseWarranty(Node child,CatalogEntryType catentry): This method is called by the next() method. The method parses the warranty element and its subelements. It then populates the appropriate attributes of the logical object.
    • parseDescription(Node child,CatalogEntryType catentry): This method is called by the next() method. The method parses the description element and its subelements. It then populates the appropriate attributes of the logical object.
    • parseCatentryAttribute(Node child,CatalogEntryType catentry): This method is called by the next() method. The method parses the CatentryAttribute element and its subelements. It then populates the appropriate attributes of the logical object.