Skip to content

ODTReportingJavaMain

Angelo edited this page Jun 7, 2016 · 3 revisions

ODT Reporting in Java Main

This section explains step by step how generate a report created with OpenOffice ODT by using Velocity syntax to set fields to replace.

We will create a basic odt document ODTProjectWithVelocity.odt :

The $project.Name is an input field and project is Java model Project class which define Project#getName(). The field name start with project because we will register the Java Model with project key in the context like this :

Project project = new Project("XDocReport");
context.put("project", project);

After report process we will generate the document ODTProjectWithVelocity_Out.odt :

Steps section are :

  1. Add XDocReport JARs in your classpath project.
  2. Create Java model create your Java model.
  3. Design odt report design your odt report with OpenOffice by using Velocity syntax to set fields to replace.
  4. Generate odt report generate odt report by using XDocReport API.

If you wish to :

  • use ODT with Freemarker, you could download *odtandfreemarker-**-sample.zip.
  • use ODT with Velocity, you could download *odtandvelocity-**-sample.zip.

1. Add XDocReport JARs

XDocReport is very modular. It means that you can choose your document type (docx, odt...) as source report and use template engine syntax to set fields name (Freemarker/Velocity...).

In our case we will generate report from odt with Velocity. So we need to set well JARs in your classpath. Here screen of your Eclipse project (if you are using Eclipse) after adding XDocReport JARs :

[## Required JARs

For any document type and template engine you need to add following JARs :

*JARs name* *Description* *OSGi Bundle/Fragment*
fr.opensagres.xdocreport.core XDocReport Core Bundle
fr.opensagres.xdocreport.document Document type API Bundle
fr.opensagres.xdocreport.template Template engine API Bundle
fr.opensagres.xdocreport.converter Converter API Bundle
commons-io-1.4 Commons IO

ODT Document JAR

In this section we wish using odt as document source for report, so you need to add ODT document implementation JAR:

*JARs name* *Description* *OSGi Bundle/Fragment*
fr.opensagres.xdocreport.document.odt ODT Document implementation Fragment -> fr.opensagres.xdocreport.document

Note for OSGi : this JAR is a fragment linked to the bundle OSGi fr.opensagres.xdocreport.document.

Velocity JARs

In this section we wish using Velocity as syntax to set fields, so you need to add Velocity template engine implementation and Velocity JARs.

Velocity Template JAR

Add Velocity template engine implementation JAR:

*JARs name* *Description* *OSGi Bundle/Fragment*
fr.opensagres.xdocreport.template.velocity Velocity template engine implementation Fragment -> fr.opensagres.xdocreport.template

Note for OSGi : this JAR is a fragment linked to the bundle OSGi fr.opensagres.xdocreport.template.

Velocity JARs

Add Velocity JARs :

*JARs name* *Description*
velocity-1.7 Velocity
commons-collections-3.2.1 Commons Collection
commons-lang-2.4 Commons Lang
oro-2.0.8 ORO

2. Create Java model

Now you can create your Java model that you wish use in your odt report. In this section we will use Java Project model like this :

package fr.opensagres.xdocreport.samples.odtandvelocity.model;

public class Project {

  private final String name;

  public Project(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }
}

3. Design ODT report

At this step you can create your odt report with OpenOffice with the content :

Project: $project.Name

(http://wiki.xdocreport.googlecode.com/git/screenshots/ODTProjectWithVelocity_AddJARs.png))

For the field to replace $project.Name, it is advice to use Input-Field and not to type directly $project.Name in your documentation (TODO : explain why).

You can note for this sample that you can style field. If you don't know how create Input-Field, please see ODT Design Report section.

4. Generate odt report

Once you have create odt, save it in your project. In this sample the odt report was saved with ODTProjectWithVelocity.odt name in the fr.opensagres.xdocreport.samples.odtandvelocity package of the Java project.

Create Java class ODTProjectWithVelocity to load ODTProjectWithVelocity.odt and generate report in file ODTProjectWithVelocity_Out.odt like this :

package fr.opensagres.xdocreport.samples.odtandvelocity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.samples.odtandvelocity.model.Project;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;

public class ODTProjectWithVelocity {

  public static void main(String[args) {
    try {
      // 1) Load ODT file by filling Velocity template engine and cache it to the registry
      InputStream in = ODTProjectWithVelocity.class.getResourceAsStream("ODTProjectWithVelocity.odt");
      IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in,TemplateEngineKind.Velocity);

      // 2) Create context Java model
      IContext context = report.createContext();
      Project project = new Project("XDocReport");
      context.put("project", project);

      // 3) Generate report by merging Java model with the ODT
      OutputStream out = new FileOutputStream(new File("ODTProjectWithVelocity_Out.odt"));
      report.process(context, out);

    } catch (IOException e) {
      e.printStackTrace();
    } catch (XDocReportException e) {
      e.printStackTrace();
    }
  }
}

Your Eclipse project looks like this :

(])

Run ODTProjectWithVelocity Java class and ODTProjectWithVelocity_Out.odt will be generated in your project home :

[If you open ODTProjectWithVelocity_Out.odt you will see :

(http://wiki.xdocreport.googlecode.com/git/screenshots/ODTProjectWithVelocity_WorkspaceOut.png))

More features

Clone this wiki locally