CSVSourceConverter Class (Extends SourceConverter)

Overview

The CSVSourceConverter, then, is the main driver for the actual conversion. Here are its attributes and methods. It inherits all the attributes and methods of its base classes, the SourceConverter and Converter classes (see Chapter 6).

Attributes:

  • CSVRecordReader Object

  • Array of Strings Partner Array

  • String Schema Location URL

  • Integer Partner Break Column

  • String Saved Partner ID

  • Integer Document Break Column

  • String Saved Document ID

Methods:

  • Constructor

  • processFile

  • testDocumentBreak

  • testPartnerBreak

Methods

Constructor

The constructor method for our CSVSourceConverter object sets up that object, but most importantly it also sets up the CSVRecordReader object. Here's the logic for it.

Logic for the CSVSourceConverter Constructor Method
Arguments:
  Boolean Validation Option
  String Output Directory Name
  String File Description Document Name

Call base class constructor
Initialize Partner Array
Call loadFileDescriptionDocument using passed File
    Description Document Name
Schema Location URL <- Call File Description Document's
    getElementsByTagName on "SchemaLocationURL", then
    getAttribute on "value"
IF Schema Location URL is null and Validation is true
  Throw Exception
ENDIF
Document Break Column <- Get Document Break Column value from File
    Description Document
Partner Break Column <- Get Partner Break Column value from File
    Description Document
Initialize Saved Partner ID and Saved Document ID
Create CSVRecordReader object, passing:
    File Description Document

processFile

After the initialization performed in the constructor method, the main processing is performed by the CSVSourceConverter's processFile method. This method converts one input CSV file into one or more output XML documents based on the input parameters.

Logic for the CSVSourceConverter processFile Method
Arguments:
  String Name of input file

Returns:
  Status or throws exception

Output Directory Path <- Base Directory + directory separator
Initialize Output Document to null
Initialize Sequence Number
Row Grammar <- Get RowDescription from Grammar Element via
    getElementsByTagName, and item[0] from NodeList
Open input file
Call CSVRecordReader's setInputStream method
Record Length _<- Call CSVRecordReader's
    readRecordVariableLength method
DO while Record Length => 0
  Call CSVRecordReader's parseRecord method to parse columns into
      DataCell Array, passing Row Grammar Element
  Call CSVRecordReader's toXMLType method to convert column contents
      in DataCell Array to their schema language datatype
      representation
  Partner Break <- Call testPartnerBreak
  IF Partner Break = true
    Output Directory Path <- Base Directory +
        Partner ID + directory separator
    Lookup Partner in Partner Array
    IF Partner is not in Array
      Create output directory from Output Directory Path
      Partner Array <- Add Partner ID
    ENDIF
  ENDIF
  Document Break <- Call testDocumentBreak
  IF Document Break = true or Output Document is null
    IF Output Document exists
      Call saveDocument
    ENDIF
    Create new Output Document
    Create Root Element, using Root Element Name from Grammar,
        and append to Output Document
    IF Schema Location URL is not NULL
      Create noNamespaceSchemaLocation Attribute and append
          to Root Element
    ENDIF
    Increment Sequence Number, and pad with leading zeroes
        to three digits
    IF Document Break Column != 0
      Output File Path <- Output Directory Path + Root Element
          Name + Sequence Number + ".xml"
    ELSE
      Output File Path <- Output Directory Path + Root Element
          Name + ".xml"
    ENDIF
    Call CSVRecordReader's setOutputDocument method for new
        Output Document
  ENDIF
  Call CSVRecordReader's writeRecord method to write row and column
      Elements from CSVRecordReader's DataCall Array, passing
      Root Element of Output Document and Row Grammar Element
  Record Length <- Call CSVRecordReader's readRecord method
ENDDO
IF Output Document is not null
  Call base class saveDocument method
ENDIF
Close input file
Display completion message with number of documents processed

You'll note that we check for a new partner or document after converting to XML formats via the toXML method. We could check before converting to XML, but performing the conversion first ensures that we are working with data that is not only in string format but also normalized by removing leading and trailing whitespace. The testDocumentBreak and testPartnerBreak methods do the checks for us. Note that if no break is specified for new documents, we unconditionally return false for break on new partner.

testDocumentBreak

This method tests the value of the document break column to determine whether the current row starts a new document.

Logic for the CSVSourceConverter testDocumentBreak Method
Arguments:
  None

Returns:
  Boolean - true if new partner and false if not

IF Document Break Column is zero
  return false
ENDIF
DocumentID <- Call CSVRecordReader's getFieldValue method
    passing the Document Break Column number
IF DocumentID = Saved Document ID
  return false
ENDIF
Saved Document ID <- Document ID
Return true

testPartnerBreak

This method tests the value of the partner break column to determine whether the trading partner in the current row is different from that of the preceding document.

Logic for the CSVSourceConverter testPartnerBreak Method
Arguments:
  None

Returns:
  Boolean - true if new partner and false if not

IF Partner Break Column is zero or if Document Beak Column
    is zero
  return false
ENDIF
Partner ID <- Call CSVRecordReader's getFieldValue method
    passing the Partner Break Column number
IF Partner ID = Saved Partner ID
  return false
ENDIF
Saved Partner ID <- Partner ID
Return true

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset