Skip to content

Commit 86c1d6d

Browse files
hugosama1pivovarit
authored andcommitted
First proposition for introduction to spring-yarg (eugenp#2540)
1 parent aaa3d11 commit 86c1d6d

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,11 @@
514514
<artifactId>hirondelle-date4j</artifactId>
515515
<version>${hirondelle-date4j.version}</version>
516516
</dependency>
517+
<dependency>
518+
<groupId>com.haulmont.yarg</groupId>
519+
<artifactId>yarg</artifactId>
520+
<version>2.0.4</version>
521+
</dependency>
517522
</dependencies>
518523
<repositories>
519524
<repository>
@@ -534,6 +539,14 @@
534539
<name>OpenGeo Maven Repository</name>
535540
<url>http://repo.opengeo.org</url>
536541
</repository>
542+
<repository>
543+
<snapshots>
544+
<enabled>false</enabled>
545+
</snapshots>
546+
<id>bintray-cuba-platform-main</id>
547+
<name>bintray</name>
548+
<url>http://dl.bintray.com/cuba-platform/main</url>
549+
</repository>
537550
</repositories>
538551
<properties>
539552
<multiverse.version>0.7.0</multiverse.version>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.baeldung.yarg;
7+
8+
import com.haulmont.yarg.formatters.factory.DefaultFormatterFactory;
9+
import com.haulmont.yarg.loaders.factory.DefaultLoaderFactory;
10+
import com.haulmont.yarg.loaders.impl.JsonDataLoader;
11+
import com.haulmont.yarg.reporting.Reporting;
12+
import com.haulmont.yarg.reporting.RunParams;
13+
import com.haulmont.yarg.structure.Report;
14+
import com.haulmont.yarg.structure.ReportBand;
15+
import com.haulmont.yarg.structure.ReportOutputType;
16+
import com.haulmont.yarg.structure.impl.BandBuilder;
17+
import com.haulmont.yarg.structure.impl.ReportBuilder;
18+
import com.haulmont.yarg.structure.impl.ReportTemplateBuilder;
19+
import java.io.File;
20+
import java.io.IOException;
21+
import javax.servlet.http.HttpServletResponse;
22+
import org.apache.commons.io.FileUtils;
23+
import org.springframework.web.bind.annotation.RequestMapping;
24+
import org.springframework.web.bind.annotation.RequestMethod;
25+
import org.springframework.web.bind.annotation.RestController;
26+
27+
@RestController
28+
public class DocumentController {
29+
30+
@RequestMapping(path = "/generate/doc", method = RequestMethod.GET)
31+
public void generateDocument(HttpServletResponse response) throws IOException {
32+
ReportBuilder reportBuilder = new ReportBuilder();
33+
ReportTemplateBuilder reportTemplateBuilder = new ReportTemplateBuilder()
34+
.documentPath("./src/main/resources/Letter.docx")
35+
.documentName("Letter.docx")
36+
.outputType(ReportOutputType.docx)
37+
.readFileFromPath();
38+
reportBuilder.template(reportTemplateBuilder.build());
39+
BandBuilder bandBuilder = new BandBuilder();
40+
String json = FileUtils.readFileToString(new File("./src/main/resources/Data.json"));
41+
ReportBand main = bandBuilder.name("Main")
42+
.query("Main", "parameter=param1 $.main", "json")
43+
.build();
44+
reportBuilder.band(main);
45+
Report report = reportBuilder.build();
46+
47+
Reporting reporting = new Reporting();
48+
reporting.setFormatterFactory(new DefaultFormatterFactory());
49+
reporting.setLoaderFactory(
50+
new DefaultLoaderFactory()
51+
.setJsonDataLoader(new JsonDataLoader()));
52+
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
53+
reporting.runReport(new RunParams(report).param("param1", json), response.getOutputStream());
54+
}
55+
56+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"main": {
3+
"title" : "INTRODUCTION TO YARG",
4+
"name" : "Baeldung",
5+
"content" : "This is the content of the letter, can be anything we like."
6+
}
7+
}
12.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)