Prev  Up  Next

Running Reports from Java


Using RecordEdit Layouts

The following code from ZRunRpt illustrates running a report using the RecordDataSource and the standard (Database Based) RecordEditor Layout definitions.

Note: Lines 4 and 5 read in the specified RecordEditor layout.

   1:            JasperReport jasp;
   2:            String fileName;
   3:            String inputFileName = SAMPLE_DIRECTORY + dataFile;
   4:            CopyBookDbReader copybookReader  = new CopyBookDbReader();
   5:            LayoutDetail copyBook = copybookReader.getLayout(layout);
   6:
   7:            System.out.println(">>> " + rpt + "  ~  " + layout + "  -  " + dataFile);
   8:
   9:            try {
  10:                jasp = JasperCompileManager.compileReport(REPORT_DIRECTORY + rpt + ".jrxml");
  11:                fileName = REPORT_DIRECTORY + rpt + ".pdf";
  12:                Map parameters = new HashMap();
  13:                RecordDataSource source = new RecordDataSource(copyBook, inputFileName);
  14:
  15:                if (!"".equals(record)) {
  16:                    source.setRequiredRecord(copyBook.getRecordIndex(record));
  17:                }
  18:
  19:                parameters.put("ReportTitle", rptTitle);
  20:
  21:                byte[] b = JasperRunManager.runReportToPdf(jasp, parameters, source);
  22:                FileOutputStream s = new FileOutputStream(fileName);
  23:                s.write(b);
  24:                s.close();
  25:            } catch (JRException e) {


Using Cobol Layouts

Following is an example (from ZRunCobolRpt) of running a report from a Cobol Copybook. When running from Cobol you are limited to files with only one record type.

Note: Lines 6 to 13 read in the specified RecordEditor layout.

   1:        JasperReport jasp = null;
   2:        String fileName = null;
   3:        String inputFileName = SAMPLE_DIRECTORY + dataFile;
   4:
   5:        try {
   6:            CopybookToLayout cobolConv = new CopybookToLayout();
   7:            cobolConv.setBinaryFormat(machine);
   8:            LayoutDetail copyBook = cobolConv.readCobolCopyBook(
   9:                    JasperConst.COBOL_DIRECTORY + layout + COBOL_SUFIX,
  10:                    CopybookToLayout.SPLIT_NONE,
  11:                    Common.LINE_SEPERATOR,
  12:                    null,
  13:                    Common.FIXED_LENGTH_IO,
  14:                    new TextLog()
  15:            );
  16:            jasp = JasperCompileManager.compileReport(REPORT_DIRECTORY + rpt + ".jrxml");
  17:            fileName = REPORT_DIRECTORY + rpt + ".pdf";
  18:            Map parameters = new HashMap();
  19:            RecordDataSource source = new RecordDataSource(copyBook, inputFileName);
  20:
  21:            byte[] b = JasperRunManager.runReportToPdf(jasp, parameters, source);
  22:            FileOutputStream s = new FileOutputStream(fileName);
  23:            s.write(b);
  24:            s.close();
  25:        } catch (JRException e) {

To run with windows / unix text files (on a windows / unix box) the machine on line 6 above becomes CopybookToLayout.FMT_INTEL For IBM Mainframe files use CopybookToLayout.FMT_MAINFRAME.


Prev  Up  Next