Prev Up Next

Formatting files with Velocity

The class FileToVelocity can format a file/files in a directory using Velocity. There are examples of its use in XmplFileToVelocity1.

Methods in FileToVelocity

   1:    /**
   2:     * This method will format all files (Record Oriented Files)
   3:     * in a directory a  using a Velocity Template
   4:     *
   5:     * @param layout Record layout of the file
   6:     * @param lineProvider line provider to create the lines.
   7:     *        note use null for the standard LineProvider
   8:     * @param records Records to be sent to the supplied Template.
   9:     *        This parameter allows you select which records to
  10:     *        format with the Template
  11:     * @param inputDirectory input directory to process
  12:     * @param filter filename filter used to select which files
  13:     *        to format
  14:     * @param template Velocity template to format the files with
  15:     * @param outputFile output filename that is to be written
  16:     */
  17:    public void processDirectory(LayoutDetail layout,
  18:                        LineProvider lineProvider,
  19:                        int[] records,
  20:                        String inputDirectory,
  21:                        FilenameFilter filter,
  22:                        String template,
  23:                        String outputFile)
  24:
  25:    /**
  26:     * process a Record Oriented File using a Velocity Template
  27:     *
  28:     * @param layout Record layout of the file
  29:     * @param lineProvider line provider to use
  30:     * @param records Records to be sent to the supplied Template.
  31:     *        This parameter allows you select which records to
  32:     *        format with the Template
  33:     * @param inputFile input file to process
  34:     * @param template Velocity template to use
  35:     * @param outputFile output filename
  36:     */
  37:    public void processFile(LayoutDetail layout,
  38:                        LineProvider lineProvider,
  39:                        int[] records,
  40:                        String inputFile,
  41:                        String template,
  42:                        String outputFile)
  43:
  44:
  45:
  46:

Calling FileToVelocity

The following is an example of using FileToVelocity (taken from XmplFileToVelocity1).

   1:
   2:        FileToVelocity fileToVelocity = new FileToVelocity(
   3:                          Constants.SOURCE_RESOURCE,
   4:                          Constants.VELOCITY_OUTPUT_DIR);
   5:
   6:        EdiPo.setCopyBook(copybookInterace);
   7:
   8:        int[] useEdiPO   = {EdiPo.getIvr0075hIndex()};
   9:
  10:        FilenameFilter filter = new FilenameFilter() {
  11:            public boolean accept(File f, String filename) {
  12:                return filename.startsWith("tgtorders");
  13:            }
  14:        };
  15:
  16:        fileToVelocity.processDirectory(
  17:                copybookInterace.getLayout("EDI PO"),
  18:                new EdiPoProvider(),
  19:                useEdiPO,
  20:                backupDir + "edi\\",
  21:                filter,
  22:                "EdiPoHtml",
  23:                "c:/t/EdiPO.html");
  24:

Sample Velocity Templates

If you use a LineIOprovider and your own Line class, you can use Getter's & Setter's to access the various fields.

   1:##
   2:##  Generate HTML for a EDI PO file
   3:## 
   4:
   5:  
   6:<table CELLPADDING="3">
   7:  <TR><TH BGCOLOR=blue><font color=white><b>&nbsp;&nbsp;$fileName&nbsp;&nbsp;</b></font></TH></TR>
   8:</table>
   9:&nbsp;
  10:<table border="1">
  11:  <TR>
  12:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">DC</TH>
  13:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">Department</TH>
  14:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">Purchase Order</TH>
  15:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">Before Date</TH>
  16:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">After Date</TH>
  17:  </TR>
  18:#foreach( $rec in $records )
  19:  <tr>
  20:      <td>${rec.DcNo}</td><td>${rec.DeptNo}</td><td>${rec.PoNum}</td>
  21:      <td>${rec.BeforeDate}</td><td>${rec.AfterDate}</td>
  22:  </tr>
  23:#end
  24:</table>
  25:

Alternatively if you use the generic Line class, you must use the generic getField to get a fields value:

   1:##
   2:##  Generate HTML for a EDI PO file
   3:##
   4:
   5:#set ( $IdxReceiptRh = ${layout.getRecordIndex("ams Receipt RH Receipt Header")} )
   6:###set ( $IdxReceiptRh = 1 )
   7:#set ( $IdxPO        = ${layout.getRecord(${IdxReceiptRh}).getFieldIndex("Order No Rh")} )
   8:#set ( $IdxSupplier  = ${layout.getRecord(${IdxReceiptRh}).getFieldIndex("Sup Id Rh")} )
   9:#set ( $IdxRctDate   = ${layout.getRecord(${IdxReceiptRh}).getFieldIndex("Receipt Date Rh")} )
  10:#set ( $IdxRctTime   = ${layout.getRecord(${IdxReceiptRh}).getFieldIndex("Receipt Time Rh")} )
  11:#set ( $IdxDC        = ${layout.getRecord(${IdxReceiptRh}).getFieldIndex("Brand Dc No Rh")} )
  12:
  13:###Order Index ~ $IdxReceiptRh ~ $IdxPO $IdxSupplier $IdxRctDate $IdxRctTime $IdxDC 
  14:
  15:&nbsp;&nbsp;
  16:<table CELLPADDING="3">
  17:  <TR><TH BGCOLOR=blue><font color=white><b>&nbsp;&nbsp;$fileName&nbsp;&nbsp;</b></font></TH></TR>
  18:</table>
  19:&nbsp;
  20:<table border="1">
  21:  <TR>
  22:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">DC</TH>
  23:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">Purchase Order</TH>
  24:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">Supplier</TH>
  25:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">Receipt Date</TH>
  26:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">Receipt Time</TH>
  27:  </TR>
  28:
  29:#foreach( $rec in $records )
  30:  <tr>
  31:    <td>${rec.getField(${IdxReceiptRh}, ${IdxDC})}</td>
  32:    <td>${rec.getField(${IdxReceiptRh}, ${IdxPO})}</td>
  33:    <td>${rec.getField(${IdxReceiptRh}, $IdxSupplier)}</td>
  34:    <td>${rec.getField(${IdxReceiptRh}, $IdxRctDate)}</td>
  35:    <td>${rec.getField(${IdxReceiptRh}, $IdxRctTime)}</td>
  36:  </tr>
  37:#end
  38:</table>
  39:<p>&nbsp;<p>

Finally you can also use getField(String fieldname) of Line class to retrieve field values:

   1:##
   2:##  Generate HTML for a EDI PO file
   3:##
   4:
   5:&nbsp;&nbsp;
   6:<table CELLPADDING="3">
   7:  <TR><TH BGCOLOR=blue><font color=white><b>&nbsp;&nbsp;$fileName&nbsp;&nbsp;</b></font></TH></TR>
   8:</table>
   9:&nbsp;
  10:<table border="1">
  11:  <TR>
  12:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">DC</TH>
  13:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">Purchase Order</TH>
  14:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">Supplier</TH>
  15:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">Receipt Date</TH>
  16:    <TH ALIGN="LEFT" VALIGN="TOP" BGCOLOR="#DADADA">Receipt Time</TH>
  17:  </TR>
  18:
  19:#foreach( $rec in $records )
  20:  <tr>
  21:    <td>${rec.getField("Brand Dc No Rh")}</td>
  22:    <td>${rec.getField("Order No Rh")}</td>
  23:    <td>${rec.getField("Sup Id Rh")}</td>
  24:    <td>${rec.getField("Receipt Date Rh")}</td>
  25:    <td>${rec.getField("Receipt Time Rh")}</td>
  26:  </tr>
  27:#end
  28:</table>
  29:<p>&nbsp;<p>

Prev Up Next