Skip to content
Andrey Subbotin edited this page Sep 20, 2021 · 83 revisions

Introduction to Yarg

This library has been developed to simplify reports generation. In short, it allows to create a template in most common formats (doc, docs, xls, xlsx, html, ftl, csv) or a custom text format and fill it with data loaded by sql, groovy or other means.

Yarg introduces generic report structure, describing how to load data, how to structurize it, how to convert it and how to show it to user. Details are in Data structure section.

Yarg splits report generation process into several steps - data loading, data conversion, templates filling. On each step you can select certain sort of actions to do. So each of these steps introduces a concept in the library: data loading introduces ReportDataLoader abstraction, data conversion introduces ReportFieldsConverter abstraction, templates filling introduces ReportFormatter abstraction. This is descibed in Data loaders and Formatters sections.

See the quick start

Why should I use this library?

From our point of view the main trend of software engineering today is reducing the costs of development. By cost we mean time spent by developers on support existing code or writing it from scratch. This library has been developed to simplify a very common task - report generation.

We have developed our own reporting engine (as part of our CUBA platform), because no free solution we could find has satisfied the requirements of our projects in terms of features and flexibility. These projects are usually enterprise applications (like document management, automation or information systems).

  1. Yarg is a ready solution, used in most of our projects.
  2. Yarg offers more than template processing, it can also manage data loading and transformation.
  3. Yarg allows users to use world standard applciations to create report templates: MS Excel and Word as well as surely OpenOffice/LibreOffice.
  4. Yarg has same rules for all supported formats: xls, xlsx, doc, docx, odt, html, ftl. This allow you to change template or data loader type, and report will work fine.
  5. Yarg allows to extend, override or add new functionallity.
  6. Yarg has simple console interface which allows to use it outside of Java world. For more information please see the topic.
  7. Yarg has simple REST server which allows to use it as a standalone service. For more information please see the topic.
  8. Yarg allows you to store report structure in simple xml (which allows to store reports in VCS).
  9. Yarg uses Groovy and it allows you to change complex reports logic at runtime.
  10. Yarg is fully embeddable. It can deal with any dependency injection frameworks (Spring, Guice, etc).
  11. The main use case for Yarg is using it as an embedded reporting engine in your application. It does not have a visual interface, but its data structure is simple so that if needed, you can easily create the interface using UI framework and design approach of your choice.

How to include binaries to my project?

Yarg versions are distributed using Nexus repository: https://repo.cuba-platform.com/content/groups/work/

There are two release lines:

  • 2.0+ version requires Java 8. Java 12 isn't supported.
  • 1.0+ version requires Java 6.

You can find the complete list of versions here: https://repo.cuba-platform.com/content/groups/work/com/haulmont/yarg/

Gradle:

repositories {
    maven {
        url "https://repo.cuba-platform.com/content/groups/work/"
    }
}
...
dependencies {
    compile 'com.haulmont.yarg:yarg:2.2.14'
}

Maven:

<repositories>
    <repository>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>repo-cuba-platform-work</id>
        <name>repo</name>
        <url>https://repo.cuba-platform.com/content/groups/work</url>
    </repository>
</repositories>
...
<dependency>
    <groupId>com.haulmont.yarg</groupId>
    <artifactId>yarg</artifactId>
    <version>2.2.14</version>
    <type>pom</type>
</dependency>

How to build it from sources?

We use Gradle to build YARG. You don't need to download and install it.

Just run gradlew assemble in the ./core folder (it contains build.gradle file). Appropriate version of Gradle will be downloaded automatically. Java 12 isn't supported.

The resulting jar file will be placed to the ./core/build/libs folder.

Data structure

Report is an object constructed from several parts.

  • ReportParameters are user defined parameters for data loading and report generation. The values for those parameters should be provided when report is invoked.
  • ReportBands are tree organized (parent - children) entities which describe how to load data to fill some template region.
  • ReportQueries are contained within bands and describe certain query (sql, groovy, etc)
  • ReportTemplates are documents (doc,docx,xls,xlsx,html or any other text format) with special markup.
  • ReportFieldFormats convert loaded data into appropriate format (date format, decimal format, etc).

See more about data structure

Data loaders

Data loaders load data which will be inserted into file template.

The result of data loader's work is list of rows containing some data.

There are several types of loaders provided by Yarg by default.

SqlDataLoader

This data loader loads data from SQL-compatible databases. See example below.

select u.user_name as name, u.user_login as login, u.user_email as email
from users u
where u.create_date < ${param1}

This query select user's name, login and email for all users created before param1 date.

GroovyDataLoader

This data loader uses groovy to load arbitrary data from anywhere. See example below.

return [[
'name':'John Doe',
'login':'jdoe',
'email':'[email protected]'
]]

JsonDataLoader

This data loader can load data from arbitrary JSON structure using JsonPath. You can see more details here.

See more about data loaders

Formatters

Yarg provides following formatters by default: DocFormatter, DocxFormatter, HtmlFormatter, XLSFormatter, XlsxFormatter, HtmlFormatter, CsvFormatter - each processing corresponding type of template file. Additionally, Yarg supports integration with JasperReports library, so you can create templates in Jasper and use them in Yarg.

Template files contain special placeholders describing the data which they will be replaced with. Placeholders can have the following forms ${fieldName}, ${BandName.fieldName}

See more about formatters

Samples

Incomes

Invoice

Breakdown