Tutorials > Program model > Customize the Data Load utility

< Previous | Next >


Create a custom data reader

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

The Data Load utility already provides you with a CSV data reader, which is implemented by the 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, implement the DataReader interface. The 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:


Procedure

  1. Extract the DataLoadSample.zip file to a temporary directory on the 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 DataLoadBusinessObject. It then returns the DataLoadBusinessObject.

      The returned data will be passed to the business object builder to generate business objects, and each business object will be 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 generates 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);
      

      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.

< Previous | Next >


+

Search Tips   |   Advanced Search