Error creating connector Script to fill table in docx template

Hi community,

I am trying to export data in a template that has tables to fill, but with the office connector "Insert data in a .docx / .odt template" I could not do it, so I am trying to generate it with a script connector following the https documentation: //github.com/opensagres/xdocreport/wiki/DocxReportingJavaMainListF ... but it generates the following error: "java.io.IOException: InputStream cannot be null."

The error occurs in these lines:

def campo4 = "http:/localhost:3849/bonita/portal/" + plantillaPrueba.url
InputStream input = XDocReportRegistry.class.getResourceAsStream(campo4)
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(input, TemplateEngineKind.Velocity);

Apparently the input variable is being empty. I have the template in a document type process variable.

Below is the complete code:

//package fr.opensagres.xdocreport.samples.docxandvelocity

import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.util.ArrayList
import java.util.List
import com.mypackage.logger
import groovy.json.*

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.Developer
//import fr.opensagres.xdocreport.samples.odtandvelocity.model.Project
import fr.opensagres.xdocreport.template.IContext
import fr.opensagres.xdocreport.template.TemplateEngineKind
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata

def idCase = processInstanceId
logger.w("Inicio - crearPlantillaCotizacion", (idCase.toString() + "-Gestionar Cotizaciones"))

public class Role {

private final String name;

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

public String getName() {
return name;
}
}

logger.w("class Role definida", (idCase.toString() + "-Gestionar Cotizaciones"))

public class Developer {

private final String name;
private final String lastName;
private final String mail;
private final List roles;

public Developer(String name, String lastName, String mail) {
this.name = name;
this.lastName = lastName;
this.mail = mail;
this.roles = new ArrayList();
}

public String getName() {
return name;
}

public String getLastName() {
return lastName;
}

public String getMail() {
return mail;
}

public Developer addRole(Role role) {
roles.add(role);
return this;
}

public List getRoles() {
return roles;
}

}

logger.w("class Developer definida", (idCase.toString() + "-Gestionar Cotizaciones"))

public class Project {

private final String name;
private String url;

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

public String getName() {
return name;
}

public void setURL(String url) {
this.url = url;
}

public String getURL() {
return url;
}
}

logger.w("class Project definida", (idCase.toString() + "-Gestionar Cotizaciones"))

// 1) Load Docx file by filling Velocity template engine and cache
// it to the registry

def campo1 = plantillaPrueba.name
logger.w("campo1: " + campo1, (idCase.toString() + "-Gestionar Cotizaciones"))

def campo2 = plantillaPrueba.contentFileName
logger.w("campo2: " + campo2, (idCase.toString() + "-Gestionar Cotizaciones"))

def campo3 = plantillaPrueba.url
logger.w("campo3: " + campo3, (idCase.toString() + "-Gestionar Cotizaciones"))

def campo4 = "http:/localhost:3849/bonita/portal/" + plantillaPrueba.url
logger.w("campo4: " + campo4, (idCase.toString() + "-Gestionar Cotizaciones"))

//def campo5 = "D:\\BonitaCommunity-2021.1\\DocxProjectWithVelocityList1.docx"
//logger.w("campo5: " + campo5, (idCase.toString() + "-Gestionar Cotizaciones"))

//File documento = new File("DocxProjectWithVelocityList1.docx")
//logger.w("documento: " + documento, (idCase.toString() + "-Gestionar Cotizaciones"))

InputStream input = XDocReportRegistry.class.getResourceAsStream(campo4)
logger.w("Paso1: input: " + input, (idCase.toString() + "-Gestionar Cotizaciones"))

IXDocReport report = XDocReportRegistry.getRegistry().loadReport(input, TemplateEngineKind.Velocity);
logger.w("Paso2", (idCase.toString() + "-Gestionar Cotizaciones"))

// 2) Create fields metadata to manage lazy loop (#foreach velocity) for table row.
FieldsMetadata metadata = report.createFieldsMetadata();
logger.w("Paso3", (idCase.toString() + "-Gestionar Cotizaciones"))

// NEW API
metadata.load( "developers", Developer.class, true);
logger.w("Paso4", (idCase.toString() + "-Gestionar Cotizaciones"))

// 3) Create context Java model
IContext context = report.createContext();
Project project = new Project( "XDocReport");
context.put( "proyecto", project);
logger.w("Paso5", (idCase.toString() + "-Gestionar Cotizaciones"))

List developers = new ArrayList();
developers.add( new Developer( "ZERR", "Angelo", "angelo.zerr@gmail.com" ) );
developers.add( new Developer( "Leclercq", "Pascal", "pascal.leclercq@gmail.com" ) );
context.put( "developers", developers );
logger.w("Paso6", (idCase.toString() + "-Gestionar Cotizaciones"))

// 4) Generate report by merging Java model with the Docx
OutputStream out = new FileOutputStream( new File( "D:/BonitaCommunity-2021.1/DocxProjectWithVelocityList_Out.docx" ) );
report.process( context, out );
logger.w("Paso7", (idCase.toString() + "-Gestionar Cotizaciones"))

logger.w("FIn - crearPlantillaCotizacion", (idCase.toString() + "-Gestionar Cotizaciones"))

return "OK"

1 Like

It appears that you are referencing an incorrect .java class:

InputStream input = XDocReportRegistry.class.getResourceAsStream(campo4);

You should probably put the name of the java class you are working on::

// InputStream input = NameOfMyClass.class.getResourceAsStream(campo4)
// ...............
// For example:

public class NameOfMyClass {
    public static void main(String[] args) {

                       InputStream input = NameOfMyClass.class.getResourceAsStream(campo4);

 

 

1 Like

Hi,

Here is an example of a project using the templating connector using tables like supported by the XdocReport library.

HTH
Romain

1 Like

I suggest contributing to the existing connector (just creating the issue already has a good value).
We may improve it to support this use case.

Thanks
Romain

1 Like

Thank Romain,

With your help I was able to solve my problem with the template.

Armando,

1 Like