[YARG] How to integrate with a webapp using Spring MVC

Hello, I’d like to know what value to set to documentPath if using this in a web application.

This was the guide I used:

currently, I set it to a local path. And it works perfectly.
However, if I deploy it to the cloud, what location should I set it to?

incomes.xml

<report name="report">
    <templates>
        <template code="DEFAULT" documentName="incomes.xlsx" documentPath="D:\\localpath\\incomes.xlsx" outputType="xlsx" outputNamePattern="incomes.xlsx"/>
    </templates>
    <rootBand name="Root" orientation="H">
        <bands>
            <band name="Header" orientation="H"/>
            <band name="Incomes" orientation="H">
                <queries>
                    <query name="Data_set_1" type="groovy">
                        <script>
                            return [['month':'Jan', 'profit':10000], ['month': 'Feb', 'profit': 12000], ['month': 'March', 'profit': 15000], ['month': 'Apr', 'profit': 12000]]
                        </script>
                    </query>
                </queries>
            </band>
            <band name="Footer" orientation="H"/>
            <band name="Chart" orientation="H"/>
        </bands>
        <queries/>
    </rootBand>
</report>

This is my controller.
ReportController.java


    @RequestMapping(value="/reports/getpdf", method=RequestMethod.GET)
    public @ResponseBody ResponseEntity<byte[]> getPDF() throws IOException {
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource("incomes.xml").getFile());
        Report report = new DefaultXmlReader().parseXml(readFileToString(file));

        Reporting reporting = new Reporting();
        reporting.setFormatterFactory(new DefaultFormatterFactory());
        reporting.setLoaderFactory(
                new DefaultLoaderFactory()
                        .setGroovyDataLoader(new GroovyDataLoader(new DefaultScriptingImpl())));

        ReportOutputDocument reportOutputDocument = reporting.runReport(new RunParams(report));

        byte[] contents = reportOutputDocument.getContent();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
        String filename = "output.xlsx";
        headers.set("Content-Disposition", "attachment; filename=" + filename);
        headers.setContentLength(contents.length);
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");

        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
        return response;
    }

Hello.

The best way I can see is to override com.haulmont.yarg.structure.xml.impl.DefaultXmlReader#getDocumentContent method to load templates from:

  1. Classpath
    or
  2. Cloud file system

I would suggest to load them from classpath - this is the simplest way. Let me provide the snippet for you.


protected InputStream getDocumentContent(String documentPath) throws FileNotFoundException {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(documentPath);
 }
1 Like

Thank you. I’ll try this.

Hello Eugene,

Thank you. Your solution worked!

Albeit I had to build the project from source.
In v1.0.56., the return type of com.haulmont.yarg.structure.xml.impl.DefaultXmlReader#getDocumentContent was still FileInputStream.

Also, I had to include all the yarg gradle dependencies to my webapp’s own .


compile(group: 'commons-cli', name: 'commons-cli', version: '1.2')
...
compile(group: "com.jayway.jsonpath", name: "json-path", version: "2.1.0")

If I didn’t include those dependencies, my call to yarg classes results to ClasDefNotFound exception.

Hello.
I’m glad to hear that your application now works fine.

  1. The return type has been changed in 1.0.58. The latest version in Maven is 1.0.59. Could you please try the newest version?
  2. The dependencies should be included automatically. Could you please provide your gradle file? (old version, before you have included the dependencies).

Thank you.

Thank you I’ll get the 1.59. Perhaps update the wiki to change the version from 1.56 to 1.59:
[url=https://github.com/Haulmont/yarg/wiki]https://github.com/Haulmont/yarg/wiki[/url]

Attaching my build.gradle

build.gradle.txt (2.2K)

Well, when you use your own compiled jars you have to include YARG’s depenndecies manually.

But if you use the version from Maven - gradle should download all dependencies automatically and link your project with them.

1 Like

Yes, I’m not that familiar with Maven I thought that YARG jars were enough.
Anyway, I used 1.59 as a dependency in my gradle and all is working

Thank you again and I saw that the wiki now displays 1.59.